问题
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