在 powershell 中,我可以使用 Catch [System.UnauthorizedAccessException] 捕获访问被拒绝错误。我如何类似地捕获 RPC Server Unavailable 错误?

最佳答案

如果将通用参数 -ErrorAction Stop 添加到 get-wmiobject 命令,在我的情况下,它将导致该命令将此非终止错误作为终止错误进行响应,并将其丢弃以进行操作。

这是我为此目的使用的代码。我可能应该在捕获中更具体,但它现在有效。

# 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

关于powershell - 捕获 RPC 服务器不可用错误 HRESULT : 0x800706BA,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8086357/

10-11 08:41