0

I'm getting a little lost working out the logic for the following script I am working on. I have a list of asset numbers currently stored in a text file as follows:

assetlist.txt
111111
222222
333333
444444

I am attempting to query Active Directory to verify if the ADComputer object exists. If it is found, I am looking for the ADComputer Name and the DistinguishedName. Here is a sample of my code:

$assetList = Get-Content C:\assetlist.txt

Foreach ($asset  in $assetList){
    $adComputer = Get-ADComputer -Filter "Name -like '*$assett'" | Select-Object Name, DistinguishedName

    $resultTable = @()
    $result = New-Object -TypeName PSObject
    $result | Add-Member -MemberType NoteProperty -Name AssetNumber -Value $asset
    $result | Add-Member -MemberType NoteProperty -Name ADName -Value $adComputer.Name
    $result | Add-Member -MemberType NoteProperty -Name OU -Value $adComputer.DistinguishedName
    $resultTable += $result

    $resultTable
}

I am getting lost in manipulating the results I want to see in my result table. Ultimately I would like to see something along the following lines returned:

AssetNumber         ADName                  OU
-----------         ------                  --
111111              111111.mydomain.com     CN=111111,OU=MYOU,DC=mydomain,DC=com
222222              Not Found               Not Found
333333              333333.mydomain.com     CN=333333,OU=MYOU,DC=mydomain,DC=com

I know I am missing an IF statement currently to handle if a result is displayed or if a "Not Found" message is recorded in its place. But currently I am stuck even getting the correct values to be returned.

Any guidance would be greatly appreciated. Here are also a few links I've gone over so far for some research:

https://technet.microsoft.com/en-us/library/ff730946.aspx https://blogs.technet.microsoft.com/josebda/2014/04/19/powershell-tips-for-building-objects-with-custom-properties-and-special-formatting/

2 Answers 2

1

To be near your code but using [pscustomobject]

$assetList = Get-Content C:\assetlist.txt
$resultTable = @()

Foreach ($asset  in $assetList){
  $adComputer = Get-ADComputer -Filter "Name -like '*$asset'"
  If ($adComputer){
    $result = [pscustomobject][ordered]@{AssetNumber = $asset
                                         ADName = $adComputer.Name
                                         OU = $adComputer.DistinguishedName}
  } else {
    $result = [pscustomobject][ordered]@{AssetNumber = $asset
                                         ADName = 'Not Found'
                                         OU = 'Not Found'}
  }
  $resultTable += $result
}
$resultTable
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your feedback. I think this was most along the lines of what I was looking for.
0

You had an extra t after $asset in your Get-ADComputer. Building your object can be replaced by calculated properties with Select-Object.

$assetList = Get-Content C:\assetlist.txt

$resultTable = Foreach ($asset in $assetList){
    $adComputer = Get-ADComputer -Filter "Name -like '*$asset'" |
        Select-Object @{Name = 'Asset'; Expression = {$Asset}},@{Name = 'ADName'; Expression = {$_.Name}}, @{Name = 'OU'; Expression = {$_.DistinguishedName}}
    if ($null -eq $adComputer) {
        $adComputer = "" | Select-Object @{Name = 'Asset'; Expression = {$Asset}},@{Name = 'ADName'; Expression = {"Not Found"}}, @{Name = 'OU'; Expression = {"Not Found"}}
    }
    $adComputer
}

1 Comment

Thank you for pointing out the mistake. I see now what was wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.