首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >运行``Set ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`是否安全

运行``Set ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`是否安全
EN

Stack Overflow用户
提问于 2021-07-24 04:27:44
回答 2查看 332关注 0票数 1

我在我们的windows power shell中安装了这个来自https://www.powershellgallery.com/packages/ImportExcel/7.1.0的模块:

代码语言:javascript
复制
Install-Module -Name ImportExcel -RequiredVersion 7.1.0 

然后我运行这个命令,但我得到的脚本没有数字签名:-

代码语言:javascript
复制
PS C:\WINDOWS\system32> Import-Module ImportExcel
Import-Module : File C:\Program Files\WindowsPowerShell\Modules\ImportExcel\7.1.0\ImportExcel.psm1 cannot be loaded.
The file C:\Program Files\WindowsPowerShell\Modules\ImportExcel\7.1.0\ImportExcel.psm1 is not digitally signed. You
cannot run this script on the current system. For more information about running scripts and setting execution policy,
see about_Execution_Policies at https:/go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ Import-Module ImportExcel
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Import-Module], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.ImportModuleCommand

为了解决这个问题,我运行这个命令Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass..这是安全的吗?

EN

回答 2

Stack Overflow用户

发布于 2021-07-24 06:30:49

tl;dr

代码语言:javascript
复制
# Save the current execution policy...
$currPolicy = Get-ExecutionPolicy
# ... and temporarily set the policy to 'Bypass' for this process.
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force

# ASSUMING YOU TRUST MODULE ImportExcel:
# Execution of scripts in the context of importing the module is
# now permitted.
Install-Module -Name ImportExcel -RequiredVersion 7.1.0 

# Restore the previous execution policy for this process.
Set-ExecutionPolicy -Scope Process -ExecutionPolicy $currPolicy -Force

套用Santiago Squarzon对这个问题的有用评论:

PowerShell的execution policies通过控制是否以及在什么条件下允许执行脚本文件(.ps1)来防止潜在的危害:Restricted完全阻止执行,AllSigned只允许执行经过加密签名的脚本,RemoteSigned只需要从web下载的脚本进行签名,Unrestricted对执行没有限制,但提示确认执行互联网下载的脚本的意图,Bypass根本不限制执行。

这意味着,当有效的策略阻止给定脚本的执行时,这并不意味着特定的脚本实际上包含有害内容-策略从不检查这一点,它们纯粹是按照正式的标准执行的。

当然,相反,任何给定的脚本都可能包含有害内容。

看起来您使用的是来自PowerShell GalleryImportExcel module,它是开源PowerShell模块的官方存储库;该库将提交给它的模块提交给security checks

你已经安装了这个模块,这表明你在原则上信任它。(当然,您可以通过(Get-Module ImportExcel -ListAvailable).Path报告的目录自己检查有问题的脚本以及整个模块。)

这样导入恰好涉及到运行脚本(*.ps1),并不是所有的modules都会这样做(这样做是用于模块内部帮助器脚本,或者用于通过ScriptsToProcess module-manifest条目中指定的脚本对调用者的作用域进行必要的修改)。

换句话说:

  • Import-Module调用可能根本不涉及脚本的执行,但它总是涉及将命令导入到您的会话中,您应该绝对信任会话才能使用它们。

因此,

  • 导入模块应该是一个要么全有要么全无的信任命题,如果您确实信任它,那么有效的执行策略不应该妨碍它的导入。

上面显示的代码通过有效地将流程级策略的范围限定为感兴趣的模块导入,最大限度地降低了使流程级策略更具允许性的风险。

顺便说一句:这个问题甚至不会出现在类Unix平台(需要跨平台的PowerShell (Core) v6+版本)上,因为执行策略并不适用。

票数 2
EN

Stack Overflow用户

发布于 2021-09-27 15:42:16

您可以在您的终端中运行此命令。

代码语言:javascript
复制
Set-ExecutionPolicy -Scope CurrentUser

然后,这将要求提供一个值,此时您可以将其设置为Bypass / RemoteSigned

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68504743

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档