Soldato
So, the following code isn't doing what I think it should when failing. Infact as far as I can tell it it doesn't see a failure.
In this example, it will attempt to get an average of CPU based on 5 samples from a host. Occasionally, the host may be up(pingable) but it is in a state where you can neither connect via RDP or interact with WMI. I suspect the try doesn't see the get-counter attempt as a failure and it simply waits indefinitely for the 5 samples to be collected.
Any ideas?
Cheers, Paul.
Code:
Try { $CpuUsage=(get-counter -ComputerName $hostname -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 -ErrorAction Stop | select -ExpandProperty countersamples | select -ExpandProperty cookedvalue | Measure-Object -Average).average
$CpuUsage = "{0:N1}" -f $CpuUsage; return $CpuUsage
} Catch { "Error returned while checking the CPU usage. Perfmon Counters may be at fault." }
Any ideas?
Cheers, Paul.