Windows Powershell 策略执行绕过

问题描述 投票:0回答:4

我一直在创建一个 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 脚本时,所有这些都会导致以下结果:

非常感谢所有帮助

windows powershell batch-file
4个回答
6
投票

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

来自

使用组策略管理执行策略部分(已添加重点):

您可以使用

Turn on Script Execution

组策略设置来管理企业中计算机的执行策略。 
组策略设置会覆盖所有范围内 PowerShell 中设置的执行策略。

另请参阅:

关于组策略设置 (Windows PowerShell) 和关于组策略设置 (PowerShell (Core) 7+),其中详细讨论了相关组策略设置


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

powershell.exe -executionpolicy unrestricted C:\multitool.ps1
如果有人有一个更干净的解决方案可以从bat文件运行脚本,我将不胜感激。


4
投票
尝试运行此代码,它帮助我解决了同样的问题

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
    

0
投票
在具有管理员权限的打开的 powershell 窗口中写入以下内容:

设置执行策略-执行策略远程签名

然后运行脚本:

.\你的脚本.ps1

© www.soinside.com 2019 - 2024. All rights reserved.