当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从命令行等运行 PowerShell 脚本

作者:精品下载站 日期:2024-12-14 13:09:25 浏览:14 分类:玩电脑

从命令行等运行 PowerShell 脚本


如果您不熟悉 PowerShell 脚本语言并想了解如何运行 PowerShell 脚本,那么您已经阅读了正确的博客文章。本博客将是一个教程,涵盖运行脚本的常见方法以及可能出现的一些问题。

先决条件

本文将向您介绍如何在本地计算机上运行 PowerShell。如果您想继续阅读,请确保在开始本文之前满足以下先决条件。

  • 具有管理员权限的 Windows 10 计算机。
  • Windows PowerShell 版本 5 或更高版本。您还可以使用 PowerShell v7。本教程将重点介绍 Windows PowerShell,因为 Windows 操作系统已经拥有它。
  • 任何文本文件编辑器

处理执行策略

如果这是您第一次尝试执行 Windows PowerShell 脚本,您可能会遇到常见问题。 PowerShell 可能会返回一条错误消息,指出脚本“无法加载,因为在此系统上禁用了运行脚本”。

PS> .\GetServices.ps1
 File C:\Temp\GetServices.ps1 cannot be loaded because running scripts is disabled on this system. For more information, see about_Execution_Policies at
 https:/go.microsoft.com/fwlink/?LinkID=135170.
 At line:1 char:1
 .\GetServices.ps1
 ~~~~~ CategoryInfo          : SecurityError: (:) [], PSSecurityException
 FullyQualifiedErrorId : UnauthorizedAccess   

当您尝试运行执行策略设置为受限远程签名全部签名的 PowerShell 时,PowerShell 会返回上述错误消息。

受限制的

“受限”是为 Windows 客户端计算机设置的默认策略。如果您是第一次使用 PowerShell,您的默认策略可能会设置为限制所有脚本。

您仍然可以在终端中执行单个命令,但不能执行脚本文件。该限制包括任何以 .ps1xml.psm1.ps1 结尾的文件。

无限制

无限制允许您运行任何脚本,但是,如果脚本是从互联网下载的,它会在执行前向您发出警告。此策略通常是任何非 Windows 设备的默认策略。

远程签名

远程签名策略允许您运行 (a) 经过数字签名的任何脚本或 (b) 在本地计算机上编写的带或不带签名的任何脚本。

如果脚本是从互联网下载的且未签名,则您需要取消阻止该文件。您可以通过右键单击该文件并选择属性来执行此操作。或者,您可以对特定脚本文件使用 Unblock-File PowerShell cmdlet。

运行从互联网下载的脚本时,使用远程签名策略将是一个理想的选择。

全部签名

所有签名要求所有脚本均由受信任的发布者进行数字签名。这包括从互联网下载并在您的计算机本地编写的脚本。

更改 PowerShell 执行策略

更改执行策略:

  1. 使用“以管理员身份运行”打开 Windows PowerShell,以确保您拥有进行策略更改的最高权限。

[玩转系统] 从命令行等运行 PowerShell 脚本

2. 打开后,运行以下 PowerShell 命令来设置计算机的执行策略。如上所述,执行策略可以是三种不同类型之一。本教程使用了一种有用但仍然安全的RemoteSigned执行策略。

由于本教程假设您已从 Internet 下载 GetServices.ps1 脚本文件,因此将执行策略设置为 RemoteSigned

PS> Set-ExecutionPolicy RemoteSigned

RemoteSigned 执行策略强制您对从 Internet 下载的每个 PowerShell 脚本进行加密签名,然后 PowerShell 才能在您的系统上运行该脚本。

3. 您应该看到请求确认操作的输出。输入 Y 并按 Enter 键确认策略更改。

