PowerShell - 获取服务器的平均内存使用量

时间:2014-08-22 18:13:12

标签: powershell powershell-v2.0

我正在运行Windows Server 2012 R2的实例,并希望获得我的服务器的平均内存使用量。

为了获得CPU使用率,我使用

Get-WmiObject win32_processor | select LoadPercentage  |fl

为了获得平均CPU使用率,我有

Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average | Select Average

如何在内存使用方面做同样的事情?

3 个答案:

答案 0 :(得分:2)

您需要Win32_OperatingSystem命名空间,它是TotalVisibleMemorySize(物理内存),FreePhysicalMemory,TotalVirtualMemorySize和FreeVirtualMemory属性。

Get-WmiObject win32_OperatingSystem |%{"Total Physical Memory: {0}KB`nFree Physical Memory : {1}KB`nTotal Virtual Memory : {2}KB`nFree Virtual Memory  : {3}KB" -f $_.totalvisiblememorysize, $_.freephysicalmemory, $_.totalvirtualmemorysize, $_.freevirtualmemory}

那将吐回来:

Total Physical Memory: 4079572KB
Free Physical Memory : 994468KB
Total Virtual Memory : 8157280KB
Free Virtual Memory  : 3448916KB

如果你想要使用而不是免费,我相信你可以做数学。

修改:您的CPULoad平均值并非真正的平均值。一个很好的例子:

For($a=1;$a -lt 30;$a++){
    Get-WmiObject win32_processor|ForEach{
        [pscustomobject][ordered]@{
            'Average' = $_ | Measure-Object -property LoadPercentage -Average | Select -expand Average
            'Current' = $_ | select -expand LoadPercentage
        }
    }
    Start-Sleep -Milliseconds 50
}

结果:

Average                                          CPU Load
-------                                          --------
2                                                 2
1                                                 1
1                                                 1
1                                                 1
1                                                 1
5                                                 5
1                                                 1
1                                                 1
0                                                 0
1                                                 1
1                                                 1
1                                                 1
2                                                 2
4                                                 4
0                                                  
1                                                 1
7                                                 7
24                                                24
1                                                 1

答案 1 :(得分:0)

如果您希望在一段时间内获得平均值,则可以使用效果计数器

### Get available memory in MB ###
$interval = 1 #seconds
$maxsamples = 5
$memorycounter = (Get-Counter "\Memory\Available MBytes" -maxsamples $maxsamples -sampleinterval $interval | 
select -expand countersamples | measure cookedvalue -average).average
### Memory Average Formatting ###
$freememavg = "{0:N0}" -f $memorycounter
### Get total Physical Memory & Calculate Percentage ###
$physicalmemory = (Get-WMIObject -class Win32_PhysicalMemory | Measure-Object -Property capacity -Sum).Sum / 1mb
$physicalmemory - $freememavg

答案 2 :(得分:0)

Function Get-ADtop {
[CmdletBinding()]
param(
    [String]$ComputerName,
    [String]$Sort = "none",  
    [String]$BaseDN = "OU=systems,DC=domain,DC=com",  # Edit Default Base DN
    [String]$SampleTime = 2
)
If ($ComputerName) {
    $Computers = $ComputerName
} else {
    $Computers = Get-ADComputer -Filter * -Properties * -SearchBase $BaseDN -EA SilentlyContinue | % {$_.Name}
}
$DataSet = @()
$Targets = @()
ForEach ($Comp in $Computers) {
    If (Test-Connection -ComputerName $Comp -Count 1 -Quiet -TimeToLive 1 -EA SilentlyContinue) {
        If (!(Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue)) { break }
        $Targets += $Comp
    }
}
$CompCount = $Computers | Measure-Object | % {$_.Count}
$DeadCount = $CompCount - ($Targets | Measure-Object | % {$_.Count})
If (!($DeadCount -eq 0)) {
    Write-Host "`n$DeadCount unavailable computers removed"
}
Write-Host "`nGathering realtime CPU/MEM/DISK Usage data from $CompCount computers..."
ForEach ($Comp in $Targets) {
    $proc = (Get-WmiObject -ComputerName $Comp -class win32_processor -EA SilentlyContinue | Measure-Object -property LoadPercentage -Average | Select Average | % {$_.Average / 100}).ToString("P")
    $mem = Get-WmiObject -ComputerName $Comp win32_OperatingSystem -EA SilentlyContinue 
    $mem = (($mem.TotalVisibleMemorySize - $mem.FreePhysicalMemory) / $mem.TotalVisibleMemorySize).ToString("P")
    $disk = Get-WmiObject -ComputerName $Comp -class Win32_LogicalDisk -filter "DriveType=3" -EA SilentlyContinue 
    $disk = (($disk.Size - $disk.FreeSpace) / $disk.Size).ToString("P")
    $Info = [pscustomobject]@{
        'Computer' = $Comp
        'CPU Usage' = $proc
        'MEM Usage' = $mem
        'Disk Usage' = $disk
    }
    $DataSet += Add-Member -InputObject $Info -TypeName Computers.CPU.Usage -PassThru    
}
Switch ($Sort) {
    "none" { $DataSet }
    "CPU" { $DataSet | Sort-Object -Property "CPU Usage" -Descending }
    "MEM" { $DataSet | Sort-Object -Property "MEM Usage" -Descending }
    "DISK" { $DataSet | Sort-Object -Property "DISK Usage" -Descending }
}
}

More info here GitHub Gist Link