locked
Compare 2 Attributes in AD if mismatch output to CSV File RRS feed

  • Question

  • I am trying to run a simple script that i created to compare the department field and extensionAttribute9 against users in a OU or overall OU.  then output the the mismatch ones to a CSV.  See Below

    The department field for example is CSSD and the EA9 field is XXX-XXX-CSSD.  I require the script to compare CSSD against only the last portion of XXX-XXX-CSSD.  Cannot figure out for the script to only look at the 3rd group and not the whole group.  If they do not match, output to a csv file so the AD accounts can be corrected.

    Thank you

    Clear-Host
    
    Import-Module Activedirectory
    
    Write-Host "Processing"
    
        $OU="OU=ESQ-One,OU=One-Users,OU=Two,OU=Accounts,DC=Contessa,DC=Hel,DC=ca"
    
        $AllUsersList = Get-ADUser -filter * -searchbase $OU -Properties Department, displayName,extensionAttribute9 |
    
    Select-Object displayName, Department, extensionAttribute9 -ErrorAction SilentlyContinue
    
    
    
        $AllUsersList | Export-Csv -Path "C:\Scripts\ADConditioning.csv" -NoTypeInformation
    
    Write-Host "Completed"

    Tuesday, November 7, 2017 2:27 AM

Answers

  • Same thing:

    Where{$_.extensionAttribute9 .EndsWith($_.department}


    \_(ツ)_/

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:58 AM
    Tuesday, November 7, 2017 5:28 AM
  • Hi,

    I agree with jrv. In addition, you could add -not parameter so that get ad users who do not match. The following script for your reference, hope it is helpful to you:
    $AllUserList = Get-ADUser -Filter * -SearchBase $OU -Properties DisplayName,Department,extensionAttribute9 | Where-Object {-not ($_.extensionAttribute9.EndsWith($_.Department))} | Select-Object DisplayName,Department,extensionAttribute9

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:57 AM
    Tuesday, November 7, 2017 7:46 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:57 AM
    Thursday, November 9, 2017 8:16 AM

All replies

  • I don't see where you are comparing anything.  How are you trying to do this?

    $this = 'XXX-XXX-CSSD'
    $that = 'CSSD'
    if ($this.EndsWith($that)) {
        # end with the string.
    } else {
        # doesn't end with the string
    }


    \_(ツ)_/


    • Edited by jrv Tuesday, November 7, 2017 3:04 AM
    Tuesday, November 7, 2017 3:02 AM
  • Sorry forgot a line

    Where-Object {$_.department -eq $_.extensionAttribute9 }

    not sure to use -eq or -like

    For example ea9 is ESQ-BIS-CSSD

    Tuesday, November 7, 2017 5:23 AM
  • Same thing:

    Where{$_.extensionAttribute9 .EndsWith($_.department}


    \_(ツ)_/

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:58 AM
    Tuesday, November 7, 2017 5:28 AM
  • Hi,

    I agree with jrv. In addition, you could add -not parameter so that get ad users who do not match. The following script for your reference, hope it is helpful to you:
    $AllUserList = Get-ADUser -Filter * -SearchBase $OU -Properties DisplayName,Department,extensionAttribute9 | Where-Object {-not ($_.extensionAttribute9.EndsWith($_.Department))} | Select-Object DisplayName,Department,extensionAttribute9

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:57 AM
    Tuesday, November 7, 2017 7:46 AM
  • Hi,

    Just checking in to see if the information provided was helpful. Does the script work?

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    • Marked as answer by Mike McKay2 Friday, November 10, 2017 3:57 AM
    Thursday, November 9, 2017 8:16 AM