Advertisement
Guest User

Untitled

a guest
Nov 14th, 2016
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. Param([string]$Computers) #Must supply a comma seperated list of servers
  2. $Threshold = 20 #Only show CPU over this number
  3. $NoP = 20 #Number of processes to list
  4. $NoRS = 4 #Number of result sets
  5. If (! $Computers) {
  6. Write-Host "Connection to server failed - please specify a server name." -ForegroundColor Red
  7. Break
  8. } Else {
  9. $ComputerList = $Computers -Split " "#,[StringSplitOptions]'RemoveEmptyEntries')
  10. }
  11. $Credential = $host.ui.PromptForCredential("Need credentials", "Please enter your user name and password.", "", "NetBiosUserName")
  12. If (! $Credential) {
  13. Write-Host "Authentication failed - please verify your username and password." -ForegroundColor Red
  14. Break
  15. }
  16. $UserName = $Credential.Username
  17. $Password = $Credential.GetNetworkCredential().Password
  18. $CurrentDomain = "LDAP://" + ([ADSI]"").distinguishedName
  19. $Domain = New-Object System.DirectoryServices.DirectoryEntry($CurrentDomain,$UserName,$Password)
  20. If ($Domain.Name -eq $null){
  21. Write-Host "Authentication failed - please verify your username and password." -ForegroundColor Red
  22. Break
  23. }
  24. ForEach ($ComputerName In $ComputerList) {
  25. $LoadPercentage = $Processors.LoadPercentage
  26. If (!$LoadPercentage) {$LoadPercentage = 0}
  27. Write-Host "Server: $ComputerName (CPU load $LoadPercentage%)" -NoNewline
  28. $Processors = Get-WmiObject win32_processor -ComputerName $ComputerName -Credential $Credential
  29. $i = 1
  30. $TopProcess = @()
  31. $PercentComplete = 0
  32. Do{
  33. $PercentComplete = [Math]::Floor($i/$NoRS*100)
  34. Write-Progress -Activity $ComputerName -Status "$PercentComplete% Complete:" -PercentComplete $PercentComplete
  35. $ProcessList = gwmi Win32_PerfFormattedData_PerfProc_Process -ComputerName $ComputerName -Credential $Credential |
  36. Select IDProcess,Name,PercentProcessorTime |
  37. Where {$_.Name -ne "_Total" -and $_.Name -ne "Idle"} |
  38. Sort PercentProcessorTime -Descending |
  39. Select -First $NoP
  40. ForEach ($Process In $ProcessList) {
  41. $row = New-Object PSObject -Property @{
  42. Id = $Process.IDProcess
  43. Name = $Process.Name
  44. User = (gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}).GetOwner().User
  45. CPU = $Process.PercentProcessorTime/$Processors.NumberOfLogicalProcessors -f {P}
  46. Description = (gwmi Win32_Process -ComputerName $ComputerName -Credential $Credential | Where {$_.ProcessId -eq $Process.IDProcess}).Description
  47. }
  48. $TopProcess += $row
  49. }
  50. $i++
  51. } While ($i -lt $NoRS + 1)
  52. Write-Progress -Activity $ComputerName -Completed
  53. $Group = $TopProcess | Where {$_.CPU -gt $Threshold} | Group 'ID' | Where Count -eq $NoRS
  54. If (!$Group) {
  55. Write-Host " has no processes persistently above $Threshold percent CPU usage."
  56. } Else {
  57. $Processes = @()
  58. ForEach ($Groupee In $Group) {
  59. $Ungroup = $Groupee | Select -ExpandProperty Group
  60. $CPU = 0
  61. ForEach ($ugr in $Ungroup) {
  62. $CPU += $ugr.CPU
  63. }
  64. $row = new-object PSObject -Property @{
  65. Id = $Ungroup.Id | Select -First 1
  66. Name = $Ungroup.Name | Select -First 1
  67. CPU = $CPU/$NoRS
  68. User = $Ungroup.User | Select -First 1
  69. Description = $Ungroup.Description | Select -First 1
  70. }
  71. $Processes += $row
  72. }
  73. $Processes | Format-Table @{Expression={$_.User};Label="User Name";width=25},@{Expression={$_.CPU};Label="CPU";width=5},@{Expression={$_.Id};Label="ID";width=8},@{Expression={$_.Description};Label="Description";width=48}
  74. }
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement