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

时间:2012-07-13 12:24:03

标签: windows performance powershell scripting

我试图通过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%

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

2 个答案:

答案 0 :(得分:5)

可能您最简单的方法是使用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

答案 1 :(得分:1)

这有效:

$system = Get-WmiObject win32_OperatingSystem
$totalPhysicalMem = $system.TotalVisibleMemorySize
$freePhysicalMem = $system.FreePhysicalMemory
$usedPhysicalMem = $totalPhysicalMem - $freePhysicalMem
$usedPhysicalMemPct = [math]::Round(($usedPhysicalMem / $totalPhysicalMem) * 100,1)


$driveLetters = Get-WmiObject Win32_Volume | select DriveLetter
foreach ($driveLetter in $driveLetters)
{
    $drive = Get-WmiObject Win32_Volume | where {$_.DriveLetter -eq $driveLetter.DriveLetter}
    $usedDiskSpace = $drive.Capacity - $drive.FreeSpace
    $usedDiskSpacePct = [math]::Round(($usedDiskSpace / $drive.Capacity) * 100,1)
}