运行.ps1文件时PowerShell自动关闭且执行策略无法修改求助
我编写了一份用于备份《霍格沃茨之遗》存档的PowerShell脚本,点击「用PowerShell运行」时窗口会自动关闭。通过Get-ExecutionPolicy -List查询发现CurrentUser的执行策略为Restricted,尝试修改时提示该设置被更具体范围的策略覆盖,注册表中ExecutionPolicy已设为Unrestricted,但仍无法正常运行脚本,恳请协助解决。
脚本内容
# 定义要复制的文件路径 $sourcePath = "C:\\Users\\Mr.Fox\\AppData\\Local\\Hogwarts Legacy\\Saved" # 定义文件备份目标路径 $destinationPath = "D:\\Games\\Saved HL" # 查找最新的备份文件夹编号 $latestFolder = Get-ChildItem $destinationPath | Where-Object { $_.PSIsContainer } | Sort-Object { [int]($_.Name -replace '^\D+') } -Descending | Select-Object -First 1 if ($latestFolder -eq $null) { $folderNumber = 1 } else { $folderNumber = [int]($latestFolder.Name -replace '^\D+') + 1 } # 创建新的备份文件夹 $newFolderName = "Backup_$("{0:D2}" -f $folderNumber)" $newFolderPath = Join-Path $destinationPath $newFolderName if (-not (Test-Path $newFolderPath)) { New-Item -ItemType Directory -Path $newFolderPath | Out-Null } # 写入备份日志 $logFile = Join-Path $destinationPath "backuplog.txt" $date = Get-Date -Format "yyyy-MM-dd HH:mm:ss" $logEntry = "$date - 备份已创建至文件夹 $newFolderPath" Add-Content $logFile $logEntry # 询问用户是否确认执行脚本 $confirm = Read-Host "确定要执行备份脚本吗?输入'ja'继续" Write-Host "确认输入: $confirm" if ($confirm -eq "ja") { # 复制存档文件到新备份文件夹 Copy-Item $sourcePath $newFolderPath -Recurse -Force Write-Host "备份流程已完成,日志已写入日志文件。" } else { Write-Host "脚本执行已取消。" } # 恢复PowerShell执行策略至原始设置 if ((Get-ExecutionPolicy -Scope CurrentUser) -eq "RemoteSigned") { Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Restricted } Read-Host "按任意键继续..."
执行策略查询结果
PS C:\\WINDOWS\\system32> Get-ExecutionPolicy -List Scope ExecutionPolicy ----- --------------- MachinePolicy Undefined UserPolicy Undefined Process Undefined CurrentUser Restricted LocalMachine RemoteSigned
修改执行策略时的报错信息
执行策略更改
执行策略有助于保护你免受不信任脚本的威胁。更改执行策略可能会使你面临安全风险描述。是否要更改执行策略?
[Y] 是 [A] 全部是 [N] 否 [L] 全部否 [S] 暂停 [?] 帮助 (默认值为“N”): y
Set-ExecutionPolicy : Windows PowerShell 已成功更新你的执行策略,但该设置被更具体范围定义的策略覆盖。由于存在覆盖,你的Shell将保留当前有效的执行策略Restricted。请输入"Get-ExecutionPolicy -List"查看执行策略设置。有关详细信息,请参阅"Get-Help Set-ExecutionPolicy"。
位于行:1 字符:1
- Set-ExecutionPolicy Unrestricted
+ CategoryInfo : PermissionDenied: (:) [Set-ExecutionPolicy], SecurityException + FullyQualifiedErrorId : ExecutionPolicyOverride,Microsoft.PowerShell.Commands.SetExecutionPolicyCommand
解决方案
1. 临时绕过执行策略运行脚本
不用修改全局执行策略,直接在脚本所在文件夹打开PowerShell窗口,执行以下命令:
powershell -ExecutionPolicy Bypass -File .\你的脚本文件名.ps1
该命令会临时为当前进程设置Bypass策略,允许脚本运行,不会影响全局设置。
2. 修改CurrentUser级别的执行策略
打开管理员权限的PowerShell,执行以下命令:
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
CurrentUser的策略优先级高于LocalMachine,之前报错是因为默认修改的是LocalMachine范围,但CurrentUser的Restricted策略优先级更高,因此需要明确指定Scope为CurrentUser。
3. 检查组策略限制(若上述方法无效)
按Win+R输入gpedit.msc打开组策略编辑器,依次导航到:计算机配置 → 管理模板 → Windows组件 → Windows PowerShell
找到「启用脚本执行」选项,将其设置为「允许本地脚本和远程签名脚本」,保存后重启PowerShell即可。
4. 修复脚本中的执行策略恢复逻辑
原脚本最后一段的恢复逻辑存在问题:只有当CurrentUser的策略是RemoteSigned时才会改回Restricted,但如果当前策略本来就是Restricted,这段代码不会触发。不过这不是脚本无法运行的根本原因,解决执行策略问题后可按需调整。
内容的提问来源于stack exchange,提问作者Xmaster1004

