【发布时间】:2017-02-23 05:20:46
【问题描述】:
我正在尝试制作一个脚本,将文本文件中的资产标签列表与 AD 中的计算机名称进行比较,并生成描述。稍后将其导出为 CSV。到目前为止,虽然代码确实有效,但它会给出以下错误消息。我们在 AD 中的计算机以 L 或 D 开头,说明它是笔记本电脑还是台式机,但我们收到的列表中不包含 L 或 D,这就是为什么你看到我把“L”+“D”在前面。有更好的方法吗?
代码:
Import-Module ActiveDirectory
foreach ($line in Get-Content ComputerNames.txt) {
if($line -match $regex) {
$laptop = "L" + $line
$desktop = "D" + $line
get-ADComputer $laptop -Properties * |select Description
#get-ADComputer $desktop -Properties * |select Description -ErrorAction Ignore }
}
错误:
get-ADComputer : Cannot find an object with identity: 'LD7MWQ12' under: 'DC=ap,DC=o-i,DC=intra'.
At line:9 char:9
+ get-ADComputer $laptop -Properties * |select Description
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (LD7MWQ12:ADComputer) [Get-ADComputer], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'LD7MWQ12' under: 'DC=ap,DC=o-i,DC=intra'.,Microsoft.ActiveDirectory.Management.Com
mands.GetADComputer
【问题讨论】:
-
如果您手动检查该对象是否存在,那根本就是找不到对象“LD7MWQ12”?
-
不,它不存在,因为资产 D7MWQ12 实际上以 D(全名 DD7MWQ12)开头。有没有办法忽略错误消息,或者完全绕过它并搜索以字母 D 或 L 开头的资产?
-
要绕过错误,您可以使用stackoverflow.com/questions/8388650/…
标签: powershell