我试图运行一个cmd文件,从powershell调用cmd.exe
脚本,我得到以下错误:
无法加载
Management_Install.ps1
,因为在此系统上禁用了脚本的执行。
我跑了
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
当我从Get-ExecutionPolicy
运行powershell时,我得到了Unrestricted
。
PS C:\Users\Administrator\> Get-ExecutionPolicy
Unrestricted
C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\> powershell .\Management_Install.ps1 1
WARNING: Running x86 PowerShell...
无法加载文件
C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1
,因为在此系统上禁用了脚本的执行。有关详细信息,请参阅“get-help about_signing
”。在线:1个字符:25
.\Management_Install.ps1
<<<< 1 CategoryInfo:NotSpecified:(:) [],PSSecurityException FullyQualifiedErrorId:RuntimeException
C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\> PAUSE
Press any key to continue . . .
该系统是Windows Server 2008R2。
我究竟做错了什么?
如果您正在使用Windows Server 2008 R2,那么有一个x64和x86版本的PowerShell,这两个版本都必须设置其执行策略。您是否在两台主机上设置了执行策略?
作为管理员,您可以通过在PowerShell窗口中键入以下内容来设置执行策略:
Set-ExecutionPolicy RemoteSigned
有关更多信息,请参阅Using the Set-ExecutionPolicy Cmdlet。
我正在使用Windows 10,无法运行任何命令。给我一些线索的唯一命令是这样的:
[64]
- 以管理员身份打开C:\ Windows \ SysWOW64 \ cmd.exe
- 运行命令> powershell Set-ExecutionPolicy Unrestricted
但这没效果。它很有限。可能是Windows10的新安全策略。我有这个错误:
Set-ExecutionPolicy:Windows PowerShell已成功更新执行策略,但该设置被更具体范围内定义的策略覆盖。由于覆盖,您的shell将保留其当前有效的执行策略...
所以我找到了另一种方式(解决方案):
现在打开PowerShell并享受;)
设置执行策略是特定于环境的。如果您尝试从正在运行的x86 ISE执行脚本,则必须使用x86 PowerShell来设置执行策略。同样,如果您运行的是64位ISE,则必须使用64位PowerShell设置策略。
Win + R并键入复制粘贴命令,然后按确定:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
并执行您的脚本。
然后还原更改,如:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
您还可以使用以下命令绕过此操作:
PS > powershell Get-Content .\test.ps1 | Invoke-Expression
您还可以阅读Scott Sutherland撰写的这篇文章,其中介绍了如果您没有管理员权限,可以绕过PowerShell Set-ExecutionPolicy
的15种不同方法:
在PowerShell ISE编辑器中,我发现运行以下行首先允许的脚本。
Set-ExecutionPolicy RemoteSigned -Scope Process
在PowerShell 2.0中,默认情况下将执行策略设置为禁用。
从那时起,PowerShell团队已经做了很多改进,他们相信用户在运行脚本时不会破坏很多东西。因此,从PowerShell 4.0开始,它默认启用。
在您的情况下,从PowerShell控制台键入Set-ExecutionPolicy RemoteSigned
并说“是”。
我今天遇到了同样的问题。 64位执行策略不受限制,而32位受限制。
以下是如何远程更改32位策略:
Invoke-Command -ComputerName $servername -ConfigurationName Microsoft.PowerShell32 -scriptblock {Set-ExecutionPolicy unrestricted}
转到注册表路径HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell
并将ExecutionPolicy
设置为RemoteSigned
。
您可以通过在运行PowerShell时添加-ExecutionPolicy ByPass
来绕过此策略
powershell -ExecutionPolicy ByPass -File script.ps1
几个答案指向执行政策。但是有些事情也需要“runas管理员”。这是最安全的,因为执行策略没有永久性更改,并且可以超出管理员限制。使用schedtask启动批处理:
runas.exe /savecred /user:administrator powershell -ExecutionPolicy ByPass -File script.ps1
来自上面的Jack Edmonds和后How to run an application as "run as administrator" from the command prompt?的Peter Mortensen / Dhana
我发现这条线最适合我的一台Windows Server 2008 R2服务器。在我的PowerShell脚本中,如果没有这一行,其他几个没有问题:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Force -Scope Process
以管理员身份打开PowerShell窗口。它会工作。
我有一个类似的问题,并指出cmd
上的默认Windows Server 2012正在运行x64。
对于Windows 7,Windows 8,Windows 10,Windows Server 2008 R2或Windows Server 2012,请以管理员身份运行以下命令:
x86(32位)
打开C:\Windows\SysWOW64\cmd.exe
运行命令powershell Set-ExecutionPolicy RemoteSigned
x64(64位)
打开C:\Windows\system32\cmd.exe
运行命令powershell Set-ExecutionPolicy RemoteSigned
您可以使用检查模式
echo %PROCESSOR_ARCHITECTURE%
[Environment]::Is64BitProcess
参考文献: MSDN - Windows PowerShell execution policies Windows - 32bit vs 64bit directory explanation
大多数现有答案都解释了How,但很少解释为什么。在你从互联网上的陌生人执行代码之前,特别是禁用安全措施的代码之前,你应该完全理解你在做什么。所以这里有关于这个问题的更多细节。
来自TechNet About Execution Policies Page:
通过Windows PowerShell执行策略,您可以确定Windows PowerShell加载配置文件和运行脚本的条件。
正如PowerShell Basics - Execution Policy and Code Signing所列举的那样,其好处是:
- 控制执行 - 控制执行脚本的信任级别。
- 命令Highjack - 防止在我的路径中注入命令。
- 身份 - 脚本是由我信任的开发人员创建和签名的,和/或使用我信任的证书颁发机构的证书签名。
- 完整性 - 恶意软件或恶意用户无法修改脚本。
要检查当前的执行策略,可以运行Get-ExecutionPolicy
。但你可能在这里,因为你想改变它。
为此,您将运行Set-ExecutionPolicy
cmdlet。
在更新执行策略时,您将做出两个重大决策。
Restricted
† - 不能在系统上执行本地,远程或下载的脚本。AllSigned
- 所有运行的脚本都需要进行数字签名。RemoteSigned
- 需要签署所有远程脚本(UNC)或下载。Unrestricted
- 不需要任何类型脚本的签名。LocalMachine
† - 执行政策会影响计算机的所有用户。CurrentUser
- 执行策略仅影响当前用户。Process
- 执行策略仅影响当前的Windows PowerShell进程。†=默认
例如:如果您只想为CurrentUser将策略更改为RemoteSigned,则运行以下命令:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
注意:要更改执行策略,您必须以管理员身份运行PowerShell。如果您处于常规模式并尝试更改executionpolicy,则会收到以下错误:
访问注册表项“HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ PowerShell \ 1 \ ShellIds \ Microsoft.PowerShell”被拒绝。若要更改默认(LocalMachine)范围的执行策略,请使用“以管理员身份运行”选项启动Windows PowerShell。
如果要加强对自己尚未从Internet下载的脚本(或至少不包含UNC元数据)的内部限制,可以强制策略仅运行签名的sripts。要签署自己的脚本,您可以按照Scott Hanselman关于Signing PowerShell Scripts的文章的说明进行操作。
注意:大多数人在打开Powershell时都可能会收到此错误,因为PS在启动时尝试做的第一件事是执行您的用户配置文件脚本,根据您的喜好设置您的环境。
该文件通常位于:
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
您可以通过运行powershell变量找到确切的位置
$profile
如果配置文件中没有您关心的任何内容,并且不想对您的安全设置大惊小怪,您可以删除它,而PowerShell将找不到任何无法执行的操作。
在Windows 7中:
转到“开始”菜单,然后搜索“Windows PowerShell ISE”。
右键单击x86版本,然后选择“以管理员身份运行”。
在顶部,粘贴Set-ExecutionPolicy RemoteSigned
;运行脚本。选择“是”。
对64位版本的Powershell ISE也重复这些步骤(非x86版本)。
我只是澄清了@Chad Miller所暗示的步骤。谢谢乍得!
在脚本之前运行此命令也可以解决问题:
set-executionpolicy unrestricted
如果您所处的环境不是管理员,则可以仅为您设置执行策略,并且不需要管理员。
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
要么
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
您可以在帮助条目中阅读所有相关信息。
Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full
RemoteSigned:您自己创建的所有脚本都将运行,从Internet下载的所有脚本都需要由受信任的发布者签名。
好的,只需输入以下内容即可更改策略:
Set-ExecutionPolicy RemoteSigned
我们可以通过以下命令获取当前ExecutionPolicy
的状态:
Get-ExecutionPolicy;
默认情况下,它是受限制的。要允许执行PowerShell脚本,我们需要将此ExecutionPolicy设置为Bypass或Unrestricted。
我们可以使用以下任何PowerShell命令将当前用户的策略设置为Bypass
或Unrestricted
:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
不受限制的策略加载所有配置文件并运行所有脚本。如果您运行从Internet下载的未签名脚本,则会在运行之前提示您获得权限。
在绕过策略中,没有任何内容被阻止,并且在脚本执行期间没有警告或提示。绕过ExecutionPolicy
比Unrestricted
更放松。