PowerShell 表示“此系统上禁用了脚本执行。”

发布于 2024-09-29 20:05:13 字数 1433 浏览 7 评论 0原文

我正在尝试运行一个从 cmd.exe 调用 PowerShell 脚本的 cmd 文件,但收到此错误:

Management_Install.ps1 无法加载,因为此系统上禁用了脚本执行。

我运行了以下命令:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

当我从 PowerShell 运行 Get-ExecutionPolicy 时,它返回 Unrestricted

Get-ExecutionPolicy

输出:

Unrestricted

cd“C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts” 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

    • 类别信息:未指定:(:) []、PSSecurityException

    • FullyQualifiedErrorId:运行时异常

C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts>暂停

按任意键继续。 。 .


系统是Windows Server 2008 R2。

我做错了什么?

I am trying to run a cmd file that calls a PowerShell script from cmd.exe, but I am getting this error:

Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system.

I ran this command:

Set-ExecutionPolicy -ExecutionPolicy Unrestricted

When I run Get-ExecutionPolicy from PowerShell, it returns Unrestricted.

Get-ExecutionPolicy

Output:

Unrestricted

cd "C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts"
powershell .\Management_Install.ps1 1

WARNING: Running x86 PowerShell...

File C:\Projects\Microsoft.Practices.ESB\Source\Samples\Management Portal\Install\Scripts\Management_Install.ps1 cannot be loaded because the execution of scripts is disabled on this system. Please see "get-help about_signing" for more details.

At line:1 char: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 . . .


