locked
Compare-Object - not able to get the data I want. RRS feed

  • Question

  • We are not fully synced to 365 I want to bulk sync all accounts left.

    I want to compare $Adusers with $Msolusers and only get those that match. Can't seem to be able to do that.

    This script give all the ones in $Adusers that don't match also

    $AdUsers = Get-ADUser -Filter 'UserPrincipalName -like "*@domain.local"' | Where-Object {$_.Enabled -EQ $true} 
    $MsolUsersSynced = Get-MsolUser -All -Synchronized:$True | Where-Object {$_.IsLicensed -eq $True}
    $MsolUsersNotSynced = Get-MsolUser -All -Synchronized:$False | Where-Object {$_.IsLicensed -eq $True}
    $MsolUsers = Compare-Object -ReferenceObject $($MsolUsersSynced.Userprincipalname) -DifferenceObject $($MsolUsersNotSynced.UserPrincipalName) -PassThru
    Compare-Object -ReferenceObject $($AdUsers.Samaccountname) -DifferenceObject $($MsolUsers.split("@")[0]) -Passthru

    I am not far, but I am missing something.


    • Edited by yanni99 Wednesday, February 21, 2018 6:20 PM
    Wednesday, February 21, 2018 6:18 PM

All replies

  • help compare-object -online

    Carefully read how to use this CmdLet.


    \_(ツ)_/

    Wednesday, February 21, 2018 6:24 PM
  • Thank you, I have tried -ExcludeDifferent but doesn't give any answers. 

    But you gave me an idea I think .split("@")[0] that gives me the error so I am trying to extract it like this

    MsolUsersCompare = Compare-Object -ReferenceObject $($MsolUsersSynced.Userprincipalname) -DifferenceObject $($MsolUsersNotSynced.UserPrincipalName) -PassThru
    $MsolUsers = $MsolUsersCompare.split("@")[0]
    Compare-Object -ReferenceObject $($AdUsers.Samaccountname) -DifferenceObject $($MsolUsers) -ExcludeDifferent
    

    But when I put $MsolUsers it gives me only one object

    Wednesday, February 21, 2018 7:17 PM
  • "-IncludeEqual" is required.  Please carefully read ALL of the help.


    \_(ツ)_/

    Wednesday, February 21, 2018 7:21 PM
  • Hi,

    >> $MsolUsersSynced = Get-MsolUser -All -Synchronized:$True | Where-Object {$_.IsLicensed -eq $True}
    >> $MsolUsersNotSynced = Get-MsolUser -All -Synchronized:$False | Where-Object {$_.IsLicensed -eq $True}

    Will the execution result of these two commands have the intersection?

    >> $MsolUsers = Compare-Object -ReferenceObject $($MsolUsersSynced.Userprincipalname) -DifferenceObject $($MsolUsersNotSynced.UserPrincipalName) -PassThru

    If yes, do you want to compare the same results or the different results in this command?

    >> Compare-Object -ReferenceObject $($AdUsers.Samaccountname) -DifferenceObject $($MsolUsers.split("@")[0]) -Passthru

    To split multiple values, you need to loop like this: $MsolUsers | ForEach-Object {($_ -split '@')[0]}

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

    Best Regards,
    Albert

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

    Thursday, February 22, 2018 3:02 AM
  • Hi,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,
    Albert

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

    Monday, February 26, 2018 7:01 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert

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

    Wednesday, February 28, 2018 5:18 AM