0

在这里转了一圈......我正在尝试处理脚本中的“致命错误”并继续。我知道这-EA silentlycontinue不起作用,但会继续使用foreach来解决它,但我发现的解决方案对我不起作用,这是一个例子,而不是我想要做的......

编码:

get-content serverLists.txt | 
foreach {get-wmiobject -computer $_ -query "Select * from win32_logicaldisk where drivetype=3"} | 
Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName

死于:

get-wmiobject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
At line:1 char:40
+ get-content serverLists.txt | foreach {get-wmiobject -computer $_ -query "Select ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-WmiObject], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

我希望它继续运行,我已经尝试并调查了 try/catch,这只是以可读格式给出错误并停止,我查看了 ping/端口检查解决方案,但有些服务器位于防火墙后面,但某些端口是开放的等我只是希望它处理致命错误并继续......

顺便说一句,这不是一个权利问题,它会通过一大堆服务器,然后脚本会死在一个并停止

4

1 回答 1

2

try..catch应该做你想做的事:

Get-Content serverLists.txt | % {
  try {
    gwmi -Computer $_ -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
  } catch {}
} | Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName

或者你可以改变$ErrorActionPreference

$ErrorActionPreference = "SilentlyContinue"
Get-Content serverLists.txt | % {
  gwmi -Computer $_ -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3"
} | Format-Table SystemName,DeviceID,Size,FreeSpace,VolumeName
于 2013-09-17T09:58:13.500 回答