简体   繁体   中英

Powershell Like Wildcard issue

Can't get this wildcard comparison to work, what am I doing wrong? I can only get:

Windows Server 2016

Windows 10 Enterprise x64

For instance, I can't get

Windows 10 Enterprise X64 1709 ?

how come? I also get only 2 hints, it should give me 5

Output of $TaskSequence.name is

Windows Server 2012 R2 X64

Windows 10 Enterprise x64 USMT Hardlinks

Windows 10 Enterprise x64 1703 en-US

Windows Server 2016

Windows 10 Enterprise x64 USMT Full OS

Windows 10 Enterprise x64

Windows 7

Output of $ImagePackage.name is

Windows Server 2016 x64

Windows 10 Enterprise x64

$SiteCode = "PS1"
    $SiteServer = "SRVSCCM01"
    $TaskSequence = (Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_TaskSequencePackage -ComputerName $SiteServer)
    $ImagePackage = (Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_ImagePackage -ComputerName $SiteServer)
    Foreach($Task in $TaskSequence){
        Foreach($Image in $ImagePackage){ 
            if($Task.Name -like "*$($Image.Name)*" -or $Image.Name -like "*$($Task.Name)*"){ 
                $Task.name

            } 
        }
    }

I think you're overcomplicating things a little bit. I'm assuming your WMI calls are returning arrays of objects with this example:

$Output = @()
$Output += $TaskSequence.Name | Where-Object { $ImagePackage.Name -like "*$_*" }
$Output += $ImagePackage.Name | Where-Object { $TaskSequence.Name -like "*$_*" }

But ultimately, your comparisons are failing because your logic is off. Are you looking for wildcard comparisons or equality?


Edit: got it working as anticipated with the following-

Foreach ($Task in $TaskSequence)
{
    Foreach ($Image in $ImagePackage)
    { 
        If ($Task.Name -like "*$($Image.Name)*")
        {
            $Task.Name
        }
        ElseIf ($Image.Name -like "*$($Task.Name)*")
        {
            $Image.Name
        }
    }
}

You were only outputting one side of the equation every time

I'd offer another solution. Using a RegEx match, and joining the opposite array as the match pattern.

$SiteCode = "PS1"
$SiteServer = "SRVSCCM01"
$TaskSequence = (Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_TaskSequencePackage -ComputerName $SiteServer)
$ImagePackage = (Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_ImagePackage -ComputerName $SiteServer)
$TaskPattern = ($TaskSequence.Name|%{[regex]::escape($_)}) -join '|'
$ImagePattern = ($ImagePackage.Name|%{[regex]::escape($_)}) -join '|'
$TaskSequence.Name|?{$_ -match $ImagePattern}
$ImagePackage.Name|?{$_ -match $TaskPattern}

Plus this avoids nested loops, which can get out of hand really fast. This will in fact get you 6 results, not 5 as you suggested, since 'Windows 10 Enterprise x64' is in both sets.

Edit: So if you are not seeing the results you expect I suspect that you have unaccounted for whitespace. Try trimming your strings when you generate the RegEx patterns, and see if that makes a difference.

$TaskPattern = ($TaskSequence.Name|%{[regex]::escape($_.Trim())}) -join '|'
$ImagePattern = ($ImagePackage.Name|%{[regex]::escape($_.Trim())}) -join '|'

i'm really studpid, sometimes you just need to look at your code from another angle, i was doing a

Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_TaskSequencePackage -ComputerName $SiteServer| where-object {$_.Name -like "*$($Task.Name)*"   }

inside the foreach loop, which defeats the purpose, so i just added another foreach, and now it all works perfectly as intended - thanks for all your sugestions

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM