PowerShell脚本药房部署时Windows Terminal加载设置失败求助
问题描述
我有一个用于药房新电脑配置IP的PowerShell脚本,脚本内容如下:
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Start-Process PowerShell -Verb RunAs \"-NoProfile -ExecutionPolicy Bypass -Command `\"cd '$pwd'; & '$PSCommandPath';`\"\"; exit; } $host.UI.RawUI.BackgroundColor = \"Black\" cls Write-Host -NoNewLine \"Wcisnij DOWOLNY by rozpoczac\"; $null = $Host.UI.RawUI.ReadKey(\"NoEcho,IncludeKeyDown\"); Write-Host \" \" Write-Host \" \" $host.UI.RawUI.BackgroundColor = \"White\" Disable-NetAdapterBinding -Name 'Ethernet' -ComponentID 'ms_tcpip6' $adapterV6 = Get-NetAdapterBinding -Name 'Ethernet' -ComponentID 'ms_tcpip6' for($i=0;$i -le 5;$i++){ Write-Host \"*\" -NoNewline } $hostname = hostname $currentIP = (Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null -and $_.IPEnabled -eq $true }).IPAddress[0] $firstThreeOctets = $currentIP -replace '\.\d+$', '' for($i=0;$i -le 5;$i++){ Write-Host \"*\" -NoNewline Start-Sleep -Milliseconds 1 } if ($hostname -like \"*S1\") { $desiredIP = \"$firstThreeOctets.10\" } else { $number = $hostname -replace '[^0-9]', '' $desiredIP = \"$firstThreeOctets.20$number\" } for($i=0;$i -le 5;$i++){ Write-Host \"*\" -NoNewline Start-Sleep -Milliseconds 1 } $adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPAddress -ne $null -and $_.IPEnabled -eq $true } $adapter.EnableStatic($desiredIP, \"255.255.255.0\") | Out-Null $dnsServers = \"8.8.8.8\", \"1.1.1.1\" $adapter.SetDNSServerSearchOrder($dnsServers) | Out-Null for($i=0;$i -le 5;$i++){ Write-Host \"*\" -NoNewline Start-Sleep -Milliseconds 2 } $gateway1 = \"$firstThreeOctets.254\" $gateway2 = \"$firstThreeOctets.253\" $gateways = @($gateway1, $gateway2) $adapter.SetGateways($gateways) | Out-Null for($i=0;$i -le 5;$i++){ Write-Host \"*\" -NoNewline Start-Sleep -Milliseconds 3 } $username = $env:UserName $gemini = $username.Substring(0, 6) $host.UI.RawUI.BackgroundColor = \"Black\" cls Write-Host \" mgawron\" -ForegroundColor DarkRed -NoNewline Write-Host \"; \" -NoNewline Write-Host \"Banklek\" -ForegroundColor Blue Write-Host \" \" Write-Host \" Apteka: \" -NoNewline if ($gemini -like \"Gemini\"){ $indexNawias = [regex]::Match($username, \"\\(\").Index $indexDo = $indexNawias - 6 $miasto = $username.Substring(6, $indexDo) Write-Host \"Gemini\" -ForegroundColor Red Write-Host \" Miasto: \" -NoNewline Write-Host \"$miasto\" -ForegroundColor Yellow }else{ Write-Host \"$username\" -ForegroundColor Cyan } Write-Host \" Nazwa komputera: \" -NoNewline Write-Host \"$hostname\" -ForegroundColor Green Write-Host \"Oryginalny adres: \" -NoNewline Write-Host \"$currentIP\" -ForegroundColor Green Write-Host \" Obecny adres: \" -NoNewline Write-Host \"$desiredIP\" -ForegroundColor Green Write-Host \" Maska: \" -NoNewline Write-Host \"255.255.255.0\" -ForegroundColor Green Write-Host \" Brama 1: \" -NoNewline Write-Host \"$gateway1\" -ForegroundColor Green Write-Host \" Brama 2: \" -NoNewline Write-Host \"$gateway2\" -ForegroundColor Green Write-Host \" DNSy: \" -NoNewline Write-Host \"$dnsServers\" -ForegroundColor Green Write-Host \" IPV6: \" -NoNewline if ($adapterV6.Enabled){ Write-Host \"Wlaczone\" -ForegroundColor Green }else{ Write-Host \"Wylaczone\" -ForegroundColor DarkRed } Read-Host
该脚本在本地及办公室电脑运行正常,但部署到药房环境后,启动时弹出以下错误提示:
Failed to load settings
Settings could not be loaded from file. Check for syntax errors, including trailing commas,
*Line 1, Column 1
Syntax error: value, object or array expected.
Temporarily using the Windows Terminal default settings.
随后脚本直接关闭无法执行。我曾在即将部署到药房的同一台电脑上测试过该脚本,当时运行正常,请求协助排查故障原因。
排查方向与解决方案
错误根源定位
这个报错并非来自PowerShell脚本本身,而是Windows Terminal的配置文件损坏。提示中的“Failed to load settings”明确指向终端的settings.json配置文件存在语法错误,导致终端启动失败,进而连带脚本无法执行。
具体排查与修复步骤
检查并修复终端配置文件:
- 打开文件资源管理器,导航到路径:
%LOCALAPPDATA%\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState - 找到
settings.json文件,用文本编辑器(如Notepad++)打开,检查第1行第1列是否存在语法问题,比如多余逗号、缺失括号/引号,或文件内容为空、乱码。 - 修复所有语法错误后保存,重新启动脚本。
- 打开文件资源管理器,导航到路径:
恢复终端默认配置:
如果无法修复配置文件,直接删除settings.json,Windows Terminal会在下次启动时自动生成全新的默认配置文件,解决加载失败问题。绕过Windows Terminal执行脚本:
尝试用传统PowerShell控制台运行脚本:按下Win+R,输入powershell回车,在弹出的控制台中手动执行脚本文件,验证是否还会出现报错。若正常运行,说明问题确实出在Windows Terminal配置上。修改脚本启动方式:
脚本中以管理员身份启动PowerShell的代码可能关联到Windows Terminal,可修改为明确调用传统控制台:Start-Process powershell.exe -Verb RunAs "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`""这样会直接启动独立的PowerShell控制台,避免触发Windows Terminal的配置加载错误。
排查环境变更:
药房电脑可能在测试后发生了系统更新、杀毒软件修改配置、用户误操作等情况,导致终端配置文件损坏,可对比测试时的系统状态确认变更点。
内容的提问来源于stack exchange,提问作者Max Gavron

