【发布时间】:2015-08-18 22:09:47
【问题描述】:
我正在使用this script 检索计算机列表的 OU。当计算机不存在时,我得到一个错误:
Cannot index into a null array.
At C:\tools\scripts\get-OU5.ps1:35 char:5
+ $dn = $result.Properties["distinguishedName"]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : NullArray
当我手动搜索域中的计算机时,它不存在。此外,在发生错误后,它会列出它从最后一台计算机接收到的先前 OU 值,作为给出错误的这台计算机的结果。
我知道 PowerShell 中有错误处理能力,但我只是不确定将错误处理放在哪里,然后将其作为结果报告到输出中。
【问题讨论】:
-
如果你有 ActiveDirectory 模块,你可以在一行中完成所有这些。获取-ADComputer -Filter * |选择专有名称。如果你想编辑他的脚本,很可能 $result 在第 35 行是 $null。所以尝试更改为 if ((!$?) -or (-not $result) { return }
标签: powershell active-directory