The system is Windows Server 2008 R2.

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(30

琉璃繁缕 2024-10-06 20:05:14

在 PowerShell ISE 编辑器中,我发现运行以下行首先允许的脚本。

Set-ExecutionPolicy RemoteSigned -Scope Process

In the PowerShell ISE editor I found running the following line first allowed scripts.

Set-ExecutionPolicy RemoteSigned -Scope Process
千纸鹤 2024-10-06 20:05:14

在 powershell 中

要检查当前执行策略,请使用以下命令:

Get-ExecutionPolicy

要将执行策略更改为“无限制”,允许运行任何没有数字签名的脚本,请使用以下命令:

Set -ExecutionPolicy Unrestricted

这个解决方案对我有用,但要小心涉及的安全风险。

In powershell

To check the current execution policy, use the following command:

Get-ExecutionPolicy

To change the execution policy to Unrestricted, which allows running any script without digital signatures, use the following command:

Set-ExecutionPolicy Unrestricted

This solution worked for me, but be careful of the security risks involved.

天煞孤星 2024-10-06 20:05:14

在 VS 代码中只需运行以下命令:

Set-ExecutionPolicy -Scope CurrentUser Unrestricted

In VS code just run this command:

Set-ExecutionPolicy -Scope CurrentUser Unrestricted
桃扇骨 2024-10-06 20:05:14
  1. 以管理员身份打开 PowerShell 并运行 Set-ExecutionPolicy -Scope CurrentUser
  2. 提供 RemoteSigned 并按 Enter
  3. 运行 Set-ExecutionPolicy -Scope CurrentUser
  4. 提供 Unrestricted 并按 Enter 键
  1. Open PowerShell as Administrator and run Set-ExecutionPolicy -Scope CurrentUser
  2. Provide RemoteSigned and press Enter
  3. Run Set-ExecutionPolicy -Scope CurrentUser
  4. Provide Unrestricted and press Enter
又爬满兰若 2024-10-06 20:05:13

如果您使用的是 Windows Server 2008 R2,则有一个 x64x86 版本的 PowerShell 都必须设置其执行策略。您是否在两台主机上都设置了执行策略?

作为管理员,您可以通过在 PowerShell 窗口中输入以下内容来设置执行策略:

Set-ExecutionPolicy RemoteSigned

有关详细信息,请参阅使用 Set-ExecutionPolicy Cmdlet

完成后,您可以将策略设置回其默认值:

Set-ExecutionPolicy Restricted

您可能会看到错误:

Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
To change the execution policy for the default (LocalMachine) scope, 
  start Windows PowerShell with the "Run as administrator" option. 
To change the execution policy for the current user, 
  run "Set-ExecutionPolicy -Scope CurrentUser".

因此您可能需要像这样运行命令(如注释中所示):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

If you're using Windows Server 2008 R2 then there is an x64 and x86 version of PowerShell both of which have to have their execution policies set. Did you set the execution policy on both hosts?

As an Administrator, you can set the execution policy by typing this into your PowerShell window:

Set-ExecutionPolicy RemoteSigned

For more information, see Using the Set-ExecutionPolicy Cmdlet.

When you are done, you can set the policy back to its default value with:

Set-ExecutionPolicy Restricted

You may see an error:

Access to the registry key
'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. 
To change the execution policy for the default (LocalMachine) scope, 
  start Windows PowerShell with the "Run as administrator" option. 
To change the execution policy for the current user, 
  run "Set-ExecutionPolicy -Scope CurrentUser".

So you may need to run the command like this (as seen in comments):

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
生死何惧 2024-10-06 20:05:13

您可以通过在运行 PowerShell 时添加 -ExecutionPolicy Bypass 来绕过单个文件的此策略

powershell -ExecutionPolicy Bypass -File script.ps1

You can bypass this policy for a single file by adding -ExecutionPolicy Bypass when running PowerShell

powershell -ExecutionPolicy Bypass -File script.ps1
秋心╮凉 2024-10-06 20:05:13

我遇到了类似的问题,并注意到 Windows Server 2012cmd >,正在运行 x64 版本。

对于Windows 11Windows 10Windows 7Windows 8Windows Server 2008 R2 strong> 或 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

您可以使用以下命令检查模式

  • 在 CMD 中:echo %PROCESSOR_ARCHITECTURE%
  • 在 Powershell 中:[Environment]::Is64BitProcess

参考文献:
MSDN - Windows PowerShell 执行策略
Windows - 32 位与 64 位目录说明

I had a similar issue and noted that the default cmd on Windows Server 2012, was running the x64 one.

For Windows 11, Windows 10, Windows 7, Windows 8, Windows Server 2008 R2 or Windows Server 2012, run the following commands as Administrator:

x86 (32 bit)
Open C:\Windows\SysWOW64\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned

x64 (64 bit)
Open C:\Windows\system32\cmd.exe
Run the command powershell Set-ExecutionPolicy RemoteSigned

You can check mode using

  • In CMD: echo %PROCESSOR_ARCHITECTURE%
  • In Powershell: [Environment]::Is64BitProcess

References:
MSDN - Windows PowerShell execution policies
Windows - 32bit vs 64bit directory explanation

乖不如嘢 2024-10-06 20:05:13

大多数现有答案都解释了如何,但很少有人解释为什么。在执行互联网上陌生人的代码(尤其是禁用安全措施的代码)之前,您应该确切地了解自己在做什么。这里有关于这个问题的更多细节。

从 TechNet 关于执行策略页面

Windows PowerShell 执行策略允许您确定 Windows PowerShell 加载配置文件和运行脚本的条件。

其好处,如 PowerShell 基础知识 - 执行策略和代码签名包括:

  • 执行控制 - 控制执行脚本的信任级别。
  • 命令劫持 - 防止在我的路径中注入命令。
  • 身份 - 脚本是由我信任的开发人员创建和签名的和/或使用我信任的证书颁发机构的证书签名的。
  • 完整性 - 脚本无法被恶意软件或恶意用户修改。

要检查当前的执行策略,您可以运行 Get-ExecutionPolicy。但你来这里可能是因为你想改变它。

为此,您需要运行 Set-ExecutionPolicy cmdlet 。

更新执行策略时,您需要做出两个主要决定。

执行策略类型:

  • 受限 - 系统上不能执行本地、远程或下载的脚本。
  • AllSigned - 所有运行的脚本都需要进行数字签名。
  • RemoteSigned - 所有远程脚本 (UNC) 或下载的都需要签名。
  • 无限制 - 任何类型的脚本都不需要签名。

新更改的范围

  • LocalMachine - 执行策略影响计算机的所有用户。
  • CurrentUser - 执行策略仅影响当前用户。
  • 进程 - 执行策略仅影响当前的 Windows PowerShell 进程。

† = 默认

例如:如果您想仅将当前用户的策略更改为 RemoteSigned,您需要运行以下命令:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

注意 :为了更改执行策略,您必须以管理员身份运行 PowerShell。
如果您处于常规模式并尝试更改执行策略,您将收到以下错误:

对注册表项“HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell”的访问被拒绝。要更改默认 (LocalMachine) 范围的执行策略,请使用“以管理员身份运行”选项启动 Windows PowerShell。

如果您想要加强对您自己的未从 Internet 下载的脚本(或至少不包含 UNC 元数据)的内部限制,您可以强制策略仅运行已签名的脚本。要签署您自己的脚本,您可以按照 Scott Hanselman 的文章中的说明进行操作:签署 PowerShell 脚本

注意:大多数人每次打开 PowerShell 时都可能会收到此错误,因为 PowerShell 启动时尝试执行的第一件事是执行用户配置文件脚本,该脚本会按照您喜欢的方式设置您的环境。

该文件通常位于:

%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

您可以通过运行 PowerShell 变量找到确切位置

$profile

如果配置文件中没有您关心的内容,并且不想对安全设置大惊小怪,您可以将其删除,PowerShell 不会找不到它无法执行的任何内容。

Most of the existing answers explain the How, but very few explain the Why. And before you go around executing code from strangers on the Internet, especially code that disables security measures, you should understand exactly what you're doing. So here's a little more detail on this problem.

From the TechNet About Execution Policies Page:

Windows PowerShell execution policies let you determine the conditions under which Windows PowerShell loads configuration files and runs scripts.

The benefits of which, as enumerated by PowerShell Basics - Execution Policy and Code Signing, are:

  • Control of Execution - Control the level of trust for executing scripts.
  • Command Highjack - Prevent injection of commands in my path.
  • Identity - Is the script created and signed by a developer I trust and/or a signed with a certificate from a Certificate Authority I trust.
  • Integrity - Scripts cannot be modified by malware or malicious user.

To check your current execution policy, you can run Get-ExecutionPolicy. But you're probably here because you want to change it.

To do so you'll run the Set-ExecutionPolicy cmdlet.

You'll have two major decisions to make when updating the execution policy.

Execution Policy Type:

  • Restricted - No Script either local, remote or downloaded can be executed on the system.
  • AllSigned - All script that are ran require to be digitally signed.
  • RemoteSigned - All remote scripts (UNC) or downloaded need to be signed.
  • Unrestricted - No signature for any type of script is required.

Scope of new Change

  • LocalMachine - The execution policy affects all users of the computer.
  • CurrentUser - The execution policy affects only the current user.
  • Process - The execution policy affects only the current Windows PowerShell process.

† = Default

For example: if you wanted to change the policy to RemoteSigned for just the CurrentUser, you'd run the following command:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Note: In order to change the Execution policy, you must be running PowerShell As Administrator.
If you are in regular mode and try to change the execution policy, you'll get the following error:

Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option.

If you want to tighten up the internal restrictions on your own scripts that have not been downloaded from the Internet (or at least don't contain the UNC metadata), you can force the policy to only run signed scripts. To sign your own scripts, you can follow the instructions on Scott Hanselman's article on Signing PowerShell Scripts.

Note: Most people are likely to get this error whenever they open PowerShell because the first thing PowerShell tries to do when it launches is execute your user profile script that sets up your environment however you like it.

The file is typically located in:

%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

You can find the exact location by running the PowerShell variable

$profile

If there's nothing that you care about in the profile, and don't want to fuss with your security settings, you can just delete it and PowerShell won't find anything that it cannot execute.

黎歌 2024-10-06 20:05:13

我们可以通过下面的命令获取当前ExecutionPolicy的状态:

Get-ExecutionPolicy

默认是Restricted。为了允许执行 PowerShell 脚本,我们需要将此 ExecutionPolicy 设置为无限制绕过

我们可以使用以下任意 PowerShell 命令将当前用户的策略设置为“绕过”:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

无限制策略加载所有配置文件并运行所有脚本。如果您运行从 Internet 下载的未签名脚本,系统会在运行前提示您授予权限。

而在绕过策略中,不会阻止任何内容,并且在脚本执行期间不会出现警告或提示。绕过ExecutionPolicyUnrestricted更宽松。

We can get the status of current ExecutionPolicy by the command below:

Get-ExecutionPolicy

By default it is Restricted. To allow the execution of PowerShell scripts we need to set this ExecutionPolicy either as Unrestricted or Bypass.

We can set the policy for Current User as Bypass by using any of the below PowerShell commands:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted -Force

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force

Unrestricted policy loads all configuration files and runs all scripts. If you run an unsigned script that was downloaded from the Internet, you are prompted for permission before it runs.

Whereas in Bypass policy, nothing is blocked and there are no warnings or prompts during script execution. Bypass ExecutionPolicy is more relaxed than Unrestricted.

丢了幸福的猪 2024-10-06 20:05:13

在脚本之前运行此命令也可以解决问题:

Set-ExecutionPolicy Unrestricted

Also running this command before the script also solves the issue:

Set-ExecutionPolicy Unrestricted
固执像三岁 2024-10-06 20:05:13

如果您所在的环境不是管理员,您可以专门为您设置执行策略 (CurrentUser),并且不需要管理员。

您可以将其设置为RemoteSigned

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

Unrestricted

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"

您可以在帮助条目中阅读有关获取和设置执行策略的所有内容:

Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full

If you are in an environment where you are not an administrator, you can set the Execution Policy just for you (CurrentUser), and it will not require administrator.

You can set it to RemoteSigned:

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

or Unrestricted:

Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "Unrestricted"

You can read all about Getting and Setting Execution policy in the help entries:

Help Get-ExecutionPolicy -Full
Help Set-ExecutionPolicy -Full
三人与歌 2024-10-06 20:05:13

在 Windows 7 中:

转到“开始”菜单并搜索“Windows PowerShell ISE”。

右键单击 x86 版本并选择“以管理员身份运行”。

在顶部,粘贴Set-ExecutionPolicy RemoteSigned;运行脚本。选择“是”。

对 64 位版本的 Powershell ISE(非 x86 版本)也重复这些步骤。

In Windows 7:

Go to Start Menu and search for "Windows PowerShell ISE".

Right click the x86 version and choose "Run as administrator".

In the top part, paste Set-ExecutionPolicy RemoteSigned; run the script. Choose "Yes".

Repeat these steps for the 64-bit version of Powershell ISE too (the non x86 version).

未央 2024-10-06 20:05:13

RemoteSigned:您自己创建的所有脚本都将运行,并且从 Internet 下载的所有脚本都需要由受信任的发布者签名。

好的,只需键入以下内容即可更改策略:

Set-ExecutionPolicy RemoteSigned

RemoteSigned: all scripts you created yourself will be run, and all scripts downloaded from the Internet will need to be signed by a trusted publisher.

OK, change the policy by simply typing:

Set-ExecutionPolicy RemoteSigned
朦胧时间 2024-10-06 20:05:13

首先,您需要打开 PowerShell 窗口并运行此命令。

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

然后它会要求你确认。输入 Y 并按 Enter

当您运行此命令时,您可以看到您的系统已将当前用户的所有策略设置为远程。完成此过程将需要几秒钟的时间。

图像将显示如下:

在此处输入图像描述

检查执行策略是否已设置。类型:

Get-ExecutionPolicy

如果已设置,输出将如下所示:

< img src="https://i.sstatic.net/0wTAV.png" alt="在此处输入图像描述">

First, you need to open the PowerShell window and run this command.

set-ExecutionPolicy RemoteSigned -Scope CurrentUser

Then it will ask you to confirm. Type Y and press Enter.

When you run this command, you can see that your system has set all policies for the current user as remotely. It will take a few seconds to complete this process.

The image will be shown like below:

Enter image description here

To check if the execution policy has set. Type:

Get-ExecutionPolicy

If it was set, the output would be like this:

Enter image description here

后来的我们 2024-10-06 20:05:13

我使用的是 Windows 10,无法运行任何命令。给我一些线索的唯一命令是:

[x64]

  1. 打开 C:\Windows\SysWOW64\cmd.exe [以管理员身份]
  2. 运行命令> powershell Set-ExecutionPolicy 无限制

但这不起作用。这是有限的。可能是 Windows10 的新安全策略。我有这个错误:

Set-ExecutionPolicy:Windows PowerShell 已成功更新您的执行策略,但该设置被更具体范围定义的策略覆盖。由于覆盖,您的 shell 将保留其当前有效的执行策略...

所以我找到了另一种方法(解决方案):

  1. 打开运行命令/控制台 (Win + < kbd>R)
  2. 类型:gpedit.msc组策略 编辑器)
  3. 浏览到本地计算机策略 -> 计算机配置 -> 管理模板 -> Windows 组件 -> Windows Powershell
  4. 启用“打开脚本执行
  5. 根据需要设置策略。我将其设置为“允许所有脚本”。

现在打开 PowerShell 并享受吧;)

