GC C:\Server.txt | % {
$Comp = $_
If (Test-Connection $Comp -Quiet){
$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)) %"
"Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_Volume -ComputerName $COMP -Filter "DriveType = '3'" | Measure-Object -property FreeSpace -Sum).Sum /1GB)) GB"
}
}
Else{
"" | Select @{N="Server";E={$Comp}},"CPU usage","Memory usage","Total FreeSpace"
}
}| Select Server,"CPU usage","Memory usage","Total FreeSpace" |
Export-Csv C:\report.csv -nti
GC C:\Server.txt | % {
$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)) %"
"Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_Volume -ComputerName $COMP -Filter "DriveType = '3'" | Measure-Object -property FreeSpace -Sum).Sum /1GB)) GB"
"logged Users" = $Luser
}
}
Else{
"" | Select @{N="Server";E={$Comp}},"CPU usage","Memory usage","Total FreeSpace","logged Users"
}
}| Select Server,"CPU usage","Memory usage","Total FreeSpace","logged Users" |
Export-Csv C:\report.csv -nti
GC C:\Server.txt | % {
$Comp = $_
If (Test-Connection $Comp -Quiet){
$Luser = Get-LastLogon $Comp
$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)) %"
"Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_Volume -ComputerName $COMP -Filter "DriveType = '3'" | Measure-Object -property FreeSpace -Sum).Sum /1GB)) GB"
"logged Users" = $Luser.User
Time = $Luser.Time
}
}
Else{
"" | Select @{N="Server";E={$Comp}},"CPU usage","Memory usage","Total FreeSpace","logged Users",Time
}
}| Select Server,"CPU usage","Memory usage","Total FreeSpace","logged Users",Time |
Export-Csv C:\report.csv -nti
let me know if you have any questions..
"Total FreeSpace" = "$("{0:N2}" -f ((Get-WmiObject -Class win32_Volume -ComputerName $COMP -Filter "DriveLetter='C:'").FreeSpace /1GB)) GB"
Also the script is stopping if the server is in hung state but is pinging.Is it giving error? or the script itself is not responding?
Please find the below link for script which conclusion by microsoft forum.
http://social.technet.microsoft.com/Forums/en-US/06b92d3a-30d7-4d26-92d1-b53ec8ddbf3c/combining-scriptsfunctions-to-get-cpu-usage-memory-usage-and-free-disk-space?forum=winserverpowershell
Regards,
Xerox