无法加载 Powershell profile.ps1,因为其操作被软件限制策略阻止 [英] Powershell profile.ps1 cannot be loaded because its operation is blocked by software restriction policies
问题描述
在启动 PowerShell 主机(powershell.exe、PowerShellISE、NuGet 包管理器控制台等)时,我收到以下错误消息.
When starting a PowerShell host (powershell.exe, PowerShellISE, NuGet Package Manager Console etc) I was getting the following error message.
文件 C:\Users\MyUserName\Documents\WindowsPowerShell\profile.ps1无法加载,因为其操作被软件阻止限制政策.有关更多信息,请联系您的系统管理员.
File C:\Users\MyUserName\Documents\WindowsPowerShell\profile.ps1 cannot be loaded because its operation is blocked by software restriction policies. For more information, contact your system administrator.
这在 NuGet 包管理器控制台的情况下尤其成问题,因为控制台在错误发生后处于禁用/不可用状态.至少可以使用其他 PowerShell 主机.
This was particularly problematic in the case of the NuGet Package Manager Console as the console was left in a disabled / unusable state after the error. Other PowerShell hosts could at least be used.
几周前,我在新工作中设置我的工作站时遇到了问题,因为组策略设置正在应用 AllSigned 的 MachinePolicy.该问题已通过将设置更改为 RemoteSigned 解决.我使用以下命令检查了此设置是否仍然存在:
I had problems a few weeks ago, when setting up my workstation at a new job, because a Group Policy setting was applying a MachinePolicy of AllSigned. That issue had been resolved by changing the setting to RemoteSigned. I checked that this setting is still in place using the command:
Get-ExecutionPolicy -List
我还确认此 $profile.CurrentUserAllHosts 文件存在,但我在 Internet 上搜索另一个组策略设置" 没有发现任何有趣的内容.我的 PowerShell 版本信息是:
I also confirmed that this $profile.CurrentUserAllHosts file exists but my Internet searches for another "Group Policy setting" revealed nothing of interest. My PowerShell version information is:
$PSVersionTable
Name Value
---- -----
PSVersion 3.0
WSManStackVersion 3.0
SerializationVersion 1.1.0.1
CLRVersion 4.0.30319.18408
BuildVersion 6.2.8370.0
PSCompatibleVersions {1.0, 2.0, 3.0}
PSRemotingProtocolVersion 2.2
推荐答案
如果我删除或移动问题配置文件脚本,那么我的所有 PowerShell 主机都会正常启动,包括正在使用的 PowerShell ISE它是自己的当前用户当前主机脚本 C:\Users\MyUserName\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
If I delete or move the problem profile script then all of my PowerShell hosts start without errors, including the PowerShell ISE which was using it's own Current User Current Host script C:\Users\MyUserName\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1
事实证明,我的所有主机配置文件脚本的问题是它完全为空!将任何内容添加到文件中,甚至是单个空格字符或注释,都允许所有不同的 PowerShell 主机执行这个配置文件脚本.我一定是在某个时候清除了这个脚本的内容,而不是删除它.
It turns out that the problem with my All Hosts profile script was that it was completely empty! Adding any content into the file, even a single space character or comment, allowed this profile script to be executed by all of the various PowerShell hosts. I must have cleared out the contents of this script at some point, instead of deleting it.
不完全是一条直观的错误消息,这就是我发布此问答样式条目以供将来参考的原因.
Not exactly an intuitive error message which is why I'm posting this Q&A style entry for future reference.
有趣的是,我可以在所有主机启动后执行空/空白脚本,而且这个问题似乎特定于配置文件脚本.
Interestingly, I am able to execute empty / blank scripts in all hosts after they have started and this problem seems to be specific to profile scripts.
这篇关于无法加载 Powershell profile.ps1,因为其操作被软件限制策略阻止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!