使用CPU%,磁盘%和内存%计数器

-1 windows powershell scripting performance

我试图通过PowerShell获得这3个计数器,你能帮忙吗?如下所示:

Hostname1 : CPU% : 75%
Hostname1 : MEM% : 55%
Hostname1 : Disk1 % : 15%
Hostname1 : Disk2 % : 10%
Hostname1 : Disk3 % : 13%
Hostname1 : Disk4 % : 12%
Hostname2 : CPU% : 75%
Hostname2 : MEM% : 55%
Hostname2 : Disk1 % : 11%
Hostname2 : Disk2 % : 15%
Hostname2 : Disk3 % : 15%
Run Code Online (Sandbox Code Playgroud)

注意:我找不到%used/memory的计数器所以我不会通过性能计数器.

Sin*_*ard 6

可能你最简单的方法就是使用WMI.以下是我为你准备的剧本,以展示这种能力.

你需要处理格式化,我遗漏了磁盘统计信息 - 所以需要一些工作.

# Lets import our list of computers
$computers = get-Content .\computer-list.txt
# computer-list.txt is your hostnames each on a new line

# Lets create our variables
$HostInfo = @()

# Lets loop through our computer list from computers
foreach ($computer in $computers) {
    # Lets get our stats
    # Lets create a re-usable WMI method for CPU stats
    $ProcessorStats = Get-WmiObject win32_processor -computer $computer
    $ComputerCpu = $ProcessorStats.LoadPercentage 
    # Lets create a re-usable WMI method for memory stats
    $OperatingSystem = Get-WmiObject win32_OperatingSystem -computer $computer
    # Lets grab the free memory
    $FreeMemory = $OperatingSystem.FreePhysicalMemory
    # Lets grab the total memory
    $TotalMemory = $OperatingSystem.TotalVisibleMemorySize
    # Lets do some math for percent
    $MemoryUsed = ($FreeMemory/ $TotalMemory) * 100
    $PercentMemoryUsed = "{0:N2}" -f $MemoryUsed

    # Lets throw them into an object for outputting
    $objHostInfo = New-Object System.Object
    $objHostInfo | Add-Member -MemberType NoteProperty -Name Name -Value $computer
    $objHostInfo | Add-Member -MemberType NoteProperty -Name CPULoadPercent -Value $ComputerCpu
    $objHostInfo | Add-Member -MemberType NoteProperty -Name MemoryUsedPercent -Value $PercentMemoryUsed

    # Lets dump our info into an array
    $HostInfo += $objHostInfo
}

# Lets output to the console
$HostInfo
Run Code Online (Sandbox Code Playgroud)

  • 你真正想要的是:`$ MemoryUsed = 100 - (($ FreeMemory/$ TotalMemory)*100) (2认同)