Sea*_*mIT 3 powershell powershell-remoting
在powershell中,我可以使用Catch [System.UnauthorizedAccessException]捕获“ Access is Denied”错误。如何类似地捕获RPC服务器不可用错误?
如果在我的情况下,将通用参数-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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16081 次 |
| 最近记录: |