Author Posts

March 1, 2016 at 6:31 am

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

March 1, 2016 at 6:44 am

Are the entries in the text file in order, or can the newest entry just be appended to the end?

March 1, 2016 at 6:55 am

The newest entry/entries can just be added to the end.

March 1, 2016 at 7:16 am

$guid = (get-adcomputer ...).objectguid

if($guid.guid -notin (gc h:\GUIDs.txt)){

$GUID.guid | out-file h:\GUIDs.txt -append

}

March 1, 2016 at 7:51 am

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.

March 1, 2016 at 8:05 am

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.

March 1, 2016 at 8:13 am

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
}
}

March 1, 2016 at 8:13 am

Thanks again 🙂

March 1, 2016 at 11:19 pm

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