3

I'm writing a script and want to control the errors. However im having trouble finding information on error handling using the try, catch. I want to catch the specific error (shown below) and then perform some actions and resume the code. What code is needed for this?

This is the code i am running and im entering in a invalid username when prompted.

Get-WMIObject Win32_Service -ComputerName localhost -Credential (Get-Credential)



Get-WmiObject : User credentials cannot be used for local connections 
At C:\Users\alex.kelly\AppData\Local\Temp\a3f819b4-4321-4743-acb5-0183dff88462.ps1:2 char:16
+         Get-WMIObject <<<<  Win32_Service -ComputerName localhost -Credential (Get-Credential)
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], ManagementException
    + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

2 Answers 2

2

Can anyone figure out why I can't trap this exception when trying to trap exceptions of type [System.Management.ManagementException]?

PowerShell should be able to trap exceptions that match certain exception classes, but even though the exception class for below is [System.Management.ManagementException] it won't catch it in that catch block!

i.e:

Try
{
    Get-WMIObject Win32_Service -ComputerName localhost -Credential (Get-Credential) -ErrorAction "Stop"
}
Catch [System.Management.ManagementException]
{
    Write-Host "System.Management.ManagementException"
    Write-Host $_
    $_ | Select *
}
Catch [Exception]
{
    Write-Host "Generic Exception"
    Write-Host $_
    $_ | Select *
}

Works the same as:

Try
{
    Get-WMIObject Win32_Service -ComputerName localhost -Credential (Get-Credential) -ErrorAction "Stop"
}
Catch [Exception]
{
    Write-Host "Generic Exception"
    Write-Host $_
    $_ | Select *
}

Doesn't make sense to me.

You could also catch the error in the Generic Exception catch block and then check the text to see if it matches the words you are after, but it is a bit dirty.

Sign up to request clarification or add additional context in comments.

Comments

1

You must use -erroraction stop to enter into the try/catch or trap scriptblock. You can test this :

Clear-Host
$blGoOn = $true

while ($blGoOn)
{
  trap
  {
    Write-Host $_.exception.message
    continue
  }
  Get-WMIObject Win32_Service -ComputerName $computer -Credential (Get-Credential) -ErrorAction Stop
  if ($?)
  {
    $blGoOn=$false
  }
}

2 Comments

Thanks for the swift reply. How do I trap for error message "User credentials cannot be used for local connections" in particular? The other errors wanted to be processed with different code. thanks
You're right : "User credentials cannot be used for local connections"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.