编辑 -- 添加一些信息
其中一种可能的方法完全绕过安全性并像这样启动您的脚本(绕过强加的 GPO,请参阅本文底部):
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\path_to_file\test_new.ps1""'}"
在 Windows 2003 域和受限的 powershell 中测试。
第一次编辑 - 由于评论说不可能,我正在显示我运行脚本的生产系统的屏幕截图:
在开启旁路的情况下启动 powershell 并执行脚本:
在不开启 Bypass 的情况下启动 powershell 并执行脚本:
系统信息:
第二次编辑 - 由于评论@AnsgarWiechers。
(因为我们谈论的是 Microsoft,所以通常有一种方法可以绕过强加的安全措施。)
引用评论:
...OP 具有通过 GROUP POLICY 强制执行的执行策略(范围
MachinePolicy 和/或 UserPolicy)。哪个不能被绕过...'
ExecutionPolicy 键由位于注册表配置单元的组策略对象推送:HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell。
当您将值从任何更改时,请参阅Possible values for execution policies) 到bypass 的更多信息。然后你可以绕过GPO强加。我敢肯定还有其他方法可以做到这一点。有关更多想法,您可以查看resource。我相信更有创意的人可以找到一些其他方法来绕过 GPO。
也就是说,对于永久解决方案,最好重复使用。据推测,使用密钥可能更安全,即使您也将取决于其他因素。
第三次修改 - 证明可以规避 GPO 政策(如果您有足够的权限这样做)
我终于找到了一些时间来探索这些可能性。对于Windows 2003,无法通过编辑组策略gpedit.msc来更改本地策略,必须在域级别完成(MS不支持Windows 2003,目前为EOL)
在Windows 2008 上是不同的故事。
如果您更改本地策略,您可以轻松地将策略更改为 AllSigned,如下所示:
PS C:\Windows\system32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy AllSigned
UserPolicy AllSigned
Process Bypass
CurrentUser Undefined
LocalMachine AllSigned
如果您随后通过上述参数执行脚本:-NoProfile -ExecutionPolicy Bypass,您将收到以下消息,因此您的 GPO 策略需要对脚本进行签名:
File E:\t\powershell\get_local_admins_computer.ps1 cannot be loaded. The file E
:\t\powershell\get_local_admins_computer.ps1 is not digitally signed. The scrip
t will not execute on the system. Please see "get-help about_signing" for more
details..
+ CategoryInfo : NotSpecified: (:) [], ParentContainsErrorRecordE
xception
+ FullyQualifiedErrorId : RuntimeException
您还可以检查注册表设置以确保(也将显示您希望在运行脚本后返回的值):
C:\Windows\system32>REG QUERY HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Win
dows\PowerShell
HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell
EnableScripts REG_DWORD 0x1
ExecutionPolicy REG_SZ AllSigned
如果您有正确的权限,可以通过以下方式轻松规避此问题(这会将 ExecutionPolicy 设置为“绕过”):
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell" /f /v "ExecutionPolicy" /t REG_SZ /d "bypass"
/v ... ValueName
/t ... type
/d ... data
/f ... force (overwrites current value)
然后,如果您检查执行策略,您会发现确实有变化:
PS C:\Windows\system32> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy Bypass
UserPolicy AllSigned
Process Bypass
CurrentUser Undefined
LocalMachine AllSigned
如果您随后运行该脚本,它将运行良好。
完成后,您可以通过以下方式返回之前的值:
reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\PowerShell" /f /v "ExecutionPolicy" /t REG_SZ /d "AllSigned"
第四次编辑 - 结论
我想清楚,所以我要添加结论,因为下面的 cmets 可能会导致错误的假设。
这是参数'-NoProfile -ExecutionPolicy Bypass 将使用的最大安全性:
PS C:\> Get-ExecutionPolicy -List
Scope ExecutionPolicy
----- ---------------
MachinePolicy RemoteSigned
UserPolicy AllSigned
Process AllSigned
CurrentUser AllSigned
LocalMachine AllSigned
唯一的情况是你需要一些方法来解决它或拥有一个签名的密钥是当
MachinePolicy 设置为 AllSigned。最佳解决方案是使用密钥。如果由于某种原因不能,您可以使用上述步骤更改注册表中的值或尝试上面已经链接的15 ways to bypass the powershell execution policy 之一。