none
Powershell Get-AdComputer - How to search for computer names when you only know part of the name RRS feed

  • Question

  • Hi Guys,

    I need to search AD using a list of strings, each string is a portion of the computer name.

    I was able to get this to work fine using part of the actual computer name, it finds the computer information.

    Get-ADComputer-Filter{ name  -like"*hf38fhed*"} -Properties*|Format-TableName-AutoSize

    When I use a list in a file, and use a variable in a foreach statement the statement returns

    nothing.

    See below

    $compNames = "C:\Users\mderose\Downloads\Project\ewaste\CompNames.txt" $Computers = Get-Content "C:\computers.txt" $Computersdone = Get-ADComputer -Filter * | ForEach-Object {$_.Name -like "$Serial"} #The following works using part of the computer name. Get-ADComputer -Filter { name -like "*2UA21113XK*"} -Properties * Foreach ($Item in $Computers) { Get-ADComputer -Filter { name -like "*$Item*"} -Properties * # | Format-Table Name -AutoSize }

    So can anyone tell me why when I use the variable it returns nothing?

    I need to look up a lot of partial computer names and list the full computer names.




    Michael




    • Edited by Arkangel30 Wednesday, June 10, 2015 10:52 PM
    Wednesday, June 10, 2015 10:33 PM

Answers

  • This worked for me. There could be other ways:

    $Items = Get-Content .\Partials.txt
    
    ForEach ($Item In $ITems)
    {
        $Value = "*$ITem*"
        Get-ADComputer -Filter {Name -Like $Value} | Select Name
    }
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by Arkangel30 Wednesday, June 10, 2015 11:30 PM
    Wednesday, June 10, 2015 11:18 PM
    Moderator

All replies

  • This worked for me. There could be other ways:

    $Items = Get-Content .\Partials.txt
    
    ForEach ($Item In $ITems)
    {
        $Value = "*$ITem*"
        Get-ADComputer -Filter {Name -Like $Value} | Select Name
    }
    


    Richard Mueller - MVP Directory Services

    • Marked as answer by Arkangel30 Wednesday, June 10, 2015 11:30 PM
    Wednesday, June 10, 2015 11:18 PM
    Moderator
  • Hi Richard,

    Thank you for that update I think I see what I did wrong.

    Now what I need to do is take the items that a match was found on and list them like this:

    original value : Matched Value

    I just need to be sure these items are matched up, as I will need to remove them from the system and cannot remove the wrong ones.


    Michael

    Wednesday, June 10, 2015 11:31 PM
  • Here is what I tried but it does not output the matches

    ForEach($ItemIn$ITems)

    {

       

    $Value="*$ITem*"


       

    $Name=Get-ADComputer-Filter{Name-Like$Value} |SelectName

       

    $Name|Out-File$Results


    }


    Michael

    Wednesday, June 10, 2015 11:33 PM
  • Here is what I ended up with

    $Items = Get-Content "C:\serials.txt"
    $Results = "C:\Results.txt"
    ForEach ($Item In $ITems)
        {
            $Value = "*$ITem*"
            If ($Name = Get-ADComputer -Filter {Name -Like $Value} | Select Name)
            {
            #Write-host $Name, $Item
            "$Name,$Item" | Out-File $Results -Append
            }
            Else{
            "Serial $Item not found" | Out-File $Results -Append
            }
        }


    Michael

    Wednesday, June 10, 2015 11:45 PM
  • So easy it's easy...

    Get-Content C:\serials.txt }
        ForEach-Object{
            Get-ADComputer -Filter "Name -Like '*$ITem*'" |
              Select @{N='Item';E={$_}},Name
        } |
        Format-Table -Auto |
        Out-String |
        Out-File C:\Results.txt
    
    Hey.  It's deja' vu all over again.


    \_(ツ)_/

    Thursday, June 11, 2015 12:05 AM
  • @jrv

    Could you help explain what is occuring at this point in the scrpt you wrote?

    Select @{N='Item';E={$_}},Name


    Michael

    Thursday, June 11, 2015 4:23 PM
  • Search for "calculated properties"


    \_(ツ)_/

    Thursday, June 11, 2015 5:17 PM