2014-03-02 46 views
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 

回答

4

什麼是你期待$GetWmiClass $GetWmiClass2辦?這不是有效的Powershell表達式。

你必須在聲明的開頭變量$GetWmiClass,所以它必須是命令的表達式不能是與變量。緊接着是另一個沒有中介運算符的變量,因此您會收到意外的令牌錯誤。

你或許意味着:

@($GetWmiClass, $GetWmiClass2) | % { ... } 

爲每個值傳遞到腳本塊?

+0

是的,我做到了。你是對的。正如我想的那樣,很簡單。謝謝! – Alesia