I'm using Windows 10 and was unable to run any command. The only command that gave me some clues was this:

[x64]

  1. Open C:\Windows\SysWOW64\cmd.exe [as administrator]
  2. Run the command> powershell Set-ExecutionPolicy Unrestricted

But this didn't work. It was limited. Probably new security policies for Windows10. I had this error:

Set-ExecutionPolicy: Windows PowerShell updated your execution policy successfully, but the setting is overridden by a policy defined at a more specific scope. Due to the override, your shell will retain its current effective execution policy of...

So I found another way (solution):

  1. Open Run Command/Console (Win + R)
  2. Type: gpedit.msc (Group Policy Editor)
  3. Browse to Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows Powershell.
  4. Enable "Turn on Script Execution"
  5. Set the policy as needed. I set mine to "Allow all scripts".

Now open PowerShell and enjoy ;)

攒一口袋星星 2024-10-06 20:05:13

打开 Windows PowerShell 命令窗口并运行以下查询来更改 ExecutionPolicy

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

如果要求确认更改,按 Y 并按 Enter

Open a Windows PowerShell command window and run the below query to change ExecutionPolicy:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

If it asks for confirming changes, press Y and hit Enter.

黯然 2024-10-06 20:05:13

你应该运行这个命令:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

You should run this command:

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted
他夏了夏天 2024-10-06 20:05:13
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

