rws907
rws907

Reputation: 777

Identify if any string in one array exists in second array of strings with PowerShell

I have two arrays that I'm working with. Once that holds a set of criteria stored as strings, and one that is dynamically generated based on the result of a lookup performed earlier in my script. What I need to do is determine if ANY of the strings in the criteria array are present in the dynamic array so that I can do some conditional routines.

# The criteria array used for comparison 
$criteria = "sysadmin", "dbadmin", "netadmin"

What'd I'd like to do from this point is if the dynamic array contains any of those values, proceed otherwise stop processing. So if

$dynamicarray = "voipadmin", "mailadmin", "sysadmin"

The comparison would be true.

I'm still fairly new with PowerShell but so far -contains or .contains or -match don't seem to work. The answer needs to work with PoSH 2.0.

UPDATE

The system I was testing on worked but when I moved it over to the other system it did not. The only difference is that the test system is PoSH 3.0 and the prod system is 2.0

Here's more of the prod script so you can better understand

$user = $env:USERNAME
$workstation = $env:COMPUTERNAME

$userMemberOf = ([ADSISEARCHER]"samaccountname=$user").Findone().Properties.memberof -replace '^CN=([^,]+).+$','$1' # User Group Memberships

$MMIArray1 = "Dispatch", "Engineers", "Operators"

# USER GROUP-BASED CUSTOMIZATIONS
if(@(Compare-Object $MMIArray1 $userMemberOf -includeequal -excludedifferent).count -gt 0){
#matches
  Write-Host "true"
}else{
  #no matches
  Write-Host "false"
}

On the Prod PoSH 2.0 environment everything evaluates to false.

UPDATE 2

After some tinkering I found that I needed to change

if($(Compare-Object $MMIArray1 $userMemberOf -includeequal -excludedifferent).count -gt 0){

to

if(@(Compare-Object $MMIArray1 $userMemberOf -includeequal -excludedifferent).count -gt 0){

Upvotes: 2

Views: 9467

Answers (2)

Matt
Matt

Reputation: 46730

Nathan Rice has a simpler answer but since you mention -contains and -match I thought I could show you a couple of ways to make that work. Given your sample strings

$criteria = "sysadmin", "dbadmin", "netadmin"
$dynamicarray = "voipadmin", "mailadmin", "sysadmin"

-Match

If([string]$criteria -match ($dynamicarray -join "|")){
    Write-host "Do something"
}

-Match is typically doing a regex comparison on a string and returning a [boolean] result. You have arrays to we would need to cast one as a string and create a regex match with the other. A translated way to look at what happens here is

"sysadmin dbadmin netadmin" -match voipadmin|mailadmin|sysadmin

In a production environment in might be benificial to harden the regex string. Since there is potential that special characters might sneak in the array it would be a good practice to escape them if present.

If([string]$criteria -match "($(($dynamicarray|ForEach{[RegEx]::Escape($_)}) -join '|'))"{}

Looking to match the string to one of those variables "voipadmin", "mailadmin" or "sysadmin". Since it contains "sysadmin" the statement is true.

-Contains

$results = @($dynamicarray | Where-Object{$criteria  -contains $_})
If($results.Count -gt 0){
    "There was at least one match"
}

-Contains can be misleading to new users (side note it go me). It is checking to see if a single element is located in an array. Since you have two arrays we would need to check each element at a time. Not the only approach but the above will allow elements through that are contained in the $dynamicarray. Again, there is one match to the .Count will be 1. I think there are some issues with how count deals with single element returns to the result needs to be cast as an array @() to ensure the correct number.

Upvotes: 5

Nathan Rice
Nathan Rice

Reputation: 3111

If the count of equal values is greater than 0 you've got matches:

If ($(Compare-Object $criteria $dynamicarray -includeequal -excludedifferent).count -gt 0) {
  #matches
  Write-Host "true"
} Else {
  #no matches
  Write-Host "false"
}

Upvotes: 1

Related Questions