我们相信:世界是美好的,你是我也是。平行空间的世界里面,不同版本的生活也在继续...

在这篇文章中,苏南大叔要聊聊powershellpolicy问题。当然,事情的缘由还是调试使用vscode执行python的事情,具体的可以参见苏南大叔的前几篇文章。

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - powershell-policy
powershell无法加载profile.ps1脚本,如何解决?(图6-1)

本文的测试环境:win10vscode@1.50.1powershell@1.0

配置powershell

因为conda的缘故,提示需要在powershell中执行conda init命令。

conda activate your_env_name

CommandNotFoundError: Your shell has not been properly configured to use 'conda activate'.
If using 'conda activate' from a batch script, change your
invocation to 'CALL conda.bat activate'.

To initialize your shell, run

    $ conda init <SHELL_NAME>

Currently supported shells are:
  - bash
  - cmd.exe
  - fish
  - tcsh
  - xonsh
  - zsh
  - powershell

See 'conda init --help' for more information and options.

IMPORTANT: You may need to close and restart your shell after running 'conda init'.

这个命令,更改了powershell的配置文件profile.ps1

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - poershell-2
powershell无法加载profile.ps1脚本,如何解决?(图6-2)

然后在执行powershell的时候,会有如下错误提示:

无法加载文件 C:\Users\sunan\Documents\WindowsPowerShell\profile.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?LinkID=135170 中的 about_Execution_Pol 
icies。
所在位置 行:1 字符: 3
+ . 'C:\Users\sunan\Documents\WindowsPowerShell\profile.ps1'
+   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [],PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess
加载个人及系统配置文件用了 522 毫秒。

查看当前policy

powershell里面执行下列命令,可以获得当前的powershellpolicy设置。

get-ExecutionPolicy

比如:在anaconda自带的powershell里面执行相关命令,就可以获得bypass的结果。

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - poershell-3
powershell无法加载profile.ps1脚本,如何解决?(图6-3)

更改当前policy

命令行如下:

set-ExecutionPolicy RemoteSigned

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - poershell-4
powershell无法加载profile.ps1脚本,如何解决?(图6-4)

如果想要更改当前的policy,就需要在管理员模式下,执行powershell,然后执行相关命令。否则会得到更严重的错误提示信息。同时需要注意的是:命令执行过程中,需要主动回答y以继续执行。

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - poershell-5
powershell无法加载profile.ps1脚本,如何解决?(图6-5)

本文中,是要把policy更改的更宽松。以使得powershell可以加载自定义的profile.ps1文件。

苏南大叔:powershell无法加载profile.ps1脚本,如何解决? - poershell-6
powershell无法加载profile.ps1脚本,如何解决?(图6-6)

相关文章

总结

powershell究竟比传统的cmd要强大多少呢?在后续的文章中,苏南大叔会继续和大家分享,相关的powershell的强大之处。

更多相关文章,请点击苏南大叔的博客文章:

如果本文对您有帮助,或者节约了您的时间,欢迎打赏瓶饮料,建立下友谊关系。
本博客不欢迎:各种镜像采集行为。请尊重原创文章内容,转载请保留作者链接。

 【福利】 腾讯云最新爆款活动!1核2G云服务器首年50元!

 【源码】本文代码片段及相关软件,请点此获取更多信息

 【绝密】秘籍文章入口,仅传授于有缘之人   powershell