Error handling - PowerShell script


I have the following powershell script that cycles through a list of hostnames and changes the DNS settings for the active interfaces:

$servers = Get-Content C:\users\kevin.todd\desktop\serverlist.txt 

foreach($server in $servers)


    Write-Host "Connect to $server..."

    $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}

    $newDNS = "",""

    foreach($nic in $nics)


        Write-Host "`tExisting DNS Servers " $nic.DNSServerSearchOrder

        $x = $nic.SetDNSServerSearchOrder($newDNS)

        if($x.ReturnValue -eq 0)


            Write-Host "`tSuccessfully Changed DNS Servers on " $server




            Write-Host "`tFailed to Change DNS Servers on " $server




The problem is on some hosts I get the following error:

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At C:\Documents and Settings\user1\desktop\changednsserver.ps1:20 char:26 + $nics = Get-WmiObject <<<< Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{ $_.IPEnabled -eq "TRUE"} + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
Existing DNS Servers You cannot call a method on a null-valued expression. At C:\Documents and Settings\user1\desktop\changednsserver.ps1:30 char:42 + $x = $nic.SetDNSServerSearchOrder <<<< ($newDNS) + CategoryInfo : InvalidOperation: (SetDNSServerSearchOrder:String) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull

And I'm asked the following question by Powershell:

The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) [Y] Yes [A] Yes to All [H] Halt Command [S] Suspend [?] Help (default is "Y"):

I would like the script to just answer A - Yes to all and continue running the script. The problem is it just halts the script until I manually enter "A". How can I have it automatically answer and continue?

asked on Stack Overflow Aug 11, 2011 by UncleKevo

2 Answers


Well the short answer is to not tell it to stop in the first place:

$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"}


$nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction SilentlyContinue | Where{$_.IPEnabled -eq "TRUE"}

answered on Stack Overflow Aug 11, 2011 by EBGreen

If you need to see where Errors occur, you have also a possibility to define an error variable using the switch -ErrorVariable $<variable>

If errors occur in a foreach loop, the Error variable will be an array of error messages you can later analyze by querying the defined error variable with an array index echo $<EVariable>[<index>]

it would be something like echo $MyErrorArray[0] to get the first occurred error in that array.

An elegant way would be to have all that added to a textbox/combobox for log purposes but the coice of error handling ananalyzing is yours.

answered on Stack Overflow Feb 20, 2018 by Netspark

User contributions licensed under CC BY-SA 3.0