Powershell脚本获取cpu利用率,磁盘空间,MemoryUsage和用户

时间:2017-09-15 07:24:09

标签: powershell csv memory-management cpu-usage diskspace

嗨有人可以帮助我获得所需的输出。

GC D:\ServerList.txt | % {

$xl = New-Object -ComObject "Excel.Application"
$xl.Visible = $true
$xl.DisplayAlerts = $false #for debugging, no prompts to save, etc.
$ConvertToGB = (1024 * 1024 * 1024)

$wkbk = $xl.Workbooks.Add()
$sheet = $wkbk.WorkSheets.Item(1)
$sheet.Name = "Transposed"
$Comp = $_
If (Test-Connection $Comp -Quiet) {
    $Luser = (Get-WmiObject -class win32_process -Filter "Name='Explorer.exe'" -ComputerName $Comp |
            % {$_.GetOwner().User} | Sort-Object -Unique) -join ","
    $Mem = GWMI -Class win32_operatingsystem -computername $COMP
    New-Object PSObject -Property @{
        Server         = $Comp
        "CPU_Usage"    = "$((GWMI -ComputerName $COMP win32_processor | Measure-Object -property LoadPercentage -Average).Average)"
        "Memory_Usage" = "$("{0:N2}" -f ((($Mem.TotalVisibleMemorySize - $Mem.FreePhysicalMemory)*100)/ $Mem.TotalVisibleMemorySize)) %"
        "DiskSpace"    = "$("{0:N2}" -f (Get-WmiObject -Class win32_logicaldisk -ComputerName $COMP -Filter "DriveType = 3"| Select-Object "Size","FreeSpace"))"
        #$Comp = ($disk.Size / $ConvertToGB),($disk.FreeSpace / $ConvertToGB)
        "logged_Users" = $Luser
    }
}
Else {

}
$results = Get-Service -Name *bits*| Select Server    #,"CPU usage","Memory usage","DiskFreeSpace","logged Users" 

$column = 1
$row = 1
foreach ($psRow in $results) {
    foreach ($item in $psRow.PSObject.Properties) {
        $sheet.Cells.Item($row, $column) = $item.Name
        $column++
        #$sheet.Cells.Item($row, $column) = $item."CPU","Memory_Usage","DiskSpace","logged_Users"
        $row++
        $column--
    }

} 

}

我得到的输出

Memory_Usage : 68.76 %
CPU_Usage    : 12
Server       : DIN35002781
logged_Users : aanshuma
DiskSpace    : @{Size=250051817472; FreeSpace=164924461056}**

但我想要的输出是:

DiskSpace,127696629760,46401740800
Server,LIN35006176
Memory_Usage,51.22
logged_Users,Boss
CPU_Usage,14

1 个答案:

答案 0 :(得分:0)

我首先将PSObject保存到变量中,不要在DiskSpace上使用字符串。

$obj =New-Object PSObject -Property @{
    Server         = $Comp
    "CPU_Usage"    = "$((GWMI -ComputerName $COMP win32_processor | Measure-Object -property LoadPercentage -Average).Average)"
    "Memory_Usage" = "$("{0:N2}" -f ((($Mem.TotalVisibleMemorySize - $Mem.FreePhysicalMemory)*100)/ $Mem.TotalVisibleMemorySize)) %"
    "DiskSpace"    = Get-WmiObject -Class win32_logicaldisk -ComputerName $COMP -Filter "DriveType = 3"| Select-Object "Size","FreeSpace"
    #$Comp = ($disk.Size / $ConvertToGB),($disk.FreeSpace / $ConvertToGB)
    "logged_Users" = $Luser
}

要获得所需的输出,您可以使用:

Write-Output "DiskSpace, $($obj.DiskSpace.Size), $($obj.DiskSpace.FreeSpace)"
Write-Output "Server, $($obj.Server)"
Write-Output "Memory_Usage, $($obj.Memory_Usage)"
Write-Output "logged_Users, $($obj.logged_Users)"
Write-Output "CPU_Usage, $($obj.CPU_Usage)"

如果您想要CSV,可以使用:

$obj | ConvertTo-Csv

但是我会在PSObject的两个属性中分离FreeSpace和Size。