In powershell, I can catch Access is Denied error using Catch [System.UnauthorizedAccessException]. How do I similarly catch RPC Server Unavailable error?
Asked
Active
Viewed 2.1k times
3
-
I think this is the error with more details: `Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA) At C:\Users\flickerfly\Documents\scripts\Set-LocalServerAdmin.ps1:22 char:33 + $oldexists = Get-WmiObject <<<< Win32_UserAccount -Filter "Name='$olduser'" -ComputerName $computerName + CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand` – Josiah Nov 11 '12 at 00:46
2 Answers
5
If you add the common parameter -ErrorAction Stop to the, in my case, get-wmiobject command it will cause the command to respond to this non-terminating error as a terminating error and drop it to catch for action.
Here is the code I'm using for this purpose. I probably should be more specific in the catch, but it works for now.
# Is this machine on network?, if not, move to next machine
If (!(Test-Connection -ComputerName $computerName -Count 1 -Quiet)) {
Write-Host "$computerName not on network."
Continue # Move to next computer
}
# Does the local Administrator account exist? Returns a string if it exists, which is true-ish.
try {
$filter = "Name='$olduser' AND Domain='$computerName'"
$account = Get-WmiObject Win32_UserAccount -Filter $filter -ComputerName $computerName -ErrorAction Stop
} catch {
Write-Warning "$computerName Can't check for accounts, likely RPC server unavailable"
Continue # Move to next computer
} #end try

Josiah
- 2,666
- 5
- 30
- 40
-
What do you write in $olduser variable? Loacl administrator user name? – Bomberlt Sep 03 '14 at 07:56
-
The script this was a part of was changing usernames for admin accounts I believe. $olduser was the name of the user account I was changing to another username. So $olduser would have probably been "Administrator". $account would then be the "Administrator" account object if one existed. If not, it would then not include a user object and clearly not needing any attention. (It's been awhile, but I think that's how it worked.) – Josiah Sep 03 '14 at 14:23
-
1It seems the RPC Server Unavailable error does not get caught by default, hence the OP's question, perhaps (or, *my* issue :) But adding -ErrorAction Stop to my GWMI does the trick, thank you. – Shoeless Feb 27 '15 at 14:48
4
You can catch every exception you want. Just write:
$_.Exception.GetType()
inside your catch to see what exception is there and then catch it.

Andrey Marchuk
- 13,301
- 2
- 36
- 52