Windows Server 2012 R2上PowerShell脚本执行报错求助
解决PowerShell 4.0中IIS脚本的字符串终止符错误
这个错误提示The string is missing the terminator: '.看起来像是有未闭合的单引号,但你提到相同脚本在其他服务器能正常运行,说明问题大概率出在PowerShell 4.0的语法兼容性或者脚本中混入的隐形字符上。下面是针对性的解决步骤:
1. 先排查隐形字符问题
脚本复制粘贴过程中很容易带入非打印的隐形字符(比如全角空格、特殊换行符),这些字符会干扰PowerShell的解析逻辑:
- 把脚本复制到Notepad++这类编辑器,开启「显示所有字符」功能(视图→显示符号→显示所有字符)
- 检查每行末尾、引号周围有没有奇怪的符号,删除后重新保存为UTF-8无BOM格式的脚本文件
2. 调整语法适配PowerShell 4.0
PowerShell 4.0对语法的解析比高版本更严格,你可以对脚本做以下修改:
替换?为Where-Object全称
在PS4.0中,管道里用?(Where-Object的别名)偶尔会出现解析歧义,换成全称更稳妥:
# 原写法 if( !((Get-WebConfiguration //staticcontent).collection | ? {$_.fileextension -eq '.woff'}) ) # 修改后 if( !((Get-WebConfiguration //staticcontent).collection | Where-Object {$_.fileextension -eq '.woff'}) )
拆分长行避免解析压力
把原本挤在一行的判断和执行语句拆成多行,减少PowerShell解析长字符串的出错概率:
# 原单行写法 if( !((Get-WebConfiguration //staticcontent).collection | Where-Object {$_.fileextension -eq '.woff'}) ) { add-webconfigurationproperty //staticContent -name collection -value @{fileExtension='.woff'; mimeType='application/font-woff'} } # 修改为多行 $woffExists = (Get-WebConfiguration //staticcontent).collection | Where-Object {$_.fileextension -eq '.woff'} if (-not $woffExists) { Add-WebConfigurationProperty //staticContent -name collection -value @{fileExtension='.woff'; mimeType='application/font-woff'} }
改用完整的XPath路径
PS4.0对简化的XPath写法支持可能不佳,把//staticcontent换成完整路径system.webServer/staticContent:
$woffExists = (Get-WebConfiguration 'system.webServer/staticContent').collection | Where-Object {$_.fileextension -eq '.woff'}
3. 完整修改后的兼容脚本
这里是调整后适配PS4.0的完整脚本:
Import-Module "WebAdministration" # 创建应用池 if (-not (Test-Path IIS:\AppPools\MgmtSite)) { New-Item IIS:\AppPools\MgmtSite -force } # 创建站点或绑定http if (Test-Path 'iis:\Sites\Mgmt Site') { New-WebBinding -name 'Mgmt Site' -Protocol http -IPAddress '*' -Port 80 } else { New-Item 'iis:\Sites\Mgmt Site' -bindings @{protocol='http';bindingInformation='*:80:'} -physicalPath 'C:\Program Files\AP\MgmtServices' -force } # 设置站点应用池 Set-ItemProperty 'iis:\Sites\Mgmt Site' -name applicationPool -value MgmtSite # 获取站点ID $newId = (Get-ChildItem IIS:\Sites | Where-Object {$_.Name -eq 'Mgmt Site'}).Id Write-Host -Object "Mgmt Site ID is: $newId " # 绑定https New-WebBinding -name 'Mgmt Site' -Protocol https -IPAddress '*' -Port 443 -SslFlags 0 # 启动站点 Start-WebSite 'Mgmt Site' # 添加woff MIME类型 $woffExists = (Get-WebConfiguration 'system.webServer/staticContent').collection | Where-Object {$_.fileextension -eq '.woff'} if (-not $woffExists) { Add-WebConfigurationProperty 'system.webServer/staticContent' -name collection -value @{fileExtension='.woff'; mimeType='application/font-woff'} } # 添加woff2 MIME类型 $woff2Exists = (Get-WebConfiguration 'system.webServer/staticContent').collection | Where-Object {$_.fileextension -eq '.woff2'} if (-not $woff2Exists) { Add-WebConfigurationProperty 'system.webServer/staticContent' -name collection -value @{fileExtension='.woff2'; mimeType='application/font-woff2'} } # 添加webm MIME类型 $webmExists = (Get-WebConfiguration 'system.webServer/staticContent').collection | Where-Object {$_.fileextension -eq '.webm'} if (-not $webmExists) { Add-WebConfigurationProperty 'system.webServer/staticContent' -name collection -value @{fileExtension='.webm'; mimeType='video/webm'} }
4. 执行验证
修改后,右键以管理员身份运行PowerShell执行脚本,应该就能避免字符串终止符的错误了。如果还是有问题,可以逐行执行脚本,定位具体出错的行再进一步排查。
内容的提问来源于stack exchange,提问作者saravana
相关产品推荐
相关产品推荐

