Greetings!
Relatively new to powershell attempting to find AD service accounts that are not in the file test.txt. As you can see I’m struggling with how to get this to work
Here is the code I’m working with:
$server=get-content C:\temp\test.txt
Get-ADUser -SearchBase “OU=Serviceaccounts,DC=nlong,DC=com” -Filter {Name -like “svcxxsql*”} |Where-Object {$_.Name -notin $server} | sort | Select-Object Name | Out-File -FilePath C:\temp\foo.txt
Here are the contents of test.txt:
svcXXSQL001Agent
svcXXSQL001DBEng
svcXXSQL001Int
svcXXSQL002Agent
svcXXSQL002Eng
svcXXSQL002Int
When running the code with test.txt here is output in foo.txt:
Name
svcXXSQL002Agent
svcXXSQL002Eng
As you can see the svcXXSQL001Agent, svcXXSQL001DBEng and svcXXSQL001Int are not there, which is a good. However, svcXXSQL002Agent and svcXXSQL002Eng are still in foo.txt, they should not be there.
Now if I change the contents of test.txt to the following:
svcXXSQL001Agent
svcXXSQL001DBEng
svcXXSQL001Int
And re-run Get-ADUser -SearchBase “OU=ServiceAccounts,DC=nlong,DC=com” -Filter {Name -like “svcxxsql*”} |Where-Object {$_.Name -notin $server} | sort | Select-Object Name | Out-File -FilePath C:\temp\foo.txt
The output in foo.txt is correct the XXSQL001 entries have been removed, the first entries in the file are xxsql002Agent, Eng and Int.
Name
svcXXSQL002Agent
svcXXSQL002Eng
svcXXSQL002Int
Something is amiss, having more than three entries in test.txt is causing shall we say opportunities which I’m not smart enough to understand.
Thanks for your input.
Norm