简体   繁体   English

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

[英]Is it safe to run `Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass`

I installed this module from https://www.powershellgallery.com/packages/ImportExcel/7.1.0 inside our windows power shell:-我从https://www.powershellgallery.com/packages/ImportExcel/7.1.0在我们的 windows 电源 shell 中安装了这个模块:-

Install-Module -Name ImportExcel -RequiredVersion 7.1.0 

Then i run this command, but i got that the script is not digitally signed:-然后我运行这个命令,但我发现脚本没有数字签名:-

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

and to fix this i run this command Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass .. so is this safe?为了解决这个问题,我运行了这个命令Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass .. 这样安全吗?

tl;dr tl;博士

# 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

To paraphrase Santiago Squarzon 's helpful comment on the question:转述Santiago Squarzon对这个问题的有益评论:

PowerShell's execution policies prevent potential harm by controlling if and under what conditions the execution of script files ( .ps1 ) is permitted: Restricted prevents execution altogether, AllSigned only allows cryptographically signed scripts to execute, RemoteSigned requires only scripts downloaded from the web to be signed, Unrestricted places no restriction on execution but prompts to confirm the intent to execute internet-downloaded scripts, Bypass doesn't restrict execution at all. PowerShell 的执行策略通过控制是否以及在什么条件下允许执行脚本文件 ( .ps1 ) 来防止潜在危害: Restricted完全阻止执行, AllSigned只允许执行加密签名的脚本, RemoteSigned只需要从 Web 下载的脚本进行签名, Unrestricted对执行没有限制,但会提示确认执行互联网下载脚本的意图, Bypass根本不限制执行。

This means when the effective policy prevents execution of a given script, the implication is not that the particular script actually contains harmful content - the policies never check for that, they purely act on formal criteria.这意味着当有效策略阻止执行给定脚本时,其含义并不在于特定脚本实际上包含有害内容——策略从不检查这一点,它们纯粹是按照正式标准行事。

Conversely, of course, any given script could contain harmful content.相反,当然,任何给定的脚本都可能包含有害内容。

It looks like you're using the ImportExcel module from the PowerShell Gallery , the official repository for open-source PowerShell modules;看起来您正在使用PowerShell Gallery 中ImportExcel模块PowerShell Gallery是开源 PowerShell 模块的官方存储库; the gallery subjects modules submitted to it to security checks .画廊主题模块提交给它进行安全检查

The fact that you've already installed this module suggests that you trust it in principle.您已经安装了此模块的事实表明您原则上信任它。 (Of course, you can inspect the script in question - and the module as a whole - yourself, via the directory that (Get-Module ImportExcel -ListAvailable).Path reports.) (当然,您可以通过(Get-Module ImportExcel -ListAvailable).Path报告的目录自己检查有问题的脚本 - 以及整个(Get-Module ImportExcel -ListAvailable).Path 。)

Importing it just so happens to involve running a script ( *.ps1 ), which not all modules do (doing so is used for module-internal helper scripts or for required modifications to the caller 's scope via the script specified in the ScriptsToProcess module-manifest entry).导入它恰好涉及运行脚本*.ps1 ),并非所有模块都这样做(这样做用于模块内部帮助程序脚本或通过ScriptsToProcess 模块中指定的脚本对调用者的范围进行所需的修改-清单条目)。

To put it differently:换个说法:

  • An Import-Module call may not involve execution of scripts at all, yet it invariably involves importing commands into your session, which you should definitely trust in order to use them. Import-Module调用可能根本不涉及脚本的执行,但它总是涉及将命令导入到您的会话中,您绝对应该信任这些命令才能使用它们。

  • Importing a module should therefore be an all-or-nothing trust proposition, and if you do trust it, the effective execution policy should not get in the way of importing it.因此,导入模块应该是一个全有或全无的信任主张,如果您确实信任它,那么有效的执行策略不应该妨碍导入它。

The code shown above minimizes the risk of making the process-level policy more permissive by effectively scoping it to the module import of interest.上面显示的代码通过有效地将其范围限定为感兴趣的模块导入,从而最大限度地降低了使流程级策略更加宽松的风险。


As an aside: The problem wouldn't even arise on Unix-like platforms (which requires the cross-platform PowerShell (Core) v6+ edition), where execution policies do not apply.顺便说一句:这个问题甚至不会出现在类 Unix 平台上(需要跨平台的PowerShell (Core) v6+版本),其中执行策略不适用。

You can run this command in your terminal.您可以在终端中运行此命令。

Set-ExecutionPolicy -Scope CurrentUser

This should then ask for a value at which point you can set it to Bypass / RemoteSigned然后这应该要求一个值,此时您可以将其设置为 Bypass / RemoteSigned

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM