Help to Avoid WMI Delays

by n3rdbiker at 2012-09-13 12:13:46

I am looking for a way to keep on trucking if I try to do a WMI query on a system and get a RPC Server Unavailable – I have no interest in resolving the issue, I would like to test the wmi connection and if it’s not going to cooperate move on down the road to the next system. If it works, I have a list of WMI calls to make. I am already planning on skipping systems that I can’t talk to at all - so it’s primarily ones that are currently throwing the RPC Server Unavailable Exception.


by DonJ at 2012-09-13 12:19:39
The only real way to test it is with a WMI call… so the trick is to just trap the error.

$it_worked = $true
try {
get-wmiobject -class win32_BIOS -computername $whatever -erroraction stop
} catch {
$it_worked = $false
if ($it_worked) {
# rest of code goes here

Kinda like that. This won’t get rid of the timeout involved in WMI figuring out it won’t work, but there’s not really a great way to avoid that timeout, either. You could precede this with a ping (Test-Connection), I suppose, but even if that works there’s no guarantee WMI will.
by RichardSiddaway at 2012-09-13 12:37:15
Test-Connection uses the WMI class Win32_PingStatus so if you get a response from test-connection you should be OK

One option is
$comp = $env:COMPUTERNAME
if (Test-Connection -ComputerName $comp -Count 1 -Quiet ) {
< do lots of stuff >
else {
< it didn’t work>

Use the -Quiet means to get a Boolean (true/false) returned so works if very nicely
by DonJ at 2012-09-13 13:09:43
Actually, Win32_PingStatus just sends an ICMP ping - it doesn’t actually test connectivity to the WMI RPC endpoint. A ping might work, but RPC wouldn’t, and vice-versa, depending on the systems’ configuration.

But if you’re sure that your target systems will always allow a ping if they’re online, then it’s a good first check. I have a lot of servers whose firewalls won’t permit a ping, but they will permit an RPC, so Test-Connection will fail when Get-WmiObject won’t.