PowerShell运行部署组代理安装脚本报语法错误如何解决
故障现象
- 运行环境:Win7 虚拟机
- 操作行为:复制Azure DevOps部署组板块提供的PowerShell代理安装脚本,直接粘贴到PowerShell窗口执行
- 触发报错:PowerShell解析阶段直接抛出异常,核心报错信息如下:
At line:1 char:179 + ... Current() ).IsInRole( [Security.Principal.WindowsBuiltInRole] “Adminis ... + ~ Missing ')' in method call. At line:1 char:180 + ... nRole( [Security.Principal.WindowsBuiltInRole] “Administrator?){ throw ... + ~~~~~~~~~~~~~~ Unexpected token '“Administrator?' in expression or statement. At line:1 char:180 + ... nRole( [Security.Principal.WindowsBuiltInRole] “Administrator?){ throw ... + ~~~~~~~~~~~~~~ Missing closing ')' after expression in 'If' statement. At line:1 char:194 + ... Role( [Security.Principal.WindowsBuiltInRole] “Administrator?){ throw ... + ~ Unexpected token ')' in expression or statement. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MissingEndParenthesisInMethodCall
多次重复复制粘贴运行,均触发相同解析错误。
故障根因
报错属于PowerShell语法解析错误,核心原因是复制得到的脚本中,管理员权限判断语句里包裹Administrator的引号是中文全角排版弯引号(“”),不是PowerShell可识别的标准半角直双引号("")。Win7环境下的低版本PowerShell无法识别这类全角符号,直接将其判定为非法字符,截断了方法参数解析流程,最终抛出括号缺失、意外标记的报错。
这类问题通常是网页端渲染脚本时,自动将代码里的半角直引号替换为排版用的弯引号导致的,复制时会将非法符号一并带入。
解决方案
按以下步骤操作即可修复问题:
- 修正非法引号
找到脚本中判断管理员权限的代码行,将“Administrator”两侧的全角弯引号替换为标准半角双引号,修正后的完整可运行脚本如下(执行前将脚本里的MY_GROUP_NAME、MY_ORG_NAME、MY_PROJ_NAME替换为实际的部署组、组织、项目名称):$ErrorActionPreference="Stop"; If(-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent() ).IsInRole( [Security.Principal.WindowsBuiltInRole] "Administrator")){ throw "Run command in an administrator PowerShell prompt"}; If($PSVersionTable.PSVersion -lt (New-Object System.Version("3.0"))){ throw "The minimum version of Windows PowerShell that is required by the script (3.0) does not match the currently running version of Windows PowerShell." }; If(-NOT (Test-Path $env:SystemDrive\'azagent')){mkdir $env:SystemDrive\'azagent'}; cd $env:SystemDrive\'azagent'; for($i=1; $i -lt 100; $i++){$destFolder="A"+$i.ToString(); if(-NOT (Test-Path ($destFolder))){mkdir $destFolder;cd $destFolder;break;}}; $agentZip="$PWD\agent.zip"; $DefaultProxy=[System.Net.WebRequest]::DefaultWebProxy;$securityProtocol=@();$securityProtocol+=[Net.ServicePointManager]::SecurityProtocol; $securityProtocol+=[Net.SecurityProtocolType]::Tls12;[Net.ServicePointManager]::SecurityProtocol=$securityProtocol; $WebClient=New-Object Net.WebClient; $Uri='https://vstsagentpackage.azureedge.net/agent/2.204.0/vsts-agent-win-x64-2.204.0.zip'; if($DefaultProxy -and (-not $DefaultProxy.IsBypassed($Uri))){$WebClient.Proxy= New-Object Net.WebProxy($DefaultProxy.GetProxy($Uri).OriginalString, $True);}; $WebClient.DownloadFile($Uri, $agentZip); Add-Type -AssemblyName System.IO.Compression.FileSystem; [System.IO.Compression.ZipFile]::ExtractToDirectory( $agentZip, "$PWD"); .\config.cmd --deploymentgroup --deploymentgroupname "MY_GROUP_NAME" --agent $env:COMPUTERNAME --runasservice --work '_work' --url 'https://dev.azure.com/MY_ORG_NAME/' --projectname 'MY_PROJ_NAME'; Remove-Item $agentZip; - 使用管理员权限启动PowerShell
右键点击PowerShell快捷方式,选择「以管理员身份运行」后再执行脚本,普通权限窗口运行会触发脚本内置的权限校验拦截。 - 校验PowerShell版本
在PowerShell窗口执行$PSVersionTable.PSVersion查看版本,若主版本号低于3,先安装对应版本的Windows Management Framework更新,满足脚本最低版本要求后再操作。 - 避免超长脚本直接粘贴运行
Win7自带的PowerShell宿主对超长单行内容的粘贴兼容性较差,可将修正完成的脚本保存为本地.ps1后缀的文件,在管理员PowerShell中切换到脚本存放目录,执行.\你的脚本文件名.ps1运行,减少粘贴解析异常。
内容的提问来源于stack exchange,提问作者han shih
相关产品推荐
相关产品推荐

