Windows Powershell 策略执行绕过

KR4*_*STL 7 windows powershell batch-file

我一直在创建一个 powershell 脚本来帮助我在不同用户的 PC 上自动执行任务,我遇到了一个问题,我必须手动允许脚本在每台 PC 上运行,然后才能执行它。

我尝试使用我找到的各种解决方案,但到目前为止似乎都不起作用。

我尝试过作为批处理文件的解决方案(理想情况下,我希望批处理文件下载脚本(已经对此进行排序),然后打开 powershell 脚本并成功绕过此问题):

powershell.exe -executionpolicy bypass -windowstyle hidden -noninteractive -nologo -file "multitool.ps1"
Run Code Online (Sandbox Code Playgroud)
powershell -command "& {Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force}"
Run Code Online (Sandbox Code Playgroud)
    @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"
Run Code Online (Sandbox Code Playgroud)
@echo off
regedit /s file.reg
Run Code Online (Sandbox Code Playgroud)

其中 file.reg 包含以下内容:

[hkey_local_machine\system32\windows\microsoft\powershell\1\shellids\microsoft.powershell] 
"Path"="c:\windows\system32\windowspowershell\v1.0\powershell.exe"
"ExecutionPolicy"="unrestricted"
Run Code Online (Sandbox Code Playgroud)

运行 powershell 脚本时,所有这些都会导致以下结果:截屏

非常感谢所有帮助

mkl*_*nt0 6

powershell.exe -executionpolicy bypass ...原则上是临时策略覆盖的正确方法,但正如错误消息指向的概念帮助主题about_Execution_Policies所指出的,如果执行策略是通过组策略(而不是通过Set-ExecutionPolicy)设置的,则不能通过以下方式覆盖它们其他方式,包括在命令行上

“使用组策略来管理执行策略”部分(添加了重点):

您可以使用Turn on Script Execution组策略设置来管理企业中计算机的执行策略。组策略设置会覆盖所有范围内 PowerShell 中设置的执行策略。

另请参阅:关于组策略设置(Windows PowerShell) 和关于组策略设置(PowerShell (Core) 7+),其中详细讨论了相关的组策略设置。


KR4*_*STL 5

我找到的最接近的解决方案是以管理员身份在 powershell 中运行以下行,它将执行脚本并绕过限制:

powershell.exe -executionpolicy unrestricted C:\multitool.ps1
Run Code Online (Sandbox Code Playgroud)

如果有人有一个更干净的解决方案可以从 bat 文件运行脚本,我将不胜感激。