Board logo

标题: [转载代码] PowerShell实现Windows服务器性能查询 [打印本页]

作者: DAIC    时间: 2013-9-8 20:50     标题: PowerShell实现Windows服务器性能查询

  1. #查询服务器性能
  2. $cpu=((get-counter -counter "\processor(_total)\% processor time").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue
  3. $men = gwmi  win32_OperatingSystem
  4. $hostname=((Gwmi Win32_NetworkAdapterConfiguration|?{$_.IPEnabled -and $_.DefaultIPGateway}).IPAddress)[0]
  5. $Disks = gwmi win32_logicaldisk|?{$_.drivetype -eq 3}
  6. $Havecpu = "{0:0.0} %" -f $cpu
  7. $Allmen = "{0:0.0} MB" -f ($men.TotalVisibleMemorySize  / 1mB)
  8. $Freemen = "{0:0.0} MB" -f ($men.FreePhysicalMemory  / 1mB)
  9. $Permem =  "{0:0.0} %" -f ((($men.TotalVisibleMemorySize-$men.FreePhysicalMemory)/$men.TotalVisibleMemorySize)*100)
  10. $logical_read=" {0:0.0} KB" -f ($(((get-counter -counter "\LogicalDisk(_total)\Disk Read Bytes/sec").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue) / 1KB)
  11. $logical_write=" {0:0.0} KB" -f ($(((get-counter -counter "\LogicalDisk(_total)\Disk Write Bytes/sec").CounterSamples|where {$_.InstanceName -eq "_total" }).CookedValue) / 1KB)
  12. $network_sent=" {0:0.0} KB" -f ($((Get-Counter -Counter "\Network Interface(*)\Bytes Sent/sec").CounterSamples|%{$_.CookedValue}|sort|select -last 1) / 1KB)
  13. $network_rece=" {0:0.0} KB" -f ($((Get-Counter -Counter "\Network Interface(*)\Bytes Received/sec").CounterSamples|%{$_.CookedValue}|sort|select -last 1) / 1KB)
  14. Write-Host "  $(,"="*25)
  15. $(get-date)  服务器:" $($hostname.Padleft(16)) "
  16. CPU利用率:$($Havecpu.Padleft(8))" <#内存总数:$Allmen 内存可用数:$Freemen#> " 内存使用率:$($Permem.Padleft(12) )
  17. 网络发/秒: $($network_sent.Padleft(8))  网络收/秒: $($network_rece.Padleft(13) )
  18. 磁盘读/秒: $($logical_read.Padleft(8))  磁盘写/秒: $($logical_write.Padleft(13) )
  19. 盘符   盘总空间  空闲空间  使用空间  使用百分比" -ForegroundColor Green
  20. foreach ($Disk in $Disks) {
  21.     $Size = "{0:0.0} GB" -f ($Disk.Size / 1GB )
  22.     $FreeSpace = " {0:0.0} GB" -f ($Disk.FreeSpace / 1GB)
  23.     $Used = ([int64]$Disk.size - [int64]$Disk.freespace)
  24.     $SpaceUsed = " {0:0.0} GB" -f ($Used / 1GB)
  25.     $Percent ="{0:0.0} %" -f ($Used * 100 / $Disk.Size)
  26.     #Write-Host " "$Disk.deviceid $Disk.volumename"  `t$Size `t$FreeSpace `t$SpaceUsed `t$Percent" -ForegroundColor Green
  27.     $n=5
  28.     Write-Host  "  "$Disk.deviceid.PadRight($n) -no -ForegroundColor Green
  29.     $n=10
  30.     Write-Host  $Size.Padleft($n) -no -ForegroundColor Green
  31.     Write-Host  $FreeSpace.Padleft($n) -no -ForegroundColor Green
  32.     Write-Host  $SpaceUsed.Padleft($n) -no -ForegroundColor Green
  33.     Write-Host  $Percent.Padleft($n)  -ForegroundColor Green
  34. }
复制代码
http://hi.baidu.com/shrekzz/item/a40360495a395be9bdf45188




欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2