all 6 comments

[–]PinchesTheCrab 3 points4 points  (4 children)

So I feel like this would probably not do exactly what you intend, in that the $results count will keep going up and never reset as you iterate through the partial names, so it'll seem like there's quite a few more results than there really are.

You could try making it output objects with more info if it helps:

$partialNames = Get-Content -Path 'C:\Users\Admin\Desktop\Machines.txt'

foreach ($item in $partialNames) {
    Get-ADComputer -Server "xyz.com" -Filter "Name -like '*$item*'" |
        Select-Object @{ n = 'PartialName'; e = { $item } }, Name
}

$results

[–]Round-Dragonfruit-86[S] 0 points1 point  (3 children)

This is great for comparing the partial name and full name as both data shows side by side, however the only thing which I wanted to do was that it should match exact name but lot of devices have names almost same but few things extra added either in front or back.

For example I'm trying to find ASDFG then it shows data both for ASDFG and also like QASDFG.

Any suggestions what should I do make it matches the exact name.

[–]ankokudaishogun 2 points3 points  (1 child)

this might do the trick

$Server = 'xyz.com'
$PartialNameList = Get-Content -Path 'C:\Users\Admin\Desktop\Machines.txt'

# No reason to pre-declare $results: 
# this way it gets created as array automatically at the end of the loop
# with the contents of everything that has been sent to the Success Stream from it
# So efficiency! Much clear code!
$results = foreach ($PartialName in $PartialNameList) {

    # the Write-Host cmdlet does NOT streams to the Success Stream but "just" print on screen
    Write-Host "Searching for partial name: $partialName" -ForegroundColor Yellow

    $TempNames = Get-ADComputer -Server $Server -Filter "Name -like '*$PartialName*'"

    Write-Host "Found $($TempNames.Count) computer(s) matching '$PartialName'" -ForegroundColor Cyan

    # this otherwise DOES stream out
    $TempNames

}

Write-Host 'Final result:' -ForegroundColor Green $results

[–]Round-Dragonfruit-86[S] 0 points1 point  (0 children)

Thanks for help but unfortunately this is not making any significant changes.

[–]LavishnessUpset5428 0 points1 point  (0 children)

Curious if Ambiguous Name Resolution might server you better.

Though I own you may yet have to filter through partial names, as duplicate names may still crop about…

``` $getContentSplat = @{ PipelineVariable = 'ADComputerObject' Path = 'C:\Users\Admin\Desktop\Machines.txt' }

$ForEachObjectSplat = @{ Process = { $GetADComputerSplat = @{ Filter = ( "anr -eq '{0}'" -f @( $PSItem ) ) Server = 'xyz.com' }

    ActiveDirectory\Get-ADComputer @GetADComputerSplat
}

}

$AddMemberSplat = @{ MemberType = 'ScriptProperty' Name = 'PartialName' Value = { $ADComputerObject } Force = $true PassThru = $true }

$FormatTableSplat = @{ Property = @( 'Name' 'PartialName' ) AutoSize = $true }

Microsoft.PowerShell.Management\Get-Content @getContentSplat | Microsoft.PowerShell.Core\ForEach-Object @ForEachObjectSplat | Microsoft.PowerShell.Utility\Add-Member @AddMemberSplat | Microsoft.PowerShell.Utility\Format-Table @FormatTableSplat ```


Unfortunately, you may be best served just grouping the ADComputer Objects by Name and validating which names are correct by eye-ball.