且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PowerShell 错误“在此系统上禁止执行脚本."

更新时间:2023-12-04 19:22:10

我要出去玩一下,只是重新整理一下 关于执行策略.

I am going to go out on a limb here and just rehash a portion of About Execution Policies.

Windows 客户端操作系统的默认执行策略是受限的.这意味着脚本不会自动运行.如果您的 VM 具有 Windows 客户端操作系统并且您从未更改过执行策略,那么您的问题是预料之中的.如果一台 Windows 10 机器可以正常工作,那么有人更改了执行策略.

The default execution policy for Windows client OSes is Restricted. This means that a script will not run automatically. If your VM has a Windows client OS and you have never changed the execution policy, then your issue is expected. If the one Windows 10 machine works without issues, then someone changed the execution policy.

在有问题的 VM 上,您需要确定运行脚本的范围(或帐户).然后您需要相应地设置执行策略.

On the problematic VMs, you will need to determine the scope (or account) that is running your script. Then you will need to set the execution policy accordingly.

如果您在以自己的身份登录服务器时测试运行脚本,那么您只需打开 PowerShell 控制台并运行以下命令:

If you are testing running a script while logged into the server as yourself, then you can just open a PowerShell console and run the following:

Set-ExecutionPolicy RemoteSigned

然后在同一个控制台中运行脚本.

Then run the script in that same console.

以下命令将列出该机器上所有范围的执行策略:

The following command will list the execution policy for all scopes on that machine:

Get-ExecutionPolicy -List

您应该在工作系统和非工作系统上比较上述命令.您的问题可能是运行脚本的特定范围的执行策略设置.如果您阅读我帖子中的链接,它应该可以帮助您确定具体需要更改的内容.

You should compare the command above on the working system and the non-working system. Your issue likely be the execution policy setting for the particular scope that is running the script. If you read the link in my post, it should help you determine what you need to change specifically.

以下将允许所有本地脚本在 VM 上执行,无论它们是否已签名:

The following will allow all local scripts to execute on the VM, irrespective of whether they're signed or not:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine