Alesia
Alesia

Reputation: 49

Powershell: Unexpected token

This must be something obvious, but I can't get this to work. I'm trying to transmit objects that should used to build $classKey which in turn lead to delete the desired software (amd64 or i386). Well, here the code:

$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()
            }
        }
    }
}

I rewrote code (added a filter) but it didn't help.

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 }

Here the error message:

Unexpected token 'GetWmiClass2' in expression or statement.
At line:2 char:31
+     $GetWmiClass $GetWmiClass2 <<<<  | % {
    + CategoryInfo          : ParserError: (GetWmiClass2:String) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken

Upvotes: 1

Views: 3654

Answers (1)

Duncan
Duncan

Reputation: 95742

What are you expecting $GetWmiClass $GetWmiClass2 to do? It isn't a valid Powershell expression.

You have a variable $GetWmiClass at the beginning of the statement, so it must be an expression as commands cannot being with variables. This is immediately followed by another variable with no intervening operator so you get an unexpected token error.

Did you perhaps mean:

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

To pass each of the values into the script block?

Upvotes: 4

Related Questions