Array comparision doesn't work with contanins and notcontains

$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"}

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.

$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.

 

 

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

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

That’s just an example!! Of course you can use whatever data you like and do a comparison. :wink:

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

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

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

My code example had 3 lines!
You wrote before:

… and that’s what my code example does. Did you try? Did you try to understand the code and the concept behind? :wink:

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 $_}