Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 捕获RPC服务器不可用错误HRESULT:0x800706BA_Powershell_Powershell Remoting - Fatal编程技术网

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

Powershell 捕获RPC服务器不可用错误HRESULT:0x800706BA,powershell,powershell-remoting,Powershell,Powershell Remoting,在powershell中,我可以使用catch[System.UnauthorizedAccessException]捕获拒绝访问错误。如何捕获RPC服务器不可用错误?您可以捕获所需的所有异常。只要写下: $_.Exception.GetType() 在catch中查看有什么异常,然后捕获它。如果将公共参数-ErrorAction Stop添加到get-wmiobject命令中(在我的例子中是get-wmiobject命令),它将导致该命令将此非终止错误作为终止错误响应,并将其丢弃以捕获以执行

在powershell中,我可以使用catch[System.UnauthorizedAccessException]捕获拒绝访问错误。如何捕获RPC服务器不可用错误?

您可以捕获所需的所有异常。只要写下:

$_.Exception.GetType()

在catch中查看有什么异常,然后捕获它。

如果将公共参数-ErrorAction Stop添加到get-wmiobject命令中(在我的例子中是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

我认为这是一个更详细的错误:
Get WmiObject:RPC服务器不可用。(来自HRESULT:0x800706BA的异常)在C:\Users\flicklyFly\Documents\scripts\Set LocalServerAdmin.ps1:22 char:33+$oldexists=Get WmiObject在$olduser变量中写了什么?Loacl管理员用户名?这是脚本的一部分,我相信它正在更改管理员帐户的用户名$olduser是我正在更改为其他用户名的用户帐户的名称。所以$olduser可能是“管理员”$帐户将成为“管理员”帐户对象(如果存在)。如果没有,它将不包括用户对象,并且显然不需要任何注意。(这已经有一段时间了,但我认为这就是它的工作原理。)似乎RPC服务器不可用错误在默认情况下不会被捕获,因此OP的问题可能是(或者,我的问题是:)但是在我的GWMI中添加-ErrorAction Stop就可以了,谢谢。