【发布时间】:2017-09-05 00:11:05
【问题描述】:
我有一个包含计算机列表的文件,我需要遍历该列表并报告其中是否有空闲。
$list = get-content "pathtofile.txt"
foreach ($computer in $list) {
try {
quser /server:$computer
} catch [System.Management.Automation.RemoteException] {
Write-Host "$computer is free"
}
}
现在它可以工作了,但我希望捕获错误消息并将其更改为混乱的计算机名称是免费的。
目前它仍在返回
quser : 没有用户存在 * 在行:5 字符:5 + quser /服务器:$计算机 + ~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (No User exists for *:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError对于免费的计算机。
我能够通过在我知道是免费的计算机上运行quser 命令然后运行$Error[0] | fl * -Force 来获得$Error[0] | fl * -Force:
这给了我异常代码。
现在我确实查看了Foreach error handling in Powershell,这表明我的代码应该是正确的,所以不确定为什么 catch 不起作用。
【问题讨论】:
标签: powershell