Ajay Pathak
asked on
script for CPU usage,Memory usage and Free disk sapce
Hi,
I am trying to send some information to management. Can you help in getting information for CPU usage, Memory used and free disk space and number of user logged in. Powershell script will be ideal. We have win2k, win2k3 servers in our domain and need this info for all 4K servers in 2 domains.
Thanks
Ajay
I am trying to send some information to management. Can you help in getting information for CPU usage, Memory used and free disk space and number of user logged in. Powershell script will be ideal. We have win2k, win2k3 servers in our domain and need this info for all 4K servers in 2 domains.
Thanks
Ajay
ASKER
The combined script is not working. need script which can export the data to csv or xls.
Thanks
Thanks
Echo "Free HDD Space"
fsutil volume diskfree C:\ > c:\status.xls
systeminfo |find "Available Physical Memory" >> c:\status.xls
systeminfo |find "Virtual Memory: In Use:" >> c:\status.xls
typeperf "\Processor(_Total)\% Processor Time" -sc 1 >> c:\status.xls
make a file extension in .bat.If the output is ok for you then we need to some modification in it.
fsutil volume diskfree C:\ > c:\status.xls
systeminfo |find "Available Physical Memory" >> c:\status.xls
systeminfo |find "Virtual Memory: In Use:" >> c:\status.xls
typeperf "\Processor(_Total)\% Processor Time" -sc 1 >> c:\status.xls
make a file extension in .bat.If the output is ok for you then we need to some modification in it.
ASKER
Total # of free bytes : 709492736
Total # of bytes : 42949668864
Total # of avail free bytes : 709492736
Available Physical Memory: 1,908 MB
I am getting this output. Need to have CPU usage and the total memory with other drive ( Free space and total disk space)info..
Thanks a lot for giving some idea.
Total # of bytes : 42949668864
Total # of avail free bytes : 709492736
Available Physical Memory: 1,908 MB
I am getting this output. Need to have CPU usage and the total memory with other drive ( Free space and total disk space)info..
Thanks a lot for giving some idea.
Try this code… You can input the server in a text file (C:\Server.txt), if the CPU, Memory & Total Free space shown empty then it means the server is not reachable..
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
ASKER
Hi Subsun,
Thanks for this. Can we incorportae logged in users in this script.
This will give me the full solution.
Thanks for this. Can we incorportae logged in users in this script.
This will give me the full solution.
Here is the updated code.. let me know if you have any questions..
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
ASKER
Hi Subsun,
Thanks. My requirement is to have to have the last logged in user with time stamp so that we can get idea about the server usage.
Thanks. My requirement is to have to have the last logged in user with time stamp so that we can get idea about the server usage.
It's a rather complex task to get last logged in user.. However you can try with the
Get-LastLogon function from the Microsoft Script Center...
You can copy the functin from following link
http://gallery.technet.microsoft.com/scriptcenter/Get-LastLogon-Determining-283f98ae
Here is the updated code with Get-LastLogon function..
Get-LastLogon function from the Microsoft Script Center...
You can copy the functin from following link
http://gallery.technet.microsoft.com/scriptcenter/Get-LastLogon-Determining-283f98ae
Here is the updated code with Get-LastLogon function..
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..
ASKER
Hi Subsun,
Greats Thaks, But this script is giving total free space on the server. I need free space info. for only C Drive.
Also the script is stopping if the server is in hung state but is pinging.
I am using this script.
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.TotalVisibleMemory Size - $Mem.FreePhysicalMemory)*1 00)/ $Mem.TotalVisibleMemorySiz e)) %"
"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}},"C PU usage","Memory usage","Total FreeSpace","logged Users"
}
}| Select Server,"CPU usage","Memory usage","Total FreeSpace","logged Users" |
Export-Csv C:\report.csv -nti
Greats Thaks, But this script is giving total free space on the server. I need free space info. for only C Drive.
Also the script is stopping if the server is in hung state but is pinging.
I am using this script.
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.TotalVisibleMemory
"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}},"C
}
}| Select Server,"CPU usage","Memory usage","Total FreeSpace","logged Users" |
Export-Csv C:\report.csv -nti
Change line 10 to following to get the C Drive free space..
If it gives error and stop for a server, then try by adding following error action preference as a first line of your code and see if it helps..
$ErrorActionPreference = "SilentlyContinue"
"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?
If it gives error and stop for a server, then try by adding following error action preference as a first line of your code and see if it helps..
$ErrorActionPreference = "SilentlyContinue"
ASKER
Is it giving error? or the script itself is not responding?
the script itself is not responding . Iam checking for C drive free space now..
the script itself is not responding . Iam checking for C drive free space now..
ASKER
Hi Subsun,
Script is giving the required information. Can we get the time and date for the script in the same sheet means when this script was run..
Thanks
Script is giving the required information. Can we get the time and date for the script in the same sheet means when this script was run..
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello,
How Come I tried your code and it's not giving me a result?
How Come I tried your code and it's not giving me a result?
Hello Subsun,
I've tried your script above, and it does not show me any result.
I've tried your script above, and it does not show me any result.
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