【问题标题】:PowerShell : error handling when using -asjob parameterPowerShell:使用 -asjob 参数时的错误处理
【发布时间】:2014-07-27 10:55:00
【问题描述】:

我不知道如何获取使用-asjob 时没有响应的计算机名,有人可以建议吗?

try{
    gwmi "Win32_OperatingSystem" -ComputerName $ordis -asjob  
    $resu=get-job | ? {$_.psjobtypename -eq "wmijob" } |wait-job |receive-job
}
catch{"error"}
$resu | select PSCOMPUTERNAME, @{name="lastboottime";expression={$_.converttodatetime($_.lastbootuptime)}} |sort lastboottime |ft
remove-job * -force

一些主机因以下错误而失败,但我不知道哪些主机

Le serverur RPC n'est pas disponible。 (HRESULT 例外: 0x800706BA)
+ CategoryInfo : InvalidResult : (:) [], COMException
+ FullyQualifiedErrorId : JobStateFailed

【问题讨论】:

  • 对于此类脚本,我使用带有计算机名称的 CSV 文件。 Import-CSV 产生一个要迭代的集合。我将查询结果保存在另一个集合中,然后更新 CSV 字段(对于响应的主机)并使用 Export-CSV 保存。
  • @AlexanderObersht 你没有像这样享受并行处理的好处
  • 我对查询使用并行处理。处理结果所需的时间可以忽略不计。如果您有兴趣,我可以发布我的代码。

标签: powershell powershell-4.0 dsc


【解决方案1】:

$resu | select.. 语句之后添加另一行以捕获失败的作业,try/catch 将不起作用,因为您将 wmi 查询生成到单独的运行空间中。

....
Get-Job -State Failed | Select-Object -ExpandProperty Location
remove-job * -force

【讨论】:

  • 我不得不遍历 childjobs 来获取信息:get-job -state failed |select -expand childjobs | ?{$_.state -eq "failed"} |select -expand location
猜你喜欢
  • 2014-05-31
  • 2021-01-15
  • 2022-01-04
  • 2019-08-11
  • 1970-01-01
  • 1970-01-01
  • 2011-10-25
  • 2013-05-30
  • 1970-01-01
相关资源
最近更新 更多