我想製作一個腳本,將文本文件中的資產標籤列表與AD中的計算機名稱進行比較,並生成說明。將其導出爲CSV將在稍後進行。儘管如此,雖然代碼確實有效,但它會提供以下錯誤消息。我們在AD中的計算機以L或D開頭,表明它是筆記本電腦還是桌面計算機,但我們收到的列表中不包含L或D,這就是爲什麼您看到我將「L」+「D」在前面。有沒有更好的方法來做到這一點?PowerShell活動目錄與文本文件比較
代碼:
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開頭的資產? – ilovetaufu
繞過錯誤,你可以使用http://stackoverflow.com/questions/8388650/powershell-how-can-i-stop-errors-from-being-displayed-in-a-script –