组合查询(WQL 子选择查询 - Powershell - SCCM)

Combine queries (WQL subselect query - Powershell - SCCM)

现在我正在使用两个不同的查询并比较生成的对象,但是我更喜欢一个独特的查询来完成所有需要的事情,因为我喜欢直接在 SCCM 中使用它,而不仅仅是 PowerShell。

(第一个查询创建一个包含所有安装了特定 x64 软件的计算机的对象,第二个对象创建一个包含所有未安装特定 x86 软件的计算机的查询)

两个对象比较,我可以得到我需要的列表(两个对象中有哪些机器)

但是,我如何将这两个查询结合起来,使它们合二为一?

如:

所有具有 SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName = "SOFTWARE1" 且不具有 SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "SOFTWARE2"

的计算机
$SiteCode = "x"
$SiteServer = "x"

$query = @"

select *  from  SMS_R_System 
inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 
on SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceId = SMS_R_System.ResourceId 
where SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName = "SOFTWARE1"
"@

$postes_xx = (Get-WmiObject -namespace root\sms\site_$SiteCode -computer $SiteServer -query $query).SMS_R_SYSTEM.Name


$query = @"

select SMS_R_SYSTEM.ResourceID,SMS_R_SYSTEM.ResourceType,SMS_R_SYSTEM.Name,SMS_R_SYSTEM.SMSUniqueIdentifier,SMS_R_SYSTEM.ResourceDomainORWorkgroup,SMS_R_SYSTEM.Client
from SMS_R_System 
inner join SMS_G_System_COMPUTER_SYSTEM 
on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId 
where SMS_G_System_COMPUTER_SYSTEM.Name 
not in (select distinct SMS_G_System_COMPUTER_SYSTEM.Name 
       from  SMS_R_System
       inner join SMS_G_System_COMPUTER_SYSTEM
       on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId
       inner join SMS_G_System_ADD_REMOVE_PROGRAMS
       on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId
       where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "SOFTWARE2" )

"@


$postes_32x = Get-WmiObject -namespace root\sms\site_$SiteCode -computer $SiteServer -query $query | select -ExpandProperty name

Compare-Object $postes_xx $postes_x32 -IncludeEqual -ExcludeDifferent

您似乎可以只包含所有联接,然后将 where 语句与 and 结合起来。您必须调整 select 语句以包含您关心的列。

$query = @"

select *  from  SMS_R_System 
inner join SMS_G_System_ADD_REMOVE_PROGRAMS_64 
on SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceId = SMS_R_System.ResourceId 
inner join SMS_G_System_COMPUTER_SYSTEM 
on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId 
where (SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName = "SOFTWARE1")
and (SMS_G_System_COMPUTER_SYSTEM.Name 
not in (select distinct SMS_G_System_COMPUTER_SYSTEM.Name 
       from  SMS_R_System
       inner join SMS_G_System_COMPUTER_SYSTEM
       on SMS_G_System_COMPUTER_SYSTEM.ResourceID = SMS_R_System.ResourceId
       inner join SMS_G_System_ADD_REMOVE_PROGRAMS
       on SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID = SMS_R_System.ResourceId
       where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "SOFTWARE2" ))

"@

好像没必要用SMS_G_System_Computer_Systemclass。这是一个简单的 WQL 版本,应该可以满足您的要求。

select SMS_R_System.Name 
from SMS_R_System 
where SMS_R_System.ResourceID in 
(select SMS_G_System_ADD_REMOVE_PROGRAMS_64.ResourceID 
from SMS_G_System_ADD_REMOVE_PROGRAMS_64 
where SMS_G_System_ADD_REMOVE_PROGRAMS_64.DisplayName = "SOFTWARE1") 
and SMS_R_System.ResourceID not in 
(select SMS_G_System_ADD_REMOVE_PROGRAMS.ResourceID 
from SMS_G_System_ADD_REMOVE_PROGRAMS 
where SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName = "SOFTWARE2")

希望我的回答对您有所帮助,期待您的反馈。

此致, 雷