调用函数时出错
Error on calling the function
我收到以下代码的解析错误。我们有多个应用程序环境,从旧版本到最新版本都不同,我们希望动态检测环境——但由于某种原因,我遇到了解析错误,如 ParseException 和意外令牌。有人可以帮忙吗?我正在使用 Powershell 2.0.
代码如下:
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
这里是错误:
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
错误消息看起来您的文件已保存为不带 BOM 的 UTF-8。使用 Notepad++ 等编辑器打开它,并将其保存为常规 UTF-8(带 BOM)或 ANSI 文本。此外,您应该避免在脚本中使用印刷字符(如印刷引号、em- 和 en-dashes 等)作为语法元素。
问题是你最后一个 if 块。
} else ($MS –eq '11.2.3.1') {
Write-Host “SM 6”
}
您不能在 else
语句中包含条件。如果你想检查另一个条件,你只需包含另一个 elseif
语句。
} elseif ($MS –eq '11.2.3.1') {
Write-Host “SM 6”
}
你还应该留下一个 else
声明来说明其他任何事情,这样你就可以用这样的东西结束它。
} else {
Write-Host "Some other SM"
}
对于这种情况,我会使用 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
}
我收到以下代码的解析错误。我们有多个应用程序环境,从旧版本到最新版本都不同,我们希望动态检测环境——但由于某种原因,我遇到了解析错误,如 ParseException 和意外令牌。有人可以帮忙吗?我正在使用 Powershell 2.0.
代码如下:
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
这里是错误:
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
错误消息看起来您的文件已保存为不带 BOM 的 UTF-8。使用 Notepad++ 等编辑器打开它,并将其保存为常规 UTF-8(带 BOM)或 ANSI 文本。此外,您应该避免在脚本中使用印刷字符(如印刷引号、em- 和 en-dashes 等)作为语法元素。
问题是你最后一个 if 块。
} else ($MS –eq '11.2.3.1') {
Write-Host “SM 6”
}
您不能在 else
语句中包含条件。如果你想检查另一个条件,你只需包含另一个 elseif
语句。
} elseif ($MS –eq '11.2.3.1') {
Write-Host “SM 6”
}
你还应该留下一个 else
声明来说明其他任何事情,这样你就可以用这样的东西结束它。
} else {
Write-Host "Some other SM"
}
对于这种情况,我会使用 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
}