Cisiur
Cisiur

Reputation: 57

Check few AD computers name by it's part

I'm trying to find full AD computer name based on it's last 7 digits (names are set as 4 digits + DELL service tag) I'm using below command to find 1 computer and it works fine but i have to find over 200 so im wondering if it's possible to do it for all servie tags from .txt file? And then export results to csv or Grid view?

Get-ADComputer -Filter 'Name -like "*93PXPV2"' -Properties * | FT Name

Upvotes: 0

Views: 31

Answers (2)

jdweng
jdweng

Reputation: 34421

Try following :

$input_filename = "c:\temp\test.txt"
$shortNames = Get-Content -Path $input_filename
$shortNames | Format-Table

$table = [System.Collections.ArrayList]::new()
foreach($shortName in $shortNames)
{
   $result = Get-ADComputer -Filter Name -like ("*" + $shortName) -Properties *
   foreach($computer in $result)
   {
      $table.Add($computer)  | Out-Null
   }
}
$table | Format-Table

Upvotes: 0

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174485

You could construct an LDAP filter that searches for all name suffixes at once, eg.:

(|(name=*93PXPV2)(name=*17GEJK8)(name=*...))

Assuming you have the suffixes stored in a txt file, one on each line, you could do something like this:

# Read name suffix information from disk
$suffixes = Get-Content path\to\computerNames.txt

# Construct LDAPFilter
$LDAPFilter = '(|{0})' -f -join($suffixes |ForEach-Object { "(name=*${_})" })

# Search for, and export, all the names at once!
Get-ADComputer -LDAPFilter $LDAPFilter |Select Name,Enabled,DistinguishedName |Export-Csv path\to\output.csv -NoTypeInformation

Upvotes: 1

Related Questions