Con*_*nor 1545 powershell windows-server-2008-r2
我正在尝试运行从命令提示符调用PowerShell脚本的.cmd文件,我收到以下错误:
无法加载Management_Install.ps1,因为在此系统上禁用了脚本的执行.
我跑了cmd.exe
,当我Management_Install.ps1
从PowerShell 运行时,我Get-ExecutionPolicy
回来了.
// Powershell的输出
PS C:\ Users\Administrator> get-executionpolicy
无限制
//从DOS输出
C:\ Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scr
ipts> powershell.\ Management_Install.ps1 1
警告:运行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
按任意键继续 ...
该系统是Windows Server 2008 R2.
我究竟做错了什么?
Cha*_*ler 1992
如果您使用的是Windows Server 2008 R2,那么有一个x64和x86版本的PowerShell,这两个版本都必须设置其执行策略.您是否在两台主机上设置了执行策略?
作为管理员,您可以通过在PowerShell窗口中键入以下内容来设置执行策略:
Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参阅使用Set-ExecutionPolicy Cmdlet.
Jac*_*nds 641
您可以通过-ExecutionPolicy ByPass
在运行PowerShell时添加来绕过此策略
powershell -ExecutionPolicy ByPass -File script.ps1
Run Code Online (Sandbox Code Playgroud)
Ral*_*oss 131
我遇到了类似的问题,并注意到Windows Server 2012cmd
上的默认设置是运行x64.
对于Windows 7,Windows 8,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执行策略
Windows - 32位对64位目录说明
Kyl*_*Mit 113
大多数现有答案都解释了How,但很少解释为什么.在你从互联网上的陌生人执行代码之前,特别是禁用安全措施的代码之前,你应该完全理解你在做什么.所以这里有关于这个问题的更多细节.
从TechNet 关于执行策略页面:
通过Windows PowerShell执行策略,您可以确定Windows PowerShell加载配置文件和运行脚本的条件.
PowerShell Basics - 执行策略和代码签名所列举的好处包括:
- 控制执行 - 控制执行脚本的信任级别.
- 命令Highjack - 防止在我的路径中注入命令.
- 身份 - 脚本是由我信任的开发人员创建和签名的,和/或使用我信任的证书颁发机构的证书签名.
- 完整性 - 恶意软件或恶意用户无法修改脚本.
要检查当前的执行策略,您可以运行Get-ExecutionPolicy
.但你可能在这里,因为你想改变它.
为此,您将运行Set-ExecutionPolicy
cmdlet.
在更新执行策略时,您将做出两个重大决策.
Restricted
† - 无法在系统上执行本地,远程或下载的脚本.AllSigned
- 所有运行的脚本都需要进行数字签名.RemoteSigned
- 需要签署所有远程脚本(UNC)或下载的脚本.Unrestricted
- 不需要任何类型脚本的签名.LocalMachine
† - 执行策略会影响计算机的所有用户.CurrentUser
- 执行策略仅影响当前用户.Process
- 执行策略仅影响当前的Windows PowerShell进程.†=默认
例如:如果您只想为CurrentUser将策略更改为RemoteSigned,则运行以下命令:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Run Code Online (Sandbox Code Playgroud)
注意:要更改执行策略,您必须运行PowerShell As Adminstrator.如果您处于常规模式并尝试更改执行策略,则会收到以下错误:
访问注册表项"HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell"被拒绝.若要更改默认(LocalMachine)范围的执行策略,请使用"以管理员身份运行"选项启动Windows PowerShell.
如果要加强对尚未从Internet下载的脚本的内部限制(或至少不包含UNC元数据),可以强制策略仅运行签名的sripts.要签署自己的脚本,可以按照Scott Hanselman关于签署PowerShell脚本的文章中的说明进行操作.
注意:大多数人在打开Powershell时都可能会收到此错误,因为PS在启动时尝试做的第一件事是执行您的用户配置文件脚本,根据您的喜好设置您的环境.
该文件通常位于:
%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
Run Code Online (Sandbox Code Playgroud)
您可以通过运行powershell变量找到确切的位置
$profile
Run Code Online (Sandbox Code Playgroud)
如果配置文件中没有您关心的任何内容,并且不想对您的安全设置大惊小怪,您可以删除它,而PowerShell将找不到任何无法执行的操作.
Rya*_*yan 37
在Windows 7中:
转到"开始"菜单,然后搜索"Windows PowerShell ISE".
右键单击x86版本,然后选择"以管理员身份运行".
在顶部,粘贴Set-ExecutionPolicy RemoteSigned
; 运行脚本.选择"是".
对64位版本的Powershell ISE也重复这些步骤(非x86版本).
我只是澄清了@Chad Miller所暗示的步骤.谢谢乍得!
小智 36
在脚本之前运行此命令也可以解决问题:
set-executionpolicy unrestricted
Run Code Online (Sandbox Code Playgroud)
gho*_*ade 36
首先,您需要打开 PowerShell 窗口并运行此命令。
set-ExecutionPolicy RemoteSigned -Scope CurrentUser
然后它会要求你确认。键入Y并按Enter。
当您运行此命令时,您可以看到您的系统已将当前用户的所有策略设置为远程。完成此过程将需要几秒钟的时间。
图像将如下所示:
检查执行策略是否已设置。类型:
Get-ExecutionPolicy
如果设置了,输出将如下所示:
小智 31
如果您所处的环境不是管理员,则可以仅为您设置执行策略,并且不需要管理员.
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)
要么
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"
Run Code Online (Sandbox Code Playgroud)
您可以在帮助条目中阅读所有相关信息.
Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full
Run Code Online (Sandbox Code Playgroud)
小智 27
RemoteSigned:您自己创建的所有脚本都将运行,从Internet下载的所有脚本都需要由受信任的发布者签名.
好的,只需输入以下内容即可更改策略:
Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)
Arv*_*iya 27
打开 Windows PowerShell 命令窗口并运行以下查询进行更改ExecutionPolicy
:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
如果要求确认更改,请按Y并点击Enter。
Pra*_*til 26
我们可以ExecutionPolicy
通过以下命令获取当前状态:
Get-ExecutionPolicy;
Run Code Online (Sandbox Code Playgroud)
默认情况下,它是受限制的.要允许执行PowerShell脚本,我们需要将此ExecutionPolicy设置为Bypass或Unrestricted.
我们可以将当前用户的策略设置为Bypass
或Unrestricted
使用以下任何PowerShell命令:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force;
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force;
Run Code Online (Sandbox Code Playgroud)
不受限制的策略加载所有配置文件并运行所有脚本.如果您运行从Internet下载的未签名脚本,则会在运行之前提示您获得权限.
在绕过策略中,没有任何内容被阻止,并且在脚本执行期间没有警告或提示.绕道ExecutionPolicy
比放松更轻松Unrestricted
.
MD *_*YON 24
Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)
使用这个命令
小智 21
你应该运行这个命令:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)
小智 19
我正在使用Windows 10,无法运行任何命令.给我一些线索的唯一命令是这样的:
[64]
- 以管理员身份打开C:\ Windows\SysWOW64\cmd.exe
- 运行命令> powershell Set-ExecutionPolicy Unrestricted
但这没效果.它很有限.可能是Windows10的新安全策略.我有这个错误:
Set-ExecutionPolicy:Windows PowerShell已成功更新执行策略,但该设置被更具体范围内定义的策略覆盖.由于覆盖,您的shell将保留其当前有效的执行策略...
所以我找到了另一种方式(解决方案):
现在打开PowerShell并享受;)
php*_*erd 14
在 Windows 中打开命令提示符。如果问题仅与 PowerShell 相关,请使用以下命令:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)
小智 13
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
Run Code Online (Sandbox Code Playgroud)
这对我有用
小智 11
你可以试试这个并选择“全部”选项
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)
小智 10
现在运行你正在使用的运行命令..相信这个应用程序会运行..享受:)
我也遇到过类似的问题试试这个希望它对某人有所帮助 因为我使用的是 windows 所以按照下面给出的步骤以管理员身份打开命令提示符然后转到此路径
C:\Users\%username%\AppData\Roaming\npm\
Run Code Online (Sandbox Code Playgroud)
在此文件夹(dir)中查找文件ng.ps1,然后将其删除(del ng.ps1)
您也可以在此之后清除 npm 缓存,尽管它也可以在没有此步骤的情况下工作。希望它对我有用,因为它对我有用。
希望能帮助到你
您还可以使用以下命令绕过此操作:
PS > powershell Get-Content .\test.ps1 | Invoke-Expression
Run Code Online (Sandbox Code Playgroud)
您还可以阅读Scott Sutherland撰写的这篇文章,其中介绍了Set-ExecutionPolicy
在没有管理员权限的情况下绕过PowerShell的15种不同方法:
Win+ R并键入copy paste命令并按OK:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
Run Code Online (Sandbox Code Playgroud)
并执行您的脚本.
然后还原更改,如:
powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
Run Code Online (Sandbox Code Playgroud)
现有答案中有很多信息,但让我尝试系统概述:
PowerShell 的有效执行策略适用:
存储在文件中的 PowerShell 代码,这意味着常规脚本文件 ( .ps1
)、脚本模块文件 ( .psm1
) 以及格式和类型扩展文件 ( .psm1xml
)。
Get-ChildItem
)-Command
命令(除非这些命令直接或间接调用上面定义的脚本文件)。仅在 Windows 上(即,在类 Unix平台(Linux、macOS)上,执行策略不适用,并且对执行 PowerShell 代码没有任何限制)
在Windows 的工作站版本上,默认情况下禁用脚本文件执行(策略Restricted
),需要对策略进行持久修改才能启用它,或者仅对当前进程进行修改,例如-ExecutionPolicy
在调用 PowerShell CLIpowershell.exe
时通过参数进行修改(Windows PowerShell版)/ pwsh.exe
(PowerShell(核心)版)。
执行策略是单独维护的:
对于两个PowerShell版本:
for the 32-bit and 64-bit versions of Windows PowerShell (both of which are preinstalled)
LocalMachine
scope would be bitness-specific.For a given edition / bitness combination of PowerShell, the execution policies can be set in multiple scopes, but there's only ever one effective policy, based on precedence rules - see below.
In PowerShell on Windows, script-file execution is disabled by default in workstation editions of Windows (on Unix, execution policies do not apply); that is, the default execution policy in workstation editions of Windows is Restricted
, whereas in server editions, it is RemoteSigned
; see the conceptual about_Execution_Policies help topic for a description of all available policies.
To set a (local) policy that permits script execution, use Set-ExecutionPolicy
. There are three scopes that Set-ExecutionPolicy
can target, using the -Scope
parameter (see below); changing the LocalMachine
scope requires elevation (running as admin).
Undefined
A frequently used policy that provides a balance between security and convenience is RemoteSigned
, which allows local scripts - including from network shares - to execute without containing a signature, while requiring scripts downloaded from the internet to be signed (assuming that the downloading mechanism marks such as scripts as internet-originated, which web browsers do by default). For instance, to set the current user's execution policy to RemoteSigned
, run the following:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
Run Code Online (Sandbox Code Playgroud)
The PowerShell CLI (powershell.exe
for Windows PowerShell, pwsh.exe
for PowerShell (Core), v6+) accepts a process-specific -ExecutionPolicy <policy>
argument too, which is often used for ad-hoc policy overrides (only for the process being created, the equivalent of Set-ExecutionPolicy -Scope Process ..
); e.g.:
pwsh.exe -ExecutionPolicy RemoteSigned -File someScript.ps1
Run Code Online (Sandbox Code Playgroud)
Important:
执行策略也可以通过组策略对象 (GPO)设置,在这种情况下,它们不能使用CLI进行更改或覆盖Set-ExecutionPolicy
:请参阅about_Group_Policy_Settings
执行策略可以在不同的作用域中设置,哪一个有效是由它们的优先级决定的(运行以查看所有作用域及其各自的策略),按降序排列:Get-ExecutionPolicy
-List
MachinePolicy
(通过 GPO;无法在本地覆盖)[1]UserPolicy
(通过 GPO;无法在本地覆盖)[1]Process
(仅限当前进程;通常通过 CLI 设置临时进程)CurrentUser
(由 设定Set-ExecutionPolicy
)LocalMachine
(由 设定Set-ExecutionPolicy
,具有管理员权限)[1] 这适用于域范围内的 GPO。本地GPO可以 在本地修改,即通过gpedit.msc
或直接通过注册表进行修改。
在 PowerShell ISE编辑器中,我发现运行以下行首先允许脚本。
Set-ExecutionPolicy RemoteSigned -Scope Process
Run Code Online (Sandbox Code Playgroud)
在窗口 10 中:
如果您不是管理员,您可以使用以下命令:
powershell Set-ExecutionPolicy -Scope CurrentUser
cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`
Run Code Online (Sandbox Code Playgroud)
它像魅力一样解决了我的问题!
小智 7
对于 Windows 11...
这确实很容易。只需打开设置应用程序即可。导航至隐私和安全:
单击“对于开发人员”并滚动到底部,找到 PowerShell 选项,在该选项下选中“更改执行策略...远程脚本”复选框。
Set-ExecutionPolicy RemoteSigned
Run Code Online (Sandbox Code Playgroud)
在powershell中以管理员模式执行此命令即可解决问题。
在 powershell 中
要检查当前的执行策略,请使用以下命令:
Get-ExecutionPolicy
要将执行策略更改为“无限制”(允许运行任何没有数字签名的脚本),请使用以下命令:
Set-ExecutionPolicy Unrestricted
这个解决方案对我有用,但要小心所涉及的安全风险。
归档时间: |
|
查看次数: |
1925645 次 |
最近记录: |