Trying to get AD Computer ObjectGUID and add it to a text file

This topic contains 8 replies, has 5 voices, and was last updated by Profile photo of Max Kozlov Max Kozlov 9 months ago.

  • Author
    Posts
  • #35850
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    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

  • #35851
    Profile photo of Bob McCoy
    Bob McCoy
    Participant

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

  • #35852
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

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

  • #35854
    Profile photo of Dan Potter
    Dan Potter
    Participant

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

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

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

    }

  • #35856
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    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.

  • #35858
    Profile photo of Dan Potter
    Dan Potter
    Participant

    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.

  • #35859
    Profile photo of Aapeli Hietikko
    Aapeli Hietikko
    Participant

    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
    }
    }
    
  • #35860
    Profile photo of Ed O'Connor
    Ed O’Connor
    Participant

    Thanks again 🙂

  • #35907
    Profile photo of Max Kozlov
    Max Kozlov
    Participant

    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
    

You must be logged in to reply to this topic.