如何从批处理文件运行 PowerShell 脚本

  1. 使用 -File 参数从批处理文件运行 PowerShell 脚本
  2. 使用 RemoteSigned 作为 -ExecutionPolicy 从批处理文件运行 PowerShell 脚本
  3. 使用 Bypass 开关从批处理文件运行 PowerShell 脚本
  4. 通过以管理员身份打开 PowerShell 从批处理文件运行 PowerShell 脚本
  5. 结论
如何从批处理文件运行 PowerShell 脚本

PowerShell 脚本是一个使用 .ps1 扩展名的文本文件,包含一系列命令。PowerShell 以顺序执行这些命令。

批处理文件是一个使用 .bat 扩展名的文本文件。它也包含一系列命令,这些命令按顺序执行。

可以通过打开 .bat 文件来执行这些命令。这个教程将教你如何从批处理文件运行 PowerShell 脚本。

我们创建了一个 PowerShell 脚本 myscript.ps1,其中包含以下命令。pause 命令会暂停批处理文件的执行,直到你按下 Enter 键。

Write-Host "Your script is executed successfully."
Pause

使用 -File 参数从批处理文件运行 PowerShell 脚本

从批处理文件运行 PowerShell 脚本是一个在 Windows 环境中自动化任务的有用技术。-File 参数方法允许将 PowerShell 脚本无缝集成到批处理文件工作流中。

bat 文件中使用以下命令来运行 PowerShell 脚本。

@echo off
powershell -File script.ps1

在这个代码片段中,我们首先使用 @echo off 来抑制正在执行的命令的显示,确保只有脚本的输出对用户可见。然后,我们使用 powershell 命令加上 -File 参数来执行名为 script.ps1 的 PowerShell 脚本。

此命令启动了一个 PowerShell 会话,并指定要执行的脚本文件。通过组合这些命令,我们可以无缝地从批处理文件运行 PowerShell 脚本,从而简化在批处理文件环境中执行 PowerShell 命令的过程。

输出:

从批处理文件运行 PowerShell 脚本 - 输出 1

使用 RemoteSigned 作为 -ExecutionPolicy 从批处理文件运行 PowerShell 脚本

PowerShell 有不同的执行策略,决定哪些脚本可以运行以及从哪里运行。一种绕过这些限制的方法是指定 -ExecutionPolicy 参数,值为 RemoteSigned

此执行策略允许本地创建的脚本运行而不需要数字签名,而从互联网下载的脚本必须由受信任的发布者签名。

@echo off
powershell -ExecutionPolicy RemoteSigned -File script.ps1

在提供的代码片段中,powershell -ExecutionPolicy RemoteSigned -File script.ps1 用于执行位于当前工作目录的 PowerShell 脚本 script.ps1。通过将 -ExecutionPolicy 参数指定为 RemoteSigned,我们确保本地创建的脚本可以在没有数字签名的情况下执行,而从互联网下载的脚本必须由受信任的发布者进行签名。

输出:

从批处理文件运行 PowerShell 脚本 - 输出 2

使用 Bypass 开关从批处理文件运行 PowerShell 脚本

为了绕过限制并允许脚本在不受执行策略限制的情况下执行,我们可以使用 -ExecutionPolicy 参数,值为 Bypass。这种方法提供了一种简单的方法来从批处理文件运行 PowerShell 脚本,同时绕过任何执行策略限制。

@echo off
powershell -ExecutionPolicy Bypass -File C:\path\script.ps1

在提供的代码片段中,powershell -ExecutionPolicy Bypass -File C:\path\script.ps1 被用于执行位于 C:\path\scripts 目录中的 PowerShell 脚本 script.ps1。通过将执行策略设置为 Bypass,我们指示 PowerShell 无视任何执行策略限制,从而允许脚本顺利运行。

输出:

从批处理文件运行 PowerShell 脚本 - 输出 3

你还可以运行以下命令。

@echo off
powershell -NoProfile -ExecutionPolicy Bypass -Command "& 'C:\path\script.ps1'"

在这个提供的命令中,我们使用 -ExecutionPolicy Bypass 指示 PowerShell 无视任何执行策略限制,从而允许脚本在没有限制的情况下执行。此外,-NoProfile 参数确保用户的 PowerShell 配置文件不会被加载,这有助于维持脚本的干净执行环境。

输出:

从批处理文件运行 PowerShell 脚本 - 输出 4

通过以管理员身份打开 PowerShell 从批处理文件运行 PowerShell 脚本

通过以管理员身份打开 PowerShell 从批处理文件运行 PowerShell 脚本是一种执行需要管理访问权限的脚本的重要方法。这种方法确保 PowerShell 会话具有执行管理任务所需的权限,例如修改系统设置或访问受限资源。

@echo off
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Start-Process powershell.exe -Verb RunAs -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File C:\path\script.ps1'"

在这个代码片段中,我们使用 powershell.exe 启动一个具有提升权限的 PowerShell 会话。Start-Process cmdlet 中的 -Verb RunAs 参数确保 PowerShell 以管理员权限打开。

通过将必要的参数 (-NoProfile -ExecutionPolicy Bypass -File C:\path\script.ps1) 传递给新的 PowerShell 会话,我们启用以提升权限执行指定的脚本 (script.ps1)。

输出:

从批处理文件运行 PowerShell 脚本 - 输出 5

结论

在本文中,我们探讨了从批处理文件运行 PowerShell 脚本的各种方法。我们学习了如何使用 -File 参数直接执行脚本,以及如何使用 RemoteSignedBypass 参数绕过执行策略限制。

此外,我们讨论了如何以管理员身份打开 PowerShell 以运行具有提升权限的脚本。每种方法提供了一种独特的方法来从批处理文件执行 PowerShell 脚本,提供了对执行环境的灵活性和控制。

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
作者: Rohan Timalsina
Rohan Timalsina avatar Rohan Timalsina avatar

Rohan is a learner, problem solver, and web developer. He loves to write and share his understanding.

LinkedIn Website

相关文章 - PowerShell Script