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/