【问题标题】:Catch RPC Server Unavailable Error HRESULT: 0x800706BA捕获 RPC 服务器不可用错误 HRESULT: 0x800706BA
【发布时间】:2012-11-11 09:20:08
【问题描述】:

在 powershell 中,我可以使用 Catch [System.UnauthorizedAccessException] 捕获 Access is Denied 错误。我如何同样捕获 RPC Server Unavailable 错误?

【问题讨论】:

  • 我认为这是更多细节的错误: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

标签: powershell powershell-remoting


【解决方案1】:

如果您将通用参数 -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

【讨论】:

  • 你在 $olduser 变量中写了什么? Loacl 管理员用户名?
  • 我相信这是其中一部分的脚本正在更改管理员帐户的用户名。 $olduser 是我要更改为另一个用户名的用户帐户的名称。所以 $olduser 可能是“管理员”。 $account 然后将是“管理员”帐户对象(如果存在)。如果不是,那么它就不会包含用户对象并且显然不需要任何关注。 (已经有一段时间了,但我认为它就是这样工作的。)
  • 默认情况下似乎没有捕获 RPC Server Unavailable 错误,因此可能是 OP 的问题(或者,my 问题:) 但是将 -ErrorAction Stop 添加到我的 GWMI成功了,谢谢。
【解决方案2】:

你可以捕获你想要的每一个异常。随便写:

$_.Exception.GetType()

在你的catch里面看看有什么异常,然后捕捉它。

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2013-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多