无法传递变量以获取wmi对象查询

时间:2017-12-21 19:09:50

标签: powershell sccm

希望你能帮我解决我的问题。我尝试将包列表导入$ list变量,并将DP服务器列表导入名为$ DP的变量。我使用foreach和write-host并确认变量中的所有项都显示正常,但是当我使用get-wmi对象中的变量作为参数来过滤$ i.packageid时,它是空的/ null。请告知如何解决此问题。

示例代码

$list=import-csv -Path 'C:\Users\Administrator\desktop\SCCM\packagelist.csv'
$DP="SCCMDP1","SCCMDP2"


foreach ($i in $list)
{

write-host "Item is $($i)"   # showing ok
write-host "package is $($i.package)" # showing ok
write-host "packageid is $($i.packageid)" # showing ok


get-wmiobject -namespace root\sms\site_v01 -class sms_distributiondpstatus -Filter {packageid like "$i.packageid"}|select name

1 个答案:

答案 0 :(得分:0)

您在参数中调用变量的方式与Write-Host次调用的调用方式不同。如果字符串$()中没有子表达式,则无法访问对象属性。

$list = import-csv -Path 'C:\Users\Administrator\desktop\SCCM\packagelist.csv'
$DP = 'SCCMDP1','SCCMDP2'

foreach ($i in $list)
{
    write-host "Item is $i"   # showing ok
    write-host "package is $($i.package)" # showing ok
    write-host "packageid is $($i.packageid)" # showing ok

    Params = @{
        Namespace = 'root\sms\site_v01'
        Class = 'sms_DistributionDPStatus'
        Filter = "PackageId LIKE '%$($i.PackageId)%'"
    }
    Get-WmiObject @Params | Select-Object -Property Name
}

如果您不打算使用通配符,则无需使用LIKE WQL比较。