Search powershell by CSV for Computer name

≡放荡痞女 提交于 2019-12-25 18:35:11

问题


Ive been tasked with the issue of searching a CSV file that has over 1000 computer names in it. I was wondering if there was a way to search AD with Get-ADComputer by the CSV file for the computer names and return the output if they exist in active directory. So far I haven't been able to come up with anything except of doing it manually.


回答1:


You can foreach through the computer names from the csv, but that could take a while for 1000 computers.

An alternative is to build an LDAP filter from the computernames in the csv, and do it on one operation:

$ComputerNames = Import-Csv c:\somedir\computers.csv |
 select -ExpandProperty ComputerName

$NameFilters = $ComputerNames -replace '^','(Name=' -replace '$',')'
$Filter = "'(|$NameFilters)'"

Get-ADComputer -LDAPFilter $Filter



回答2:


Import-CSV MyCSV.CSV | Select-Object *,@{n='InAD';e={$n = $_.Name ; IF (Get-ADComputer -filter {Name -eq $n}) {$True} ELSE {$False}}} | Export-CSV MyNewCSV.CSV

On a mega-line. Replace "Name" in "$_.Name" with whatever column header contains the computer name.

(edited to add clarification and full cmdlet name).



来源:https://stackoverflow.com/questions/25789067/search-powershell-by-csv-for-computer-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!