PowerShell script to test remote connections to a server

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.

powershell
connection
rpc
connectivity
asked on Stack Overflow Dec 18, 2015 by Rossb2 • edited Dec 20, 2015 by Booga Roo

1 Answer

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
         }
                                }
answered on Stack Overflow Dec 18, 2015 by bentek

User contributions licensed under CC BY-SA 3.0