1

I am new to PowerShell. An issue I am having is, when I run scripts against hundreds of servers, some of those servers are showing RPC unavailable in the PowerShell command line.

e.g if I run the script:

$list = Get-Content "C:\Users\hostnames.txt" foreach ($computer in $list) { Try { gwmi win32_networkadapterconfiguration -computername $computer -filter "ipenabled = 'true'" | select dnshostname,ipaddress,defaultipgateway,dnsserversearchorder,winsprimaryserver,winssecondaryserver | ft -property * -autosize | out-string -Width 4096 >>dnschgchecks.txt } Catch { "$computer.name failed" >>dnschgchecks.txt } } 

Some of the hosts report the following in the command line:

gwmi : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At C:\Users\dnschgchecks.ps1:4 char:1 + gwmi win32_networkadapterconfiguration -computername $computer -filter "ipenable ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

The issue is, I cannot tell which hosts failed to complete the gwmi command out of hundreds of hosts. I don't want to have to check the logs for missing entries to figure out which ones. So, how can I tell which hosts failed? I guess my options are

  • Modify the script somehow?
  • Use another script to test PowerShell connectivity first, but how?

I am using PowerShell 2 / 4 for the scripts.

1 Answer 1

1

After your main gwmi command, add -erroraction stop to force a terminating error which will trigger your catch{} block.

You may also want to run test-connection against your server first, and if it succeeds, proceed to WMI command.

$list = Get-Content "C:\Users\hostnames.txt" foreach ($computer in $list) { Try { gwmi win32_networkadapterconfiguration -computername $computer -filter "ipenabled = 'true'" -erroraction stop | select dnshostname,ipaddress,defaultipgateway,dnsserversearchorder,winsprimaryserver,winssecondaryserver | ft -property * -autosize | out-string -Width 4096 >>dnschgchecks.txt } Catch { "$computer.name failed" >>dnschgchecks.txt } } 
Sign up to request clarification or add additional context in comments.

2 Comments

excellent, thank you. I can now easily tell which hosts failed in the log file, plus the failure is no longer reported as an error on the command line. The catch block triggers as it should.
great hint regarding the Test-Connection method. Sometimes the Get-WmiObject calls take half a minute (or even longer) to fail because of a simple connectivity issue so running a ping test first really speeds up the process

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.