Array comparision doesn't work with contanins and notcontains

Welcome Forums General PowerShell Q&A Array comparision doesn't work with contanins and notcontains

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

 
Participant
1 month, 3 weeks ago.

  • Author
    Posts
  • #119041

    Participant
    Points: 19
    Rank: Member
    $argu1 = Get-ChildItem C:\ProgramData\Citrix\GroupPolicy -Exclude Rollback.gpf,Rsop.gpf | Split-Path -Leaf
    
    $argu2 = Get-ChildItem HKLM:\SOFTWARE\Policies\Citrix | Split-Path -Leaf
    $common = $argu1 | ? {if($argu2 -contains $_){$_}}  # find the common.
    Add-PSSnapin Citrix* -ErrorAction SilentlyContinue
    if($Ctxsession=@(Get-XASession -LocalhostOnly | Where-Object { $_.State -match 'Active' -and $_.Protocol -match 'ICA'} | Select-Object -ExpandProperty SessionID | Format-Table -HideTableHeaders))
    {
    $final = Compare-Object $Ctxsession $common | Where-Object {$_.SideIndicator -eq "=>"} | Select-Object -ExpandProperty InputObject | Format-Table -HideTableHeaders
    
    $exclude = $final | where {$Ctxsession -notcontains $_} # $exclude gives all values and doesn't work as expected and if I use contains operator $exclude is empty
    
    }
    
    else{Write-Host "Didn't find anything"}
  • #119050

    Participant
    Points: 889
    Helping Hand
    Rank: Major Contributor

    You didn't mention the problem you are facing. Please tell us what error/unexpected behavior you get using the above code.
    And please edit the post and format the code properly. Let us know if you find it difficult to format the code.

  • #119055

    Participant
    Points: 19
    Rank: Member

    $ctxsession and $final or other variables mentioned in the code are array objects.

    $Ctxsession has random values which are in the $final as well. while $final holds more array structured numerical values. example :

    PS C:\Windows\system32> $Ctxsession
    10
    11

    PS C:\Windows\system32> $final
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    2
    20

    I am trying to get the values which are not common ; where the operators being used as -notcontains but $exclude give all values which is being held by $final.

    hope would be clear now.

     

     

  • #119058

    Participant
    Points: 268
    Helping Hand
    Rank: Contributor

    How about something like this:

    $Ctxsession = @(10,11)
    $final = @(10,11,12,13,14,15,16,17,18,19,2,20)
    Compare-Object -ReferenceObject $Ctxsession -DifferenceObject $final
  • #119061

    Participant
    Points: 19
    Rank: Member

    $ctxsession and $final holds the runtime value not the static.

  • #119067

    Participant
    Points: 268
    Helping Hand
    Rank: Contributor

    That's just an example!! Of course you can use whatever data you like and do a comparison. 😉

    • #119070

      Participant
      Points: 19
      Rank: Member

      Sorry, But it didn't provide the expected result. is there any issue with code line 10.

  • #119071

    Participant
    Points: 889
    Helping Hand
    Rank: Major Contributor

    Compare-Object should work. Did you try -notin operator ?

    $final | Where-Object -FilterScript {$_ -notin $Ctxsession}
    
  • #119106

    Participant
    Points: 268
    Helping Hand
    Rank: Contributor

    Sorry, But it didn't provide the expected result. is there any issue with code line 10.

    My code example had 3 lines!
    You wrote before:

    I am trying to get the values which are not common

    ... and that's what my code example does. Did you try? Did you try to understand the code and the concept behind? 😉

  • #119107

    Participant
    Points: 19
    Rank: Member

    Found the problem with $exclude ; as it should have been declared as empty array initially; which wasn't done there.

    $exclude = @()

    $exclude = $final | where {$Ctxsession -notcontains $_}

     

You must be logged in to reply to this topic.