Reputation: 43
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
I am using PowerShell 2 / 4 for the scripts.
Upvotes: 1
Views: 4289
Reputation: 245
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
}
}
Upvotes: 1