PowerShell 错误 “execution of scripts is disabled on this system.”

标签 powershell

我有一个 power shell 脚本,它运行以停止服务,“停止/终止进程”,删除 2 个文件,然后重新启动。

我可以在我的 Windows 10 64 位主机上完美运行这个脚本 - 零问题。
我尝试在我的虚拟机中运行它,但出现错误

cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170



所以只是为了傻笑,我去看了我的组策略,但它们都没有在任何一台机器上配置。

管理模板 > Windows 组件 > Windows PowerShell
未配置。

那么为什么问题是在虚拟机上而不是在我的主机上呢?

编辑
在 VM 上运行 Get-ExecutionPolicy 和 Get-ExecutionPolicy-List
受限制的
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine       Undefined

在我的主机上运行它
        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process       Undefined
  CurrentUser       Undefined
 LocalMachine    Unrestricted

Group Policy Screen Shot

我不知道我的本地机器是如何改变的——软件安装??

最佳答案

我要出去走走,只是重新整理一下 About Execution Policies 的一部分.

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

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

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

Set-ExecutionPolicy RemoteSigned

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

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

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

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

关于PowerShell 错误 “execution of scripts is disabled on this system.”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54776324/

相关文章:

c# - 将 PowerShell 参数传递给 Process.Start

function - 需要帮助 - 第一个功能

powershell - Vagrant Install-ChocolateyInstallPackage : $File parameter not recognized

linq - 如何在 powershell 中使用 linq explict 或在 SQL 中使用 "NOT IN"的类似物

Powershell 别名位置

powershell - 在 CMD 中运行多行 PowerShell 脚本

c# - Powershell cmdlet 的嵌套参数

powershell - powershell 的并行 foreach 最多使用 5 个线程吗?

powershell - 如何将 -Verbose 参数传递给 Invoke-Command 内部调用的函数?

powershell - 计算属性不会在 Powershell 中抛出异常。有哪些解决方法?