如何從批處理檔案運行 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 被用來執行位於當前工作目錄中的名為 script.ps1 的 PowerShell 腳本。通過將 -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 目錄中的名為 script.ps1 的 PowerShell 腳本。通過將執行策略設置為 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