-1

我正在尝试通过 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 的计数器,所以我不会通过性能计数器。

4

2 回答 2

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
于 2012-07-16T05:50:36.763 回答
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)
}
于 2016-04-01T13:39:50.560 回答