All,
I am trying to write a script that will search AD for computers in a specific OU, get the ObjectGUID, check the ObjectGUID against a text file, and if that ObjectGUID is not in the text file to add it/append it to the file. I can’t seem to get it right and could use a nudge in the right direction.
Here is what I have:
$CompGUID = $null
$GUID = $null
$GUIDinFile = $null
#Gets the ObjectGUID's from AD
$CompGUID = Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -filter * | select -expand ObjectGUID
#Checks the text file to see if the ObjectGUID is in it and adds it if not
foreach ($GUID in $CompGUID) {
$GUIDinFile = Select-String -Path c:\temp\GUIDs.txt -pattern $GUID
if ($GUIDinFile -notlike $GUID) {$GUID | out-file C:\temp\GUIDs.txt}
}
Now if I simple type $GUID in the powershell window I get the GUID but my text file is empty and nothing is being added.
Thanks, in advance
Are the entries in the text file in order, or can the newest entry just be appended to the end?
The newest entry/entries can just be added to the end.
$guid = (get-adcomputer …).objectguid
if($guid.guid -notin (gc h:\GUIDs.txt)){
$GUID.guid | out-file h:\GUIDs.txt -append
}
Thank you very much, that gave me what I needed. I just had to add a ‘for-each’ statement so it handles each GUID individually.
$CompGuid = (Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -filter *).objectguid
foreach ($Guid in $CompGuid) {
if($guid.guid -notin (gc c:\Temp\GUIDs.txt)){
$GUID.guid | out-file c:\Temp\GUIDs.txt -append
}
}
This gives me exactly what I need and I appreciate all the help.
For my knowledge can you explain the difference in the two lines:
#Dan's code
$CompGuid = (Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -filter *).objectguid
#Mine
$CompGUID = Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -filter * | select -expand ObjectGUID
I really appreciate the help.
Personal preference I suppose. When I’m dealing with single properties I use ().property
Select for more than one and less than default.
They both return strings.
I agree with Dan that being just two different ways to do things. I’m more of the (). guy, but | -expandProperty is maybe easier to read.
I’d toss one more thing, that I use with objectGuid.
$CompGuid = (Get-ADComputer -SearchBase "OU=DSC Managed Nodes,OU=SERVERS,OU=NYC,OU=Americas,DC=lab2,DC=mckinsey,DC=com" -filter *).objectGuid.Guid
foreach ($Guid in $CompGuid) {
if($Guid -notin (gc c:\Temp\GUIDs.txt)){
$Guid | out-file c:\Temp\GUIDs.txt -append
}
}
If your $CompGuid count much lesser than guids already in file but greater than 1, you get significant performance loss, because you read all file for each $CompGuid you get.
then I suggest to reverse search cycle against file
foreach ($fileGiud in (gc c:\Temp\GUIDs.txt)) {
$CompGuid = $CompGuid -ne $fileGuid #filtering
}
$CompGuid | out-file c:\Temp\GUIDs.txt -append