PowerShell Scripts 如何執行?

PowerShell Scripts 檔案,附檔名為 .ps1。(是數字的1,不是文字的小寫l)

對於 .ps1 的檔案,該如何執行?
  • 和一般的檔案執行一樣,雙擊 ps1 (double-click);不對
  • 在開始/執行 (Start / Run) 打入Script檔案名稱;不對
  • 直接在 PowerShell 的環境中,打入 Script檔案名稱 (含該檔案的完整路徑及附檔名);對的
  • 在啟動 PowerShell 環境時,一併將 Script檔案名稱 及相關參數指令打入;對的
針對,上述可以正常運作的方式,舉例說明如下:
直接在 PowerShell 的環境中
打入 D:\Learning\PowerShell\Ch02\GetFileShares.ps1
或是
先 cd D:\Learning\PowerShell\Ch02\
再打入 .\GetFileShares.ps1 (即便是在現行作用目錄區中,也要用 .\ )

在啟動 PowerShell 環境時
PowerShell -Noexit -command "D:\Learning\PowerShell\Ch02\GetWmiAndQuery.ps1"
參數
-Noexit:為了保持 PowerShell 的執行結果,能保留下來。
-command:為了是在啟動 PowerShell 時,一併載入指定的批次檔案。

===============================================
在路徑表示中,若是遇到含有 空白字元 的文件夾名稱,需要已 單引號 來標明。
例如:
PowerShell -Noexit -command "D:\Learning\'[MS Press] Windows PowerShell Scripting Guide'\Ch02\GetWmiAndQuery.ps1"

沒有留言: