~~NOTOC~~
===== q105 在 Windows 上执行 Powershell 脚本报错 =====
如果运行脚本时遇到这样的错误:
无法加载文件,因为在此系统上禁止运行脚本。
解决方法(以管理员身份运行 PowerShell):
# 查看当前执行策略
Get-ExecutionPolicy
# 设置为允许运行本地脚本
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
# 或者临时绕过(仅当前会话)
powershell -ExecutionPolicy Bypass -File your_script.ps1
==== 具体现象 ====
> .\get_hdwkey.ps1
.\get_hdwkey.ps1 : 无法加载文件 C:\Users\lxkt\Desktop\PWmatLICENSE_win\get_hdwkey.ps1,因为在此系统上禁止运行脚本。有
关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Policies。
所在位置 行:1 字符: 1
+ .\get_hdwkey.ps1
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : SecurityError: (:) [],PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
PS C:\Users\lxkt\Desktop\PWmatLICENSE_win>
==== 解决方案 ====
方法1: 更改执行策略(推荐, 一劳永逸)
以管理员身份运行 PowerShell, 然后执行:
# 设置为允许运行本地脚本
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser # 需要输入 ''Y'' 确认
或
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force
方法2: 临时绕过(仅本条命令)
powershell -ExecutionPolicy Bypass -File .\get_hdwkey.ps1
或者
powershell -ExecutionPolicy Unrestricted -File .\get_hdwkey.ps1