使用-like运算符和两个数组

时间:2017-02-02 02:42:20

标签: arrays powershell comparison powershell-v5.0

我对PowerShell中的-like运算符有疑问。

编写的脚本
PSVersion                      5.0.10240.17146
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   10.0.10011.16384
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3

我有以下变量。

$ProgramVar

此变量可以包含Example;

Microsoft VisualStudio JavaScript Project System
Java 7 Update 80
Java 8 Update 111
Microsoft VisualStudio JavaScript Language Service
Java Auto Updater

我有另一个名为$ Exclusions的变量

$Exclusions = @("*Microsoft Corporation*","*Microsoft*","*SAP BusinessObjects*","*Hewlett-Packard*","*JavaCard*","*Development*")

我需要比较$ ProgramVar中的每个$程序,看看$ program-like是否有任何$排除。我用几种不同的方式写了它,但我无法弄清楚正确的方法。我已经删除了我尝试过的大部分内容但是我现在所处的地方......

foreach($instance in $Programvar)
{

    if($instance -eq $Null) #<---- Dummy Check.
    {
        Write-Host "I is blank"
    }
    else
    {
        foreach($ex in $Exclusions)
        {
            #$count = 0
            #$exclusion = $Exclusions.Get($count)
            if($instance -like $ex)
            {
                Write-Host "A substring in $instance matches $ex.. Removing from array "                     
            }
            else
            {
                Write-Host "$instance does not contain $ex Substring"
            }

        }
    }
}

我认为这样可行,但尚未对其进行全面测试。我想知道是否有更清洁或更快捷的方式。

编辑:

支持JPBlanc回答我的问题。我已经完成并且使它更容易阅读。这就是我最终得到的结果,以便其他人可以轻松实现代码。 JPBlanc的解决方案可行,但就像我说的那样,我喜欢它有点冗长,所以我可以看到它在做什么。

    $ExclusionTester = $Programvar | % `
    {
        Write-Host "Testing Exclusions: $Exclusions"
        Write-Host "$programvar"
        $ExclusionsExtracted=@() #<--- Blank array
    }`
    {
        $Program=$_; $Exclusions | % `
            {

                if ($Program -like $_) #<---- $_ Current exclusion
                    { 
                        $ExclusionsExtracted +=$Program #<---- Adding exclusion to array
                        write-host "A substring in $Program Matches $_" -ForegroundColor green
                        Write-Host "Removing $Program from ProgramVar array" -ForegroundColor Cyan
                    }

                    #Uncomment the below line to possibly rebun?
                    <#
                    else
                    {
                        Write-Host "No Substring in $t Matches $_" -ForegroundColor Yellow
                    } #>
            }
    }`
    {
        $ExclusionsExtracted #<---- End result after testing.
        $programvar = $programvar | where {$ExclusionsExtracted -notcontains $_}
    } 

再次感谢

2 个答案:

答案 0 :(得分:1)

根据$ a包含字符串而$ e包含排除项的事实:

$a = "Microsoft VisualStudio JavaScript Project System","Java 7 Update 80","Java 8 Update 111","Microsoft VisualStudio JavaScript Language Service","Java Auto Updater"
$e = "*Microsoft Corporation*","*Microsoft*","*SAP BusinessObjects*","*Hewlett-Packard*","*JavaCard*","*Development*"

这一个班轮允许排除字符串:

$a | % {$r=@()}{$t=$_; $e | % {if ($t -like $_){$r+=$t}}}{$r}

$ r:要排除的字符串选项卡(返回值)。

$ t:要评估的temp var conttaining currentstring。

最后,您可以获得一个新标签:

$tabres = $a | where {$r -notcontains $_}

答案 1 :(得分:-1)

尝试类似这样的事情

$ProgramVar = "Microsoft VisualStudio JavaScript Project System","Java 7 Update 80","Java 8 Update 111","Microsoft VisualStudio JavaScript Language Service","Java Auto Updater"
$Exclusion = "*Microsoft Corporation*","*Microsoft*","*SAP BusinessObjects*","*Hewlett-Packard*","*JavaCard*","*Development*"

$ProgramVar | where {$Prog=$_; $Exclusion | where {$Prog -like $_ } }