我正在编写一个脚本并想控制错误。但是,我无法使用 try,catch 查找有关错误处理的信息。我想捕获特定错误(如下所示),然后执行一些操作并恢复代码。这需要什么代码?
这是我正在运行的代码,我在提示时输入了无效的用户名。
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