Powershell使用Like Wildcard从2个容器中匹配输入

时间:2017-10-03 08:29:55

标签: powershell foreach match wildcard

对PowerShell来说是新手,我有点卡住了。

我想通过WMI获取所有SCCM Tasksequnece,并将它们与图像名称匹配

$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
  

$TaskSequence.name的输出

  Windows Server 2012 R2 X64
  Windows 10 Enterprise x64 USMT完整操作系统
  升级1703
  Windows 10 Enterprise x64 USMT硬链接
  Windows 7 Enterprise x64 zh-CN   Windows 10 Enterprise x64 1703 zh-CN   Windows Server 2016

     

$ImagePackage.name的输出
  Windows Server 2016 x64
  Windows 10 Enterprise x64

我试过这个foreach循环,但似乎无法让它工作,这篇文章提示,应该只有4个提示,3个Windows 10和1个Windows服务器2016

foreach ($TS in $TaskSequence ) { 

Get-WmiObject -Namespace "root\SMS\site_$($SiteCode)" -Class SMS_TaskSequencePackage -ComputerName $SiteServer | 
    ForEach-Object{
        Get-WmiObject SMS_ImagePackage -Namespace "root\SMS\site_$($SiteCode)" -ComputerName $SiteServer | Where-Object { $_.name -like "*$($TS.Name)*"} 
    }

}

1 个答案:

答案 0 :(得分:0)

修改 问题改变之后,这里有一个新代码:

$SiteCode = "SiteCode"
$SiteServer = "Server"
$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
            #I have no idea what you want to do with this object
        } 
    }
}