捕获 RPC 服务器不可用错误 HRESULT:0x800706BA

发布于 2024-12-15 00:05:03 字数 118 浏览 1 评论 0原文

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

In powershell, I can catch Access is Denied error using Catch [System.UnauthorizedAccessException]. How do I similarly catch RPC Server Unavailable error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只等公子 2024-12-22 00:05:03

如果将公共参数 -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

If you add the common parameter -ErrorAction Stop to the, in my case, get-wmiobject command it will cause the command to respond to this non-terminating error as a terminating error and drop it to catch for action.

Here is the code I'm using for this purpose. I probably should be more specific in the catch, but it works for now.

# 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
沦落红尘 2024-12-22 00:05:03

您可以捕获您想要的每个异常。只需在 catch 中写入:

$_.Exception.GetType()

即可查看存在什么异常,然后捕获它。

You can catch every exception you want. Just write:

$_.Exception.GetType()

inside your catch to see what exception is there and then catch it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文