Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am getting this error.

Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At F:\Code\powershell\network_shutdown\TurnNetworkOff.ps1:19 char:26
+             Get-WmiObject <<<<  -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId
=0" | % {
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Here is my code

#Define variables used in this script
$namespace = "root\WMI"

$array = @()
$exceptionarray = @()

$computerlist = Get-Content F:\Code\powershell\network_shutdown\computer-list.csv

foreach ($computer in $computerlist) 
{
    # Main Processing Section
    # Write-Host $computer
    if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet))
    {
        Try
        {
            #Write-Host $computer
            #Write-Host "Disable `"Allow the computer to turn off this device to save power`""
            Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
                $strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
                Get-WmiObject -class MSPower_DeviceEnable -computername $computer -Namespace $namespace | % {
                    if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID))
                    {
                        $_.Enable = $false
                        $_.Put() | Out-Null
                    }
                }
            }

            #Write-Host "Disable `"Allow this device to wake the computer`""
            Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
                $strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
                Get-WmiObject -class MSPower_DeviceWakeEnable -computername $computer -Namespace $namespace | % {
                    if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID)){
                        $_.Enable = $false
                        $_.Put() | Out-Null
                    }
                }
            }

            #Write-Host "Disable `"Only allow a magic packet to wake the computer`""
            Get-WmiObject -computername $computer Win32_NetworkAdapter -filter "AdapterTypeId=0" | % {
                $strNetworkAdapterID=$_.PNPDeviceID.ToUpper()
                Get-WmiObject -class MSNdis_DeviceWakeOnMagicPacketOnly -computername $computer -Namespace $namespace | % {
                    if($_.InstanceName.ToUpper().startsWith($strNetworkAdapterID)){
                        $_.EnableWakeOnMagicPacketOnly = $false
                        $_.Put() | Out-Null
                    }
                }
            }
        } Catch [Exception]
        {
            Write-Host $computer + " WMIC ERROR"
            if ($_.Exception.GetType().Name -eq "COMException") {
                Write-Host $computer + " WMIC ERROR"
            }

            $output = new-object psobject
            $output | Add-Member noteproperty PCTag $computer

            [PSObject[]]$exceptionarray += $output
        }        
    } else {
        Write-Host $computer + " OFFLINE"
        $output = new-object psobject
        $output | Add-Member noteproperty PCTag $computer

        [PSObject[]]$array += $output
    }

    $array | Export-Csv -Path F:\Code\powershell\network_shutdown\ResultsOffline.csv
    $exceptionarray | Export-Csv -Path F:\Code\powershell\network_shutdown\ResultsException.csv
}
share|improve this question
    
@vjones I saw that. Isn't that what Im doing in my code? The only difference is that my Exceptions aren't being caught. This could be a possible coding error, syntax error or logic error. I am unable to find the issue. That's why I posted it here –  software is fun Aug 5 '14 at 17:14

1 Answer 1

up vote 7 down vote accepted

The GetWMICOMException is a non-terminating error, meaning that with a default $ErrorActionPreference of Continue, the code in the try block is going to continue its execution after writing out the exception as an error

Enclose the Get-WmiObject call with a try-catch block, but make sure that the -ErrorAction is set to Stop:

# Try-Catch block starts
try 
{
    # Call Get-WmiObject
    Get-WmiObject -ComputerName $computer -Class "Win32_NetworkAdapter" -ErrorAction Stop
}
# If an Exception of the type COMException is thrown, execute this block
catch [System.Runtime.InteropServices.COMException]
{
    # You can inspect the error code to see what specific error we're dealing with 
    if($_.Exception.ErrorCode -eq 0x800706BA)
    {
        # This is instead of the "RPC Server Unavailable" error
        Write-Error -Message "Your own custom message" 
    }
    else
    {
        Write-Error -Message "Some other COMException was thrown"
    }
}
# If any other type of Exception is thrown, execute this block
catch [System.Exception]
{
    Write-Error -Message "Some other exception that's nothing like the above examples"
}
# When all of the above has executed, this block will execute
finally
{
    Write-Verbose "Get-WmiObject object was executed"
}

Alternatively, you could set the $ErrorActionPreference to Stop before executing your script:

# Before the rest of the script
$ErrorActionPreference = Stop

For more help about the try-catch-finally construction:

Get-Help about_Try_Catch_Finally -Full

For more help about the $*Preference vars:

Get-Help about_Preference_variables -Full
share|improve this answer
1  
Just to add another resource on Powershell error handling, check out the eBook available on powershell.org. They have a small library of useful books written by key community members and they can be a good place to jump off from. powershell.org/wp/ebooks –  user24313 Aug 11 '14 at 16:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.