简体   繁体   English

Powershell Like Wildcard问题

[英]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 Server 2016

Windows 10 Enterprise x64 Windows 10企业版x64

For instance, I can't get 例如,我不能

Windows 10 Enterprise X64 1709 ? Windows 10企业版X64 1709

how come? 怎么会? I also get only 2 hints, it should give me 5 我也只有2条提示,应该给我5条提示

Output of $TaskSequence.name is $TaskSequence.name输出为

Windows Server 2012 R2 X64 Windows Server 2012 R2 X64

Windows 10 Enterprise x64 USMT Hardlinks Windows 10企业版x64 USMT硬链接

Windows 10 Enterprise x64 1703 en-US Windows 10 Enterprise x64 1703 zh-CN

Windows Server 2016 Windows Server 2016

Windows 10 Enterprise x64 USMT Full OS Windows 10企业版x64 USMT完整操作系统

Windows 10 Enterprise x64 Windows 10企业版x64

Windows 7 Windows 7的

Output of $ImagePackage.name is $ImagePackage.name输出是

Windows Server 2016 x64 Windows Server 2016 x64

Windows 10 Enterprise x64 Windows 10企业版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: 我假设您的WMI调用通过以下示例返回对象数组:

$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. 使用RegEx匹配,并将相反的数组作为匹配模式。

$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. 实际上这将为您提供6个结果,而不是您建议的5个结果,因为“ Windows 10 Enterprise x64”都在这两个集合中。

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. 生成RegEx模式时,请尝试修剪字符串,看看是否有区别。

$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 在foreach循环中,这违背了目的,所以我刚刚添加了另一个foreach,现在一切都按预期工作了-感谢您的支持

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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