使用PowerShell识别第二个字符串数组中是否存在一个数组中的任何字符串 [英] Identify if any string in one array exists in second array of strings with PowerShell

查看:434
本文介绍了使用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"

比较是正确的。

我对PowerShell还是很陌生,但是遥远或.contains或-match似乎不起作用。答案需要与PoSH 2.0配合使用。

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.

更新

系统I正在测试工作,但是当我将其移至另一个系统时却没有。唯一的区别是测试系统是PoSH 3.0,生产系统是2.0

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

在Prod PoSH 2.0环境中,所有评估结果均为false。

On the Prod PoSH 2.0 environment everything evaluates to false.

更新2

经过一番修补,我发现我需要进行更改

After some tinkering I found that I needed to change

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

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


推荐答案

内森·赖斯(Nathan Rice)给出了一个简单的答案,但自从您提到包含 -match 我想我可以向您展示几种实现该目标的方法。给定您的示例字符串

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"

-匹配

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

-匹配通常在字符串上进行正则表达式比较,并返回 [boolean] 结果。您有数组,我们需要将其中一个强制转换为字符串并与另一个创建正则表达式匹配。

-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 '|'))"{}

希望将字符串匹配为

-包含,因为其中之一是 voipadmin, mailadmin或 sysadmin。 >

-Contains

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

-包含可能会误导新用户(请注意,这是我的意思),它正在检查是否是元素位于一个数组中。由于您有两个数组,因此我们需要一次检查每个元素。不是唯一的方法,但是上面的方法允许通过包含的元素c> $ dynamicarray 。同样,有一个匹配项。Count将为1。我认为存在一些问题h如何将计数如何处理单个元素返回结果需要转换为数组 @()以确保正确的数字。

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

这篇关于使用PowerShell识别第二个字符串数组中是否存在一个数组中的任何字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