如何在Windows 7上执行PowerShell脚本

image

When you download a script off the internet and try to run it, if you have not previously configured PowerShell, it will throw a nasty error in red font. This is enough to scare most users off, but there is an easy fix.

当您从Internet下载脚本并尝试运行该脚本时,如果以前未配置PowerShell,它将以红色字体抛出一个讨厌的错误。 这足以吓跑大多数用户,但是有一个简单的解决方法。

PowerShell has a number of execution modes that define what type of code it is permitted to run, this is governed by a registry key that lives in the HKLM hive. There are 4 different execution modes, they are:

PowerShell有多种执行模式,用于定义允许运行的代码类型,这由驻留在HKLM配置单元中的注册表项控制。 有4种不同的执行模式,它们是:

  • Restricted: Default execution policy, does not run scripts, interactive commands only.

    限制:默认执行策略,不运行脚本,仅运行交互式命令。

  • All Signed: Runs scripts; all scripts and configuration files must be signed by a publisher that you trust; opens you to the risk of running signed (but malicious) scripts, after confirming that you trust the publisher.

    全部签名 :运行脚本; 所有脚本和配置文件必须由您信任的发布者签名; 在确认您信任发布者之后,您就有运行签名(但恶意)脚本的风险。

  • Remote Signed: Local scripts run without signature. Any downloaded scripts need a digital signature, even a UNC path.

    远程签名 :本地脚本不带签名运行。 任何下载的脚本都需要数字签名,甚至需要UNC路径。

  • Unrestricted:Runs scripts; all scripts and configuration files downloaded from communication applications such as Microsoft Outlook, Internet Explorer, Outlook Express and Windows Messenger run after confirming that you understand the file originated from the Internet; no digital signature is required; opens you to the risk of running unsigned, malicious scripts downloaded from these applications

    不受限制 :运行脚本; 从通信应用程序(例如Microsoft Outlook,Internet Explorer,Outlook Express和Windows Messenger)下载的所有脚本和配置文件在确认您了解来自Internet的文件后运行; 不需要数字签名; 使您冒着运行从这些应用程序下载的未经签名的恶意脚本的风险

The default execution policy of PowerShell is called Restricted. In this mode, PowerShell operates as an interactive shell only. It does not run scripts, and loads only configuration files signed by a publisher that you trust. If you are getting the nasty red error the most probable cause is that you are trying to run an unsigned script. The safest thing to do is to change the Execution Policy to unrestricted, run your script and then change it back to restricted.

PowerShell的默认执行策略称为“受限”。 在这种模式下,PowerShell仅充当交互式外壳。 它不运行脚本,并且仅加载由您信任的发布者签名的配置文件。 如果收到令人讨厌的红色错误,则最可能的原因是您正在尝试运行未签名的脚本。 最安全的做法是将执行策略更改为不受限制,运行脚本,然后将其更改回strict

To change it to unrestricted run the following command from an administrative PowerShell:

要将其更改为不受限制,请从管理PowerShell运行以下命令:

Set-ExecutionPolicy Unrestricted

Set-ExecutionPolicy不受限制

You will be asked if you are sure that you want to change the Execution Policy hit the enter button again.

系统将询问您是否确定要更改执行策略,请再次按Enter键。

image

You can now run your downloaded scripts without a problem. However, it’s a serious security risk if you forget to set the Execution Policy back to Restricted mode. You could probably guess how to set it back to Restricted, but incase you don’t:

现在,您可以毫无问题地运行下载的脚本。 但是,如果您忘记将执行策略重新设置为“受限”模式,则存在严重的安全风险。 您可能会猜测如何将其设置回“受限”,但是如果您不这样做:

Set-ExecutionPolicy Restricted

Set-ExecutionPolicy受限

Again you will be asked if you are sure that you want to change the execution mode, go ahead and hit enter.

再次询问您是否确定要更改执行模式,继续并按Enter。

image

翻译自: https://www.howtogeek.com/106273/how-to-allow-the-execution-of-powershell-scripts-on-windows-7/