Compare-Object don't work

Welcome Forums General PowerShell Q&A Compare-Object don't work

This topic contains 9 replies, has 3 voices, and was last updated by

 
Participant
3 months ago.

  • Author
    Posts
  • #145533

    Participant
    Topics: 3
    Replies: 8
    Points: 33
    Rank: Member
    I have an xml file to read in to ad.
    The hard part is that what is stated in the xml file is what applies.
    First, it must be checked that no countries other than Norway and Sweden are updated. No problem with that.
    But in ad it can say that a certain user belongs to Sweden but in the xml file it says for example. Finland and then it is the one in the xml file that should apply and it should not be updated and it is this particular control i have problems with. Anyone have an idea?
    Now I have don'e that control something like this. And I can't get this to work.

    #Importing AD
    Import-Module ActiveDirectory

    [xml]$XmlDocument = Get-Content -Path c:\scripts\users.xml
    $xml1 = $XmlDocument.NewDataSet.Table1

    #Importing AD Users
    $adusers = Get-ADUser -Filter * -Properties * | Where Country -NE $null

    #Compare – column SamAccountName
    $Results = Compare-Object $xml1 $adusers -Property SamAccountName,Country -IncludeEqual

    $Array = @()
    Foreach($R in $Results)
    {
    If( $R.sideindicator -eq "==" )
    {
    $Object = [pscustomobject][ordered] @{

    Username = $R.SamAccountName
    "Compare indicator" = $R.sideindicator
    Country = $R.Country
    }

    $Array += $Object
    }
    }

    #Count users in both files
    #($Array | sort-object username | Select-Object * -Unique).count

    #Display results in console
    $Array

  • #145574

    Participant
    Topics: 1
    Replies: 1374
    Points: 1,884
    Helping Hand
    Rank: Community Hero

    Please format your code as code by using the code (pre) tag button provided on the edit bar of the post editor. Thanks.

    Are you sure Compare-Object works the way you expect it? If you only need the equal elements from both objects you can add the parameter -ExcludeDifferent. Additionally you could add -PassThru to get the input object as output as well.

  • #145863

    Participant
    Topics: 3
    Replies: 8
    Points: 33
    Rank: Member

    Ok sorry for not pre tag first time in here.

    I still can't get this to work. It shows me either < = or => as sideindicator if I want to select == it wount work even if the country are like in file and in ad then it just show nothing.

    And this is the problem it shall only get info that are alike == nothing else. If ther are difference between file and ad in country then it shall not show that at all.

  • #145889

    Participant
    Topics: 1
    Replies: 1374
    Points: 1,884
    Helping Hand
    Rank: Community Hero

    Is there a special reason why you are using XML formatted data? Most people would choose CSV for structured data and for comparison. Espcially because the code line you commented with #Importing AD Users creates a structured data set from the AD.

  • #146183

    Participant
    Topics: 3
    Replies: 8
    Points: 33
    Rank: Member

    Yes! This is because the management program used by the customer only spits out file in xml.
    And yes I would prefere csv but that is not my choice.

    Right now I am away on work but I can upload the whole code for display if that helps.

    Just get rid of some details.

  • #146190

    Participant
    Topics: 1
    Replies: 1374
    Points: 1,884
    Helping Hand
    Rank: Community Hero

    You cannot post XML formatted data here in the forum. You will have to upload it to GitHubGist and share it this way here.

  • #146199

    Participant
    Topics: 3
    Replies: 8
    Points: 33
    Rank: Member

    I did mean the Powershell code in total

  • #146207

    Participant
    Topics: 1
    Replies: 1374
    Points: 1,884
    Helping Hand
    Rank: Community Hero

    OK, but without seeing at least the sctructure of the XML data you have to deal with it will be unlikely to help you.

  • #146208

    Participant
    Topics: 3
    Replies: 8
    Points: 33
    Rank: Member

    Ok. I will try to get it uploaded on GitHub. Thanks

  • #146340

    Participant
    Topics: 11
    Replies: 145
    Points: 48
    Rank: Member

    Instead of using an if clause to compare if the side indicator is the desired one, have you tried piping it through Where-Object instead?

    I ran into this issue with a script I wrote at work, and I don't have access to our private github right now, but it I remember on Monday I'll check the code.

    However, I seem to remember that Where-Object was the thing that finally worked for me. Trying to use comparison operators and if clauses on special charectors like the side indicator would not return $True even when you expect it to, but Where-Object did work.

    $Results | Where-Object {$_.SideIndicator -like '=='} | Select-Object -ExpandProperty InputObject

    If I'm way off base we can help better once we have a sanitized copy of your XML. Please do not share your actual AD userids online

The topic ‘Compare-Object don't work’ is closed to new replies.