Execution Policy Change
 The execution policy helps protect you from scripts that you do not trust. Changing the execution policy might expose you to the
 security risks described in the about_Execution_Policies help topic at https:/go.microsoft.com/fwlink/?LinkID=135170. Do you want to
 change the execution policy?
 [Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"): 

此时,请按照以下步骤探索在计算机上运行 PowerShell 脚本的不同方法。

如何运行 PowerShell 脚本

为了演示运行 PowerShell 脚本,您实际上需要一个脚本文件来运行!如果您手边没有,请下载此 ZIP 文件并解压其中的 PS1 文件。您会在其中找到一个名为 GetServices.ps1 的简单脚本文件。

Write-Output "Listing Computer Services"
Get-Service

每个 PowerShell 脚本都应以 .ps1 扩展名结尾。

使用控制台

准备好脚本后,您可以通过几种不同的方式执行 PowerShell 脚本文件。最常见的方法之一是通过 PowerShell 控制台。

为此:

  1. 打开 PowerShell 控制台,如上所示。

2. 使用 Set-Location PowerShell cmdlet 或 cd 别名导航到脚本所在的文件系统位置。本教程的脚本位于 C:\Temp 目录中。

PS> cd C:\Temp\

3. 使用点 (.) 表示法运行脚本。 PowerShell 是一个也查找命令名称的 shell。要区分 PowerShell 命令和脚本,必须在脚本前面加上点。这个点代表当前目录。

 PS> .\GetServices.ps1

如何通过 PowerShell 位置从命令行运行 PowerShell 脚本

如果您不能或不愿意通过 PowerShell 控制台运行脚本,您也可以使用良好的命令行(命令提示符)来执行此操作。

要通过命令提示符运行脚本,您必须首先启动 PowerShell 可执行文件 (powershell.exe),PowerShell 位置为 C:\Program Files\WindowsPowerShell\powershell.exe 然后将脚本路径作为参数传递给它。

您可以在任何上下文中运行带有参数的脚本,只需在运行 PowerShell 可执行文件时指定参数即可,例如 powershell.exe -Parameter 'Foo' -Parameter2 'Bar'

一旦打开cmd.exe,您就可以执行如下所示的PowerShell脚本。此示例正在运行引擎并向其传递 C:\Temp\GetServices.ps1 的脚本路径。

请注意,下面的示例使用 PowerShell 位置路径来运行脚本。如果该文件夹不在您的 PATH 中的某个位置,则必须执行此操作。

CMD> C:\Program Files\WindowsPowerShell\powershell.exe "C:\Temp\GetServices.ps1"

PowerShell 7 的 PowerShell 位置使用名为 pwsh.exe 的不同可执行文件,通常位于 C:\Program Files\PowerShell\pwsh.exe

下面是一个方便的 YouTube 视频,介绍了如何通过 cmd.exe 执行的批处理文件执行脚本。

使用 PowerShell ISE

如果您创建自己的脚本或编辑其他脚本,您可能会使用脚本编辑器,例如 PowerShell ISE 或 Visual Studio (VS) Code 等。由于 ISE 随 Windows 一起提供,因此我们在本教程中重点介绍该方法。

要通过 ISE 调用脚本:

  1. 导航到“开始”菜单,搜索PowerShell ISE并将其打开。

[玩转系统] 从命令行等运行 PowerShell 脚本

2. 单击文件打开并找到您的脚本。

[玩转系统] 从命令行等运行 PowerShell 脚本

3. 打开脚本后,单击绿色运行按钮来执行脚本。此按钮将调用底部内置 PowerShell 终端中的脚本。

[玩转系统] 从命令行等运行 PowerShell 脚本

示例脚本的输出

PowerShell 脚本有时可以返回输出。当您正在执行的脚本被构建为返回对象时,就会发生这种情况,对象是 PowerShell 的基本组件。

如果运行示例 GetServices.ps1 脚本,您将看到以下内容。此脚本运行 Get-Service cmdlet,该 cmdlet 返回本地 Windows 计算机上安装的所有服务。

PS> .\GetScripts.ps1
Listing Computer Services
Status   Name               DisplayName
------   ----               -----------
Running  aakore             Acronis Agent Core Service
Stopped  AarSvc_1b668d      Agent Activation Runtime_1b668d
Running  AcronisActivePr... Acronis Active Protection Service
Running  AcronisCyberPro... Acronis Cyber Protection Service
Running  AcrSch2Svc         Acronis Scheduler2 Service
Running  AdobeARMservice    Adobe Acrobat Update Service
Running  AdobeUpdateService AdobeUpdateService
Running  AGMService         Adobe Genuine Monitor Service
Running  AGSService         Adobe Genuine Software Integrity Se...
----Truncated----

从脚本内运行 PowerShell 脚本

假设您有两个脚本,并且希望其中一个脚本调用另一个脚本。也许您有一个名为 GetUser.ps1 的脚本和一个名为 ResetPassword.ps1 的脚本。在 GetUser.ps1 脚本内部,您需要执行 ResetPassword.ps1 来重置用户密码。

在调用脚本 (GetUser.ps1) 内部,您可以添加一行来执行其他脚本,就像从命令行调用该脚本一样。

您可以在下面看到有几个选项。通常,您应该选择在同一会话或范围内运行其他脚本以简化操作,除非您有特定原因在另一个 PowerShell 会话中运行该脚本。

## To run the other script in a new session
powershell.exe .\ResetPassword.ps1
## To run the other script in the same session
.\ResetPassword.ps1

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