这对我有用

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Unrestricted

This worked for me

抠脚大汉 2024-10-06 20:05:13

Win + R 并键入复制粘贴命令,然后按确定

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

并执行脚本。

然后恢复更改,例如:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"

Win + R and type copy paste command and press OK:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

And execute your script.

Then revert changes like:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "AllSigned"
猛虎独行 2024-10-06 20:05:13

在 Windows 中打开命令提示符。
如果问题仅与 PowerShell 相关,请使用以下命令:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"

Open the command prompt in Windows.
If the problem is only with PowerShell, use the following command:

powershell Set-ExecutionPolicy -Scope "CurrentUser" -ExecutionPolicy "RemoteSigned"
飘然心甜 2024-10-06 20:05:13

设置执行策略是特定于环境的。如果您尝试从正在运行的 x86 ISE 执行脚本,您有使用 x86 PowerShell 设置执行策略。同样,如果您运行 64 位 ISE,则必须使用 64 位 PowerShell 设置策略。

Setting the execution policy is environment-specific. If you are trying to execute a script from the running x86 ISE you have to use the x86 PowerShell to set the execution policy. Likewise, if you are running the 64-bit ISE you have to set the policy with the 64-bit PowerShell.

遗忘曾经 2024-10-06 20:05:13
  1. 打开运行命令/控制台(Win + R)
    类型:gpedit。 msc(组策略编辑器)
  2. 浏览到本地计算机策略 -> 计算机配置 -> 管理模板 -> Windows 组件 -> Windows Powershell。
  3. 启用打开脚本执行
    根据需要设置策略。 我将其设置为“允许所有脚本”

现在运行运行命令,无论您使用什么。相信应用程序将运行。享受:)

  1. Open Run Command/Console ( Win + R )
    Type: gpedit. msc (Group Policy Editor)
  2. Browse to Local Computer Policy -> Computer Configuration -> Administrative Templates -> Windows Components -> Windows Powershell.
  3. Enable "Turn on Script Execution"
    Set the policy as needed. I set mine to "Allow all scripts".

Now run the run command what ever you are using.. Trust this the app will runs.. Enjoy :)

本王不退位尔等都是臣 2024-10-06 20:05:13

现有答案中有很多信息,但让我尝试系统概述

Context

PowerShell 的有效执行策略适用

  • 存储在文件中的PowerShell代码,这意味着:

    • 常规脚本文件 (*.ps1)
    • 脚本模块文件 (*.psm1)(在 PowerShell 中实现的模块)
    • 捆绑在一起的模块格式类型-扩展 文件(*.Format.ps1xml*.Types.ps1xml) - 即使这些文件碰巧包含嵌入的PowerShell 脚本块。
    • 不适用于
      • 调用(二进制)cmdlet(例如,Get-ChildItem),除了随模块附带的第三方二进制 cmdlet包括格式化和类型扩展文件,如上所述。
      • 以交互方式提交或传递到 PowerShell 的命令 CLI 通过
        -Command 参数(除非这些命令直接或间接调用上面定义的脚本文件)。
  • 仅在 Windows 上(即,在类 Unix 平台(Linux、macOS)上)执行策略不适用,并且没有限制放置在执行 PowerShell 代码上)

在 Windows 的工作站版本中,默认情况下禁用脚本文件执行(策略 受限),需要对策略进行持续修改才能启用它,或者仅限当前进程修改,例如调用 PowerShell CLI 时通过 -ExecutionPolicy 参数,powershell.exeWindows PowerShell 版本)/ pwsh.exe PowerShell(核心)7 版)。
最近的服务器版本的Windows中,默认策略是RemoteSigned,这意味着虽然本地存储的脚本(包括在文件共享)可以执行,从网络下载的脚本只有在经过签名后才会执行。

执行策略单独维护:

  • 对于两个PowerShell版本

    • 旧版、仅限 Windows、随 Windows Windows PowerShell 版本(其最新版本为 v5.1)。 x)
    • 现代、跨平台、按需安装PowerShell(核心)7 版本。
  • 适用于32位和64位版本的Windows PowerShell(两者均已预安装)

    • 注意:如果您要并排安装 32 位和 64 位版本的 PowerShell(核心)(这并不常见),则仅 LocalMachine范围将是特定于位的。

对于给定的 PowerShell 版本/位数组合,可以在多个范围内设置执行策略,但只有一个有效策略,基于优先规则 - 见下文。


详细信息

  • 在 Windows 上的 PowerShell 中,默认情况下在工作站禁用脚本文件执行Windows 版本(在 Unix 上,执行策略不适用);也就是说,Windows 工作站版本中的默认执行策略是Restricted,而在服务器版本中,它是远程签名;请参阅概念性 about_Execution_Policies 帮助主题了解所有可用策略的说明。


  • 设置允许脚本执行的(本地)策略,请使用Set-ExecutionPolicy,策略为 AllSignedRemoteSigned无限制绕过,按安全性降序排列。使用 -Scope 参数Set-ExecutionPolicy 可以针对三个范围(见下文); 更改 LocalMachine 范围的策略需要提升(以管理员身份运行)

    • 提供安全性和便利性之间的平衡的常用策略是RemoteSigned,它允许本地 em> 脚本 - 包括来自网络共享 - 执行而不包含签名,同时要求对从互联网下载的脚本进行签名(假设下载机制标记为脚本源自互联网,网络浏览器默认执行此操作)。例如,要将当前用户的执行策略设置为 RemoteSigned,请运行以下命令:

      Set-ExecutionPolicy -Scope CurrentUser RemoteSigned -Force
      
    • 要在给定范围内取消设置先前设置的策略,请使用未定义策略。

  • PowerShell CLI (powershell.exe 用于 Windows PowerShell,pwsh.exe for PowerShell(核心)7< /em>) 也接受特定于流程的 -ExecutionPolicy 参数,该参数通常用于临时策略覆盖(仅适用于正在创建的进程,相当于Set-ExecutionPolicy -Scope Process ...);例如:

    pwsh.exe -ExecutionPolicy RemoteSigned -File someScript.ps1
    

  • 重要


[ 1] 这适用于域范围内的 GPO。 本地 GPO可以在本地修改,即通过gpedit.msc或直接通过注册表(在机器<的情况下)< /em> 策略需要管理权限。

There's great information in the existing answers, but let me attempt a systematic overview:

Context

PowerShell's effective execution policy applies:

  • to PowerShell code stored in files, which means:

    • regular script files (*.ps1)
    • script module files (*.psm1) (modules implemented in PowerShell)
    • modules that are bundled with formatting and type-extension files (*.Format.ps1xml and *.Types.ps1xml) - even if those files happen not to contain embedded PowerShell script blocks.
    • It does not apply to:
      • calls to (binary) cmdlets (e.g., Get-ChildItem), except for third-party binary cmdlets that come with modules that encompass formatting and type-extension files, as discussed above.
      • commands submitted interactively or passed to the PowerShell CLI via the
        -Command parameter (unless these commands directly or indirectly call script files as defined above).
  • on Windows only (that is, on Unix-like platforms (Linux, macOS) execution policies do not apply and no restrictions are placed on executing PowerShell code)

In workstation editions of Windows, script-file execution is disabled by default (policy Restricted), requiring either a persistent modification of the policy to enable it, or a current-process-only modification such as via the -ExecutionPolicy parameter when calling the PowerShell CLI, powershell.exe (Windows PowerShell edition) / pwsh.exe (PowerShell (Core) 7 edition).
In recent server editions of Windows, the default policy is RemoteSigned, meaning that while locally stored scripts (including on file shares) may be executed, downloaded-from-the-web scripts only execute if they're signed.

Execution policies are maintained separately:

  • for the two PowerShell editions:

    • the legacy, Windows-only, ships-with-Windows Windows PowerShell edition (whose latest and last version is v5.1.x)
    • the modern, cross-platform, install-on-demand PowerShell (Core) 7 edition.
  • for the 32-bit and 64-bit versions of Windows PowerShell (both of which are preinstalled)

    • Note: If you were to install 32-bit and 64-bit versions of PowerShell (Core) side by side (which would be unusual), only the 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.


Details

  • 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 with a policy of AllSigned, RemoteSigned, Unrestricted, or Bypass, in descending order of security. There are three scopes that Set-ExecutionPolicy can target, using the -Scope parameter (see below); changing the LocalMachine scope's policy requires elevation (running as admin).

    • 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
      
    • To unset a previously set policy in a given scope, use the Undefined policy.

  • The PowerShell CLI (powershell.exe for Windows PowerShell, pwsh.exe for PowerShell (Core) 7) 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
    
  • Important:

    • Execution policies can also be set via Group Policy Objects (GPOs), in which case they can not be changed or overridden with Set-ExecutionPolicy or via the CLI's -ExecutionPolicy parameter: see about_Group_Policy_Settings

    • Execution policies can be set in various scopes, and which one is in effect is determined by their precedence (run Get-ExecutionPolicy-List to see all scopes and their respective policies), in descending order:

      • MachinePolicy (via GPO; cannot be overridden locally)[1]
      • UserPolicy (via GPO; cannot be overridden locally)[1]
      • Process (current process only; typically set ad-hoc via the CLI)
      • CurrentUser (as set by Set-ExecutionPolicy)
      • LocalMachine (as set by Set-ExecutionPolicy, with admin rights)

[1] This applies to domain-wide GPOs. Local GPOs can be modified locally, namely via gpedit.msc or directly via the registry, which in the case of the machine policy requires administrative privileges.

但可醉心 2024-10-06 20:05:13

您可以尝试此操作并选择“全部”选项

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned

you may try this and select "All" Option

Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
我早已燃尽 2024-10-06 20:05:13

您还可以使用以下命令绕过此问题:

powershell Get-Content .\test.ps1 | Invoke-Expression

您还可以阅读 Scott Sutherland 撰写的这篇文章,其中解释了在没有管理员权限的情况下绕过 PowerShell Set-ExecutionPolicy 的 15 种不同方法:

绕过 PowerShell 执行策略的 15 种方法

You can also bypass this by using the following command:

powershell Get-Content .\test.ps1 | Invoke-Expression

You can also read this article by Scott Sutherland that explains 15 different ways to bypass the PowerShell Set-ExecutionPolicy if you don't have administrator privileges:

15 Ways to Bypass the PowerShell Execution Policy

〗斷ホ乔殘χμё〖 2024-10-06 20:05:13

我也遇到过类似的问题。试试这个。

由于我使用的是 Windows,因此我按照以下步骤操作。
以管理员身份打开命令提示符,然后转到此路径:

C:\Users\%username%\AppData\Roaming\npm\

在此文件夹(目录)中查找文件 ng.ps1
然后删除它(del ng.ps1)。

您还可以在此之后清除 npm 缓存,尽管没有此步骤它也应该可以工作。

I have also faced a similar issue. Try this.

As I'm using Windows, I followed the steps as given below.
Open a command prompt as an administrator and then go to this path:

C:\Users\%username%\AppData\Roaming\npm\

Look for the file ng.ps1 in this folder (directory)
and then delete it (del ng.ps1).

You can also clear npm cache after this though it should work without this step as well.

薄荷梦 2024-10-06 20:05:13

如果您安装了 Git,只需使用 Git Bash

输入图片此处描述

If you have Git installed, just use Git Bash.

Enter image description here

新人笑 2024-10-06 20:05:13
Set-ExecutionPolicy RemoteSigned

在PowerShell中以管理员模式执行此命令将解决该问题。

Set-ExecutionPolicy RemoteSigned

Executing this command in administrator mode in PowerShell will solve the problem.

谢绝鈎搭 2024-10-06 20:05:13

在Window 10中:

如果您不是管理员,您可以使用这个:

powershell Set-ExecutionPolicy -Scope CurrentUser

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`

它像魅力一样解决了我的问题!

In Window 10:

If you are not administrator, you can use this:

powershell Set-ExecutionPolicy -Scope CurrentUser

cmdlet Set-ExecutionPolicy at command pipeline position 1
Supply values for the following parameters:
ExecutionPolicy: `RemoteSigned`

It solved my problem like a charm!

佞臣 2024-10-06 20:05:13

对于Windows 11...

确实非常简单。只需打开设置应用程序即可。
导航到隐私和安全

隐私和安全图像

单击对于开发人员并滚动到底部并找到 PowerShell 选项,在该选项下选中“更改”复选框执行策略...远程脚本”。

开发者选项图片

For Windows 11...

It is indeed very easy. Just open the settings application.
Navigate to Privacy and Security:

Privacy and security image

Click on For Developers and scroll to the bottom and find the PowerShell option under which check the checkbox stating "Change the execution policy ... remote scripts".

Developer options image

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文