我一直在创建一个 powershell 脚本来帮助我在不同用户的 PC 上自动执行任务,我遇到了一个问题,我必须手动允许脚本在每台 PC 上运行,然后才能执行它。
我尝试使用我找到的各种解决方案,但到目前为止似乎都不起作用。
我尝试过作为批处理文件的解决方案(理想情况下,我想让批处理文件下载脚本(已经对此进行排序),然后打开 powershell 脚本并成功绕过此问题):
powershell.exe -executionpolicy bypass -windowstyle hidden -noninteractive -nologo -file "multitool.ps1"
powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"
@echo off
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "Path" /d "c:\windows\system32\windowspowershell\v1.0\powershell.exe"
reg add HKLM\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell /v "ExecutionPolicy" /d "unrestricted"
@echo off
regedit /s file.reg
其中 file.reg 包含以下内容:
[hkey_local_machine\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell]
"Path"="c:\windows\system32\windowspowershell\v1.0\powershell.exe"
"ExecutionPolicy"="unrestricted"
运行 powershell 脚本时,所有这些都会导致以下结果:
非常感谢所有帮助
powershell.exe -executionpolicy bypass ...
原则上是临时策略覆盖的正确方法,但作为错误消息指向的概念帮助主题,about_Execution_Policies,指出,如果通过组设置执行策略策略(而不是通过Set-ExecutionPolicy
),它们不能通过其他方式覆盖,包括通过命令行:
来自使用组策略管理执行策略部分(已添加重点):
您可以使用另请参阅:
Turn on Script Execution
组策略设置来管理企业中计算机的执行策略。组策略设置会覆盖所有范围内 PowerShell 中设置的执行策略。
关于组策略设置 (Windows PowerShell) 和关于组策略设置 (PowerShell (Core) 7+),其中详细讨论了相关组策略设置。
powershell.exe -executionpolicy unrestricted C:\multitool.ps1
如果有人有一个更干净的解决方案可以从bat文件运行脚本,我将不胜感激。
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
设置执行策略-执行策略远程签名然后运行脚本:
.\你的脚本.ps1