PS脚本不会在服务器上运行,在ISE中运行并粘贴到PS窗口中

时间:2014-02-14 00:48:53

标签: powershell

我有一个PS脚本我试图在Hyper-V 2012服务器上运行。我们的想法是建立一个基本菜单,启动几个可能的管理实用程序之一。当我将内容粘贴到打开的PowerShell窗口时,它工作正常。它也在ISE中运行。当我尝试从服务器上的.ps1运行它时,它会出现此错误:

PS C:\> C:\Mgmt\MgmtSel.ps1
At C:\Mgmt\MgmtSel.ps1:70 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Mgmt\MgmtSel.ps1:72 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
At C:\Mgmt\MgmtSel.ps1:73 char:1
+ }
+ ~
Unexpected token '}' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

我查看了引用的行,封闭的括号似乎对我来说很合适。我已经尝试过改变它们,它仍然无法运行,以及在ISE中打破它。任何指导或想法将不胜感激!!

这是虚线,70-73:

}
default { $global:xExitSession=$true;break }
}
}

以下是完整的脚本:

<######################################################################

Present user with options to launch one of several management programs
Save as MgmtSel.ps1 in c:\Mgmt

v 0.1   Basic Menu Presentation for different management tools

Written By Ricky Carleton
Based on code by Paul Westlake

######################################################################>
$xAppName    = ‘MgmtSel’
[BOOLEAN]$global:xExitSession=$false
function LoadMenuSystem(){
[INT]$xMenu1=0
[INT]$xMenu2=0
[BOOLEAN]$xValidSelection=$false
while ( $xMenu1 -lt 1 -or $xMenu1 -gt 4 ){
CLS
#… Present the Menu Options
Write-Host “`n`tLogin Management Tools Selection – Version 0.1`n” -ForegroundColor Magenta
Write-Host “`t`tPlease select the admin area you require`n” -Fore Cyan
Write-Host “`t`t`t1. sconfig console” -Fore Cyan
Write-Host “`t`t`t2. Corefig” -Fore Cyan
Write-Host “`t`t`t3. Hyper-V Mgmt” -Fore Cyan
Write-Host “`t`t`t4. Quit and exit`n” -Fore Cyan
#… Retrieve the response from the user
[int]$xMenu1 = Read-Host “`t`tEnter Menu Option Number”
if( $xMenu1 -lt 1 -or $xMenu1 -gt 4 ){
Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
}
}
Switch ($xMenu1){    #… User has selected a valid entry.. load next menu
1 { 
CLS
Write-Host “`t`tStarting sconfig`n” -Fore Cyan
start c:\Windows\System32\sconfig.cmd
}
2 {
CLS
Write-Host “`t`tStarting Corefig`n” -Fore Cyan
C:\Corefig\COREFIG.PS1
}
3 {
while ( $xMenu2 -lt 1 -or $xMenu2 -gt 3 ){
CLS
# Present the Menu Options
Write-Host “`n`tSelect the Hyper-V Mgmt Tool you would like to use`n” -Fore Cyan
Write-Host “`t`t`t1. PSHVM30” -Fore Green
Write-Host “`t`t`t2. ProHVM (not working yet)” -Fore Green
Write-Host “`t`t`t3. Go to Main Menu`n” -Fore Green
[int]$xMenu2 = Read-Host “`t`tEnter Menu Option Number”
if( $xMenu1 -lt 1 -or $xMenu1 -gt 3 ){
Write-Host “`tPlease select one of the options available.`n” -Fore Red;start-Sleep -Seconds 1
}
}
Switch ($xMenu2){
1 { 
CLS
Write-Host “`t`tStarting PSHVM`n” -Fore Cyan
call powershell -WindowStyle Hidden C:\PSHVM30\hyperv.ps1
}
2 { 
CLS
Write-Host “`t`tStarting Corefig (For now, later, ProHVM)`n” -Fore Cyan
start C:\Corefig\COREFIG.PS1
}
default { Write-Host “`n`tYou Selected Option 3 – Go to Main Menu`n” -Fore Yellow; break}
}
}
default { $global:xExitSession=$true;break }
}
}

LoadMenuSystem
If ($xExitSession){
Exit-PSSession    #… User quit & Exit
} Else {
C:\Mgmt\MgmtSel.ps1    #… Loop the function
}

1 个答案:

答案 0 :(得分:1)

尝试使用分号break终止所有;语句 - 看看是否有帮助。