Powershell try catch

Soldato
Joined
8 Mar 2005
Posts
3,852
Location
London, UK
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.
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." }
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.
 
Not sure, you can check the error array maybe? Sticks the most recent to the front so if $error[0] has something in it then it's seen an error.
 
Give this a bash, test the connection and then capture:
Code:
Try {
    Test-Connection -ComputerName $hostname -Count 1 -ErrorAction Stop
    Try {
            
        $CpuUsage = (Get-Counter -ComputerName $hostname -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 -ErrorAction Stop |
                Select-Object -ExpandProperty countersamples | Select-Object -ExpandProperty cookedvalue | Measure-Object -Average).average
        $CpuUsage = "{0:N1}" -f $CpuUsage; return $CpuUsage
    }
    Catch { Write-Verbose  "Error returned while checking the CPU usage. Perfmon Counters may be at fault." }

}
Catch [Exception] {
    $_.Exception
}
 
And make it re-usable:

Code:
Function Get-CPUAverage ($hostname) {
    Try {
        Test-Connection -ComputerName $hostname -Count 1 -ErrorAction Stop
        Try {
            
            $CpuUsage = (Get-Counter -ComputerName $hostname -Counter "\Processor(_Total)\% Processor Time" -SampleInterval 1 -MaxSamples 5 -ErrorAction Stop |
                    Select-Object -ExpandProperty countersamples | Select-Object -ExpandProperty cookedvalue | Measure-Object -Average).average
            $CpuUsage = "{0:N1}" -f $CpuUsage; return $CpuUsage
        }
        Catch { Write-Verbose  "Error returned while checking the CPU usage. Perfmon Counters may be at fault." }

    }
    Catch [Exception] {
        $_.Exception
    }
}

Get-CPUAverage $hostname
 
Hmmm, Thanks for giving it a go. However, exactly the same behaviour; it just sits there indefinitely. As I say the server is pingable but seemingly not allowing certain calls to it (I cannot RDP or pull up a remote tasklist) but I can browse to it, connect to its services etc.
 
Odd! Have you tried running powershell as administrator? Can you get direct access to the box? Doesn't sound healthy but why your script isn't bombing out is more of a mystery.

Could try sticking it in a scriptblock and seeing if that gets through? Or you said WMI calls not working, do these return an error? If they do then worst case could use one of these calls as a pseudo test connection just so your script can crack on with the rest of the computers in your list.
 
So, a little further along now. The server is basically pegged at 100% CPU. Although you can browse and interact in various ways; any attempt to connect to WMI counters or directly attempting to RDP fails. So I think my original assumption holds true that the command executes successfully. it just never completes.

I've managed to get around the issue by placing this and other similar code inside another function which does successfully process based on another condition.
 
Back
Top Bottom