PowerShell.exe -ExecutionPolicy 旁路 - Header 在脚本中

PowerShell.exe -ExecutionPolicy Bypass - Header in Script

我正在尝试轻松 ByPass PowerShell ExecutionPolicy。我意识到一个简单的解决方法是创建 runme.ps1script.ps1,在 runme.ps1 中我可以 Bypass ExecutionPolicy 并调用 script.ps1。有什么方法可以把它放在脚本的“header”中,并让它在 Bypassing ExecutionPolicy 时调用自己?

运行我.ps1:

PowerShell.exe -ExecutionPolicy Bypass -File "C:\tmp\script.ps1"

脚本.ps1:

Write-Host "Hello World"
PAUSE

我目前正在研究某种 if "flag" 或 "tmpfile" 逻辑并让脚本自行调用,但我想知道是否有 known/better 方式甚至可能的方式来实现这一点在我所有的脚本中都是 header,这样最终用户就可以在没有提示的情况下直接“运行 w/ powershell”。

欢迎补充详细说明 ExecutionPolicy,但让我们专注于问题。

关于 ExecutionPolicy 的讨论应该集中在“Security Stack Exchange”上,相关的 post 链接在这里:

https://security.stackexchange.com/questions/118553/whats-the-purpose-of-executionpolicy-settings-in-powershell-if-the-bypass

https://blog.netspi.com/15-ways-to-bypass-the-powershell-execution-policy/:

However, it’s important to understand that the setting was never meant to be a security control. Instead, it was intended to prevent administrators from shooting themselves in the foot.

您可以创建某种受信任的启动器(cmd 文件或 exe 文件),它将 运行 powershell 与 --ExecutionPolicy ByPass 标志。或者甚至您也可以将 double-click 操作的行为更改为 运行 PowerShell 始终带有 ByPass 策略标志。

但是,系统管理员可以在 MachinePolicy\ExecutionPolicyUserPolicy\ExecutionPolicy 中强化计算机上的设置,您将无法以正常方式覆盖它。

ExecutionPloicy配置为4+1级,优先级从高到低:

> Get-ExecutionPolicy -List

MachinePolicy (Group Policy)
   UserPolicy (Group Policy)
      Process (Configured using powershell -ExecutionPolicy flag for new process only)
  CurrentUser (User settings)
 LocalMachine (Computer settings)

当您 运行 PowerShell 带有 ByPass 标志时,您实际上设置了 Process 级别的 ExecutionPolicy,它覆盖了 CurrentUserLocalMachine 设置,但可以在 UserPolicyMachinePolicy 级别由本地或域组策略管理。


更好的方法是使用组策略配置用户策略以允许 运行 仅 AllSignedRemoteSigned 脚本,生成证书 New-SelfSignedCertificate -Type CodeSigning 100 年,部署它每台计算机使用 GPO 作为 Trusted Publisher,并使用 Set-AuthenticodeSignature 为您部署给用户的每个脚本签名。

TLDR;

PowerShell.exe -ExecutionPolicy Bypass -File "C:\circumvent\industry\standard.ps1" 2>&1>$null

我想分享脚本并且能够说“右键单击并 运行 w/powershell”这已经比我说“双击文件”的批处理脚本更糟糕了(人们总是得到那个!).

我想到了解决方案,因为我的主脚本中有一个 PAUSE 来读取控制台输出,我注意到在我的主脚本调用 script.ps1 之后,我收到了一个额外的 PAUSE 来自“main/parent”脚本的提示。这让我意识到,parent 脚本在调用 child 脚本后能够继续。因此,call nonexistent script and pipe output to null! & 继续愉快。

示例场景:

重新启动后,以下脚本不会通过“right-click,运行 w/ PowerShell”运行,我得到了标准的“执行策略提示”:

脚本.ps1

Write-Host "Calling Scripts? No Problem!"
PAUSE

重新启动后以下内容有效:

PowerShell.exe -ExecutionPolicy Bypass -File "C:\circumvent\industry\standard.ps1" 2>&1>$null
ECHO "This Script Won't Run Without Line 1" 
ECHO "I had fun to try to circumvent an industry standard" 
ECHO "I Learned a lot about PowerShell ExecutionPolicy"
C:\tmp\script.ps1
PAUSE

结果:

This Script Won't Run Without Line 1
I had fun to try to circumvent an industry standard
I Learned a lot about PowerShell ExecutionPolicy
Calling Scripts? No Problem!
Press Enter to continue...:

根据@BACON 的评论进行更新,这确实只能通过“上下文菜单”使用“运行 w/powershell”来实现。我尝试将“powershell”设置为 .ps1 的默认应用程序,但它不仅不起作用,而且上下文菜单删除了“运行 w/powershell”选项!

谢天谢地,最终用户将拥有默认设置and/or系统管理员已经知道如何解决了。

我最初没有测试的东西,但想知道这个解决方案到底有多“规避”是尝试在 header 中使用 PowerShell.exe -ExecutionPolicy Bypass。这导致脚本未 运行ning,因此必须为其分配 -File 但如果文件不存在则无效并允许脚本继续执行。