我如何在下面的脚本中添加一个捕获,以便它忽略不在线的机器?
目前,当机器不在线时,我收到以下错误消息:
Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At C:\fcheck\run.ps1:6 char:9
+ gwmi <<<< win32_operatingsystem -computername $hostname | ForEach-Object {
+ CategoryInfo : InvalidOperation: (:) [Get-WmiObject], COMException
+ FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
到目前为止,这是我的代码:
$dict.Keys | foreach-object {
$currKey = $_
$dict[$_] | ForEach-Object {
$PSObject = New-Object PSCustomObject | Select hostname, os, type
$PSObject.hostname = $_
$PSObject.os = Get-OS $_
$PSObject.type = $currType
$VMObjects += $PSObject
}
}
$VMObjects | ft