我正在尝试创建一个脚本,将文本文件中的资产标记列表与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
答案 0 :(得分:1)
可能是一种更有效的方法,但下面有效:
Import-Module ActiveDirectory
foreach ($line in Get-Content ComputerNames.txt) {
Get-ADComputer -Filter * -Property Description | Where {$_.samaccountname -Like "*$line"} | select Description
}
对于computernames.txt
对象中的每一行,它将找到与$line
变量类似的AD对象,然后选择该对象的描述
答案 1 :(得分:0)
慢速位将是AD的网络链接,如果可能的话,你真的只想做一次。除非您在AD中拥有大量计算机,否则最好下拉所有计算机,然后将它们与文本文件进行本地比较。
此外,如果您从AD中提取信息,不要带来超出您需要的信息,则会浪费网络流量和内存开销,因此只需添加说明
Import-Module ActiveDirectory
# AD query which will get all computers with names starting D or L
$ADFilter = "Name -like 'D*' -or Name -like 'L*'"
$ADComputers = Get-ADComputer -filter $ADFilter -Properties Description | Select Name, Description
$NamesFromFile = Get-Content ComputerNames.Txt
# Filter the AD Computers where the name without the first character is
# mentioned in the file
$ADComputers | Where-Object { $_.Name.SubString(1) -in $NamesFromFile } | Export-Csv -NoTypeInformation out.csv