Reputation: 3519
I have seen - http://technet.microsoft.com/en-us/library/hh849832.aspx
And, I have seen: How to get CPU usage & Memory consumed by particular process in powershell script
I understand that I can send a: Get-process tomcat*| Select-Object CPU inside of Powershell
And the biggest issue that I see is that CPU is not returned either locally or remotely..
I have put together the following script.. The CPU portion is now working..
writeHtmlHeader $TomcatMemFileName
writeTableHeader $TomcatMemFileName
[int]$i = 1
foreach ($server in Get-Content $serverlist)
{
$Computer=$server.split(",")[0]
$ip=$server.split(",")[1]
IF ( $Computer -notmatch 'DB' ) {
$procs = Gps tomcat* -ComputerName $Computer
foreach ($proc in $procs){
$id = $proc.ID
$machine = $proc.MachineName
$process = $proc.ProcessName
$WorkingSet = [int64]($proc.WorkingSet64/1024)
$VirtualMem = [int]($proc.VM/1MB)
$cpuse= Get-Counter -computername $machine '\Process(tomcat6)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property cookedvalue
foreach ($cpus in $cpuse){
$cpu=$cpus.cookedvalue
write-host $TomcatMemFileName $i $machine.ToUpper() $id $process $WorkingSet $VirtualMem $cpu
writeTomcatInfo $TomcatMemFileName $i $machine.ToUpper() $id $process $WorkingSet $VirtualMem $cpu
}
}
}
$i++
}
writeTablefooter $TomcatMemFileName
writeHtmlFooter $TomcatMemFileName
Thanks,
Kent
Upvotes: 0
Views: 1900
Reputation: 36297
The reason that your CPU portion is not working is that you are piping it to Format-Table (you use the FT alias). Go to the end of your $cpuse =
line and remove the | ft -AutoSize
. Once you do that it should work as you expect it to. I would also suggest moving the process filtering to the counter as Greg did.
$cpuse= Get-Counter -computername $machine '\Process(tomcat*)\% Processor Time' | Select-Object -ExpandProperty countersamples | Select-Object -Property cookedvalue
Upvotes: 2
Reputation: 772
I'm going to assume you have the rights to read the counter information if the rest of the script works...
Why not specify the process name in the counter that way you don't have to mess with all the filtering later. For example:
$counter = Get-Counter '\Process(tomcat*)\% Processor Time' -ComputerName $myServer
$counter.CounterSamples.CookedValue
If there is more than one tomcat process you'll have to take apart the CounterSamples collection but it should work just fine.
Upvotes: 1