Search code examples
arrayspowershellcomparisonpowershell-5.0

Using -like operator with two arrays


I have a question regarding the -like operator in PowerShell.

Script written in

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

I have the following variable.

$ProgramVar

This variable can contain Example;

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

I have another variable called $Exclusions

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

I need to compare each $program in $ProgramVar and see if the $program -like any $exclusion. I've written it several different ways but i cannot figure out the correct way. I've deleted most of what i've tried but here's where i'm currently at..

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

        }
    }
}

I think this will work but haven't fully tested it. I would like to know if there is more cleaner or quicker way.

EDIT:

Props to JPBlanc for answering my question. I have gone through and made it a little easier to read. Here's what i ended up with so that others can easily implement the code. JPBlanc's solution will work, but like i said, i like it to be a little verbose so i can see what it's doing.

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

Thanks again


Solution

  • According to the fact that $a contains the strings and $e contains the exclusions :

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

    This one liner gives the strings to exclude :

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

    $r : a tab of string to exclude (the return value).

    $t : a temp var conttaining currentstring to evaluate.

    At the end you can get a new tab :

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