Powershell: error handling with try and catch
Posted
by
resolver101
on Stack Overflow
See other posts from Stack Overflow
or by resolver101
Published on 2012-04-07T18:37:56Z
Indexed on
2012/04/08
23:29 UTC
Read the original article
Hit count: 296
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
© Stack Overflow or respective owner