9009
9009

Reputation: 87

Error on calling the function

I am getting parse errors for the following code. We have multiple environments of the application with different versions legacy to the latest and we want to dynamically detect the environment - but for some reason I am getting parse errors like ParseException and Unexpected Token. Can someone please help? I am using Powershell 2.0.

Here is the code:

param(
    ## The name of the software to search for
    $DisplayName = "*Systems Manager"
)

Set-StrictMode -Off

## Get all the listed software in the Uninstall key
$keys = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall

## Get all of the properties from those items
$items = $keys | Foreach-Object { Get-ItemProperty $_.PsPath }

## For each of those items, display the DisplayName and Publisher
foreach ($item in $items) {
    if (($item.DisplayName) -and ($item.DisplayName -like $DisplayName)) {
        $MS = $item.DisplayVersion
    }
}

Function SyMan ($SM42) {
    #SM 1
    if ($MS –eq '11.3.0.23') {
        Write-Host “SM 1”
    #SM 2
    } elseif ($MS –eq '11.1.0.12') {
        Write-Host “SM 2”
    #SM 3
    } elseif ($MS –eq '11.0.0.26') {
        Write-Host “SM 3”
    #SM 4
    } elseif ($MS –eq '10.2.1.5') {
        Write-Host “SM 4”
    #SM 5
    } elseif ($MS –eq '10.1.1.2') {
        Write-Host “SM 5”
    #SM 6
    } else ($MS –eq '11.2.3.1') {
        Write-Host “SM 6”
    }
} #end of function 

SysMan $MS

Here is the error:

Unexpected token 'â?"eq '11.3.0.23'){ Write-Host â?oSM 1â?? #SM 1 }elseif($MS â?"eq' in expression or statement.
At C:\Tests\get_SMVersion.ps1:38 char:24
+ IF ($MS â?"eq '11.3.0 <<<< .23'){
+ CategoryInfo : ParserError: (â?"eq `'11.3.0....eif($MS â?"eq:String) [], ParseException
+ FullyQualifiedErrorId : UnexpectedToken

Upvotes: 1

Views: 155

Answers (3)

z47hr4s
z47hr4s

Reputation: 23

for such cases I would use switch.

switch -exact ( $MS )
{
    "11.3.0.23" { Write-Host “SM 1” } #SM 1
    "11.1.0.12" { Write-Host “SM 2” } #SM 2
    "11.0.0.26" { Write-Host “SM 3” } #SM 3
    "10.2.1.5" { Write-Host “SM 4” } #SM 4
    "10.1.1.2" { Write-Host “SM 5” } #SM 5
    "11.2.3.1" { Write-Host “SM 6” } #SM 6
}

check https://technet.microsoft.com/en-us/library/ff730937.aspx

Upvotes: 0

Adam Bertram
Adam Bertram

Reputation: 4198

The problem is your last if block.

} else ($MS –eq '11.2.3.1') {
    Write-Host “SM 6”
}

You can't include a condition with the else statement. If you want to check another condition you'd simply include another elseif statement.

} elseif ($MS –eq '11.2.3.1') {
    Write-Host “SM 6”
}

You should also leave with an else statement though to account for anything else so you could end that with something like this.

} else {
    Write-Host "Some other SM"
}

Upvotes: 2

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200523

The error message looks like your file was saved as UTF-8 without BOM. Open it with an editor like Notepad++ and save it as regular UTF-8 (with BOM) or as ANSI text. Also, you should avoid using typographic characters (like typographic quotes, em- and en-dashes, etc.) as syntax elements in scripts.

Upvotes: 1

Related Questions