1
這一定是顯而易見的,但我無法得到它的工作。我試圖傳遞應該用來構建$ classKey的對象,這又導致刪除所需的軟件(amd64或i386)。那麼,這裏的代碼:Powershell:意外的令牌
$name = @("softwareName1", "softwareName2", "softwareName3")
$strComputer = "localhost"
$getWmiClass = get-wmiobject -class "SampleProductsList32" -namespace "root\DEFAULT" -computername $strComputer | select-object IdentifyingNumber, Displayname, DisplayVersion
$getWmiClass2 = get-wmiobject -class "SampleProductsList" -namespace "root\DEFAULT" -computername $strComputer | select-object IdentifyingNumber, Displayname, DisplayVersion
foreach ($AppDisplayName in $name) {
$GetWmiClass $GetWmiClass2 | % {
% ($DispName in $_) {
if($DispName.DisplayName -eq $AppDisplayName) {
$classKey += "IdentifyingNumber=`"`{${DispName.IdentifyingNumber}`}`",Name=`"${DispName.Displayname}`",version=`"${DisplayVersion}`""
([wmi]"\\$server\root\cimv2:Win32_Product.$classKey").uninstall()
}
}
}
}
我重寫了代碼(添加了一個過濾器),但它沒有幫助。
filter GetApps {
foreach ($DispName in $_) {
if($DispName.DisplayName -eq $AppDisplayName) {
$classKey += "IdentifyingNumber=`"`{${DispName.IdentifyingNumber}`}`",Name=`"${DispName.Displayname}`",version=`"${DisplayVersion}`""
([wmi]"\\$server\root\cimv2:Win32_Product.$classKey").uninstall()
}
}
}
$name = @("softwareName1", "softwareName2", "softwareName3")
$strComputer = "localhost"
$getWmiClass = get-wmiobject -class "SampleProductsList32" -namespace "root\DEFAULT" -computername $strComputer | select-object IdentifyingNumber, Displayname, DisplayVersion
$getWmiClass2 = get-wmiobject -class "SampleProductsList" -namespace "root\DEFAULT" -computername $strComputer | select-object IdentifyingNumber, Displayname, DisplayVersion
foreach ($AppDisplayName in $name) { $GetWmiClass $GetWmiClass2 | GetApps }
這裏的錯誤消息:
Unexpected token 'GetWmiClass2' in expression or statement.
At line:2 char:31
+ $GetWmiClass $GetWmiClass2 <<<< | % {
+ CategoryInfo : ParserError: (GetWmiClass2:String) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnexpectedToken
是的,我做到了。你是對的。正如我想的那樣,很簡單。謝謝! – Alesia