Determine if any string exists in one array in second array of strings using PowerShell

I have two arrays that I am working with. When it contains a set of criteria, stored as strings, and one that is dynamically generated based on the search result I did earlier in my script. What I need to do is determine if there are ANY rows in the criteria array in the criteria array so that I can perform some conditional procedures.

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

      

What I would like to make from this point, if the dynamic array contains any of these values, continue otherwise terminate processing. Therefore, if

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

      

The comparison will be correct.

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

UPDATE

The system I tested worked, but when I moved it to another system it didn't. The only difference is that the test system is PoSH 3.0 and the prod system is 2.0

There is more prod script here so you can understand better

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

      

In the Prod PoSH 2.0 environment, everything evaluates to false.

UPDATE 2

After some exercise 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){

      

+1


source to share


2 answers


Nathan Rice has an easier answer, but since you mention -contains

and -match

, I thought I could show you several ways to make this work. Given your lines of lines

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

      

-Match

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

      

-match

usually performs a regex comparison on a string and returns the result [boolean]

. You have arrays, we would need to distinguish one string and create a regex with another. The translated way to see what's going on here is

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

      



In a production environment it can be benificial to solidify the regex string. Since there is a potential for special characters to sneak into an array, it would be good practice to avoid them if they exist.

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

      

Looking at whether a string matches one of these 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 confusing for new users (note that I'm going). It checks if one element is in the array. Since you have two arrays, we will need to check each element at a time. Not the only approach, but the above, will allow items that are contained in $dynamicarray

. Again, there is one match with .Count

will 1. I think there are some problems with how the counter handles returning one element to the result, must be distinguished as an array @()

to provide the correct number.

+4


source


If the number of equal values ​​is greater than 0, you have matches:



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

      

+1


source







All Articles