﻿# 如何在 Powershell 開啟時自動引入常用的 ps1 指令檔腳本

最近公司內負責 IT 的同事希望能透過 Powershell 測試遠端主機的 HTTP ( Port 80 ) 是否斷線，在以前，大家可能都會安裝 **telnet** 工具程式來做 TCP 測試，但在伺服器安裝 **telnet** 工具程式不是個好習慣，因為早期經常有許多駭客程式會利用系統內建的 **telnet** 程式發動一些攻擊或測試，所以近幾代的 Windows 也都變成預設不安裝 **telnet** 程式，如果你希望在沒有安裝 **telnet** 程式的情況下完成 HTTP 連線偵測，就可以利用 Powershell 達成這個目標。不過，每次開啟 Powershell 都要輸入長長的指令或每次都要手動載入 ps1 指令檔不是很麻煩嗎？今天我就來說說怎樣讓這些常用的 ps1 指令檔腳本在你開啟 Powershell 的同時就自動載入完成。

微軟有個 [Script Center](http://technet.microsoft.com/en-US/scriptcenter/) 網站，收納了許多各式各樣的 Powershell 指令檔腳本與 VBScript 指令檔，對於系統管理者來說是個大寶庫，只要你會搜尋、會使用，基本上各式各樣的管理指令檔都可以在這裡找到，不但能找到適用的指令檔，也能從中學習到很多，稍加修改就能用在自己日常的管理工作上。

對於「測試遠端主機的 HTTP ( Port 80 ) 是否有正常連線」這個需求，我就找到一個 [Test-Port](https://www.powershellgallery.com/packages/PoshFunctions/2.2.1.4/Content/Functions%5CTest-Port.ps1) 指令檔，可以方便用來完成這個任務（此腳本除了能測試 TCP 連線外，還可以測試 UDP 連線喔）。如下圖示，點選 **Test-Port.ps1** 即可下載該指令檔腳本：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/b92f943a5ad6356af860-image_1722.png)

開啟這個 **Test-Port.ps1** 腳本的原始碼來看，可以發現作者已經把這個 **Test-Port** 寫成了程式庫(**function**)，也就是只要將 **Test-Port.ps1** 載入執行環境後，就能直接使用 **Test-Port** cmdlet 執行：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/cc61ec9f1caf95a5367f-image_1723.png)

**※ 載入 ps1 程式庫的方法**

在 Powershell 裡，載入程式庫的方式是用一個 **小數點** ( dot ) 開頭，然後接著一個 **\*.ps1** 的檔名路徑，這種載入方式又稱為 **dot sourcing** 方法。如下圖示：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/001dee7f5599a86b2586-SNAGHTML3ad4edeb.png)

接著，就可以順利使用這個程式庫裡提供的 function 來用，如下圖示：

![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/b19b905cf2e6e625c2d5-SNAGHTML3ad6ef62.png)

由於 Powershell 中所有的變數都是「物件」，所以輸出的格式還能用「表格」的方式呈現，如果你一次檢測多台電腦或網站的話，可以搭配 **Format-Table** 來呈現，如下圖示：

[![](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/8356fe189d62b875506e-SNAGHTML3ad8c095_thumb.png)](https://stwillblogassets.blob.core.windows.net/files/images/external/stwillblogassets.blob.core.windows.net/6a9110a98b4d178e8efa-SNAGHTML3ad8c095.png)

**※ 開啟 Powershell 時自動載入 ps1 程式庫的方法**

由於用 **dot sourcing** 方法載入 ps1 腳本，只會影響或套用到目前登入或開啟的這個 Powershell Console 工作階段，如果你在日常的管理工作上經常需要載入這支 **Test-Port.ps1** 腳本，每次都要載入程式庫還挺煩人的，這時你必須瞭解 [Windows PowerShell Profiles](http://msdn.microsoft.com/en-us/library/bb613488%28VS.85,loband%29.aspx) 的觀念。

[Windows PowerShell Profiles](http://msdn.microsoft.com/en-us/library/bb613488%28VS.85,loband%29.aspx) 是指一個 Windows PowerShell 的**設定檔**(Profiles)，也就是說，如果你有正確設定一個 Windows PowerShell 設定檔，每次開啟 Windows PowerShell 的同時，就會預設載入這個設定檔，而這個設定檔的內容，其實就是 PowerShell 的指令檔。

在 Windows PowerShell 裡，設定檔可以放在以下這 4 個檔名路徑，而且會依序載入，如果找不到檔案也會自動跳過，不會引發錯誤：

1.  **%windir%\\system32\\WindowsPowerShell\\v1.0\\profile.ps1**  
    這個設定檔 ( profile.ps1 ) 會載入到所有使用者與所有 shell 執行環境。
2.  **%windir%\\system32\\WindowsPowerShell\\v1.0\\Microsoft.PowerShell\_profile.ps1**  
    這個設定檔 ( Microsoft.PowerShell\_profile.ps1 ) 會載入到所有使用者，但僅限於使用 Microsoft.PowerShell 的 shell 執行環境。
3.  **%UserProfile%\\My Documents\\WindowsPowerShell\\profile.ps1**  
    這個設定檔 ( profile.ps1 ) 會載入到目前登入的使用者，且會套用到該使用者所有 shell 執行環境。
4.  **%UserProfile%\\My Documents\\WindowsPowerShell\\Microsoft.PowerShell\_profile.ps1**  
    這個設定檔 ( Microsoft.PowerShell\_profile.ps1 ) 會載入到目前登入的使用者，但僅限於使用 Microsoft.PowerShell 的 shell 執行環境。  
    

這 4 個設定檔，預設並沒有自動被建立，如果你要建立設定檔的話，自行建立相對應的目錄與 ps1 檔案即可，以自動載入 **Test-Port.ps1** 來說，你可以有兩種設定方法：

1.  建立一個設定檔，假設位於 **我的文件\\WindowsPowerShell\\profile.ps1**  
    假設你的 **Test-Port.ps1** 置於 **G:\\** 目錄下，你就可以將 **profile.ps1** 輸入以下內容：
    
    ```
    . G:\Test-Port.ps1
    ```
    
2.  建立一個設定檔，假設位於 **我的文件\\WindowsPowerShell\\profile.ps1**  
    將你下載的 **Test-Port.ps1** 腳本內容，完整複製到 **profile.ps1** 檔案裡。

當然，這個 Windows PowerShell 設定檔(Profiles) 可以放置的不僅僅是載入程式庫而已，你要設定預設變數、設定 alias 指令別名、加入額外的 Windows PowerShell snap-in (匯入模組)、其他自訂的 function 等等，通通都可以，是不是非常方便呢！ :-)

**相關連結**

-   [Script Center Home Page](http://technet.microsoft.com/en-US/scriptcenter/)

-   [Test-Port](https://www.powershellgallery.com/packages/PoshFunctions/2.2.1.4/Content/Functions%5CTest-Port.ps1)
-   [Windows PowerShell Script: Test-Port](http://blogs.technet.com/b/wsnetdoc/archive/2011/11/07/windows-powershell-script-test-port.aspx)

-   [Windows PowerShell 論壇 – TechNet](http://social.technet.microsoft.com/Forums/zh-tw/powershellzhcht/threads)
-   [Windows PowerShell Profiles](http://msdn.microsoft.com/en-us/library/bb613488%28VS.85,loband%29.aspx)
-   [Windows PowerShell – TechNet Library](https://learn.microsoft.com/en-us/powershell/scripting/what-is-windows-powershell)

-   [What's New in Windows PowerShell 3.0](https://learn.microsoft.com/en-us/powershell/scripting/whats-new/overview)

-   [Hey, Scripting Guy! Blog](http://blogs.technet.com/b/heyscriptingguy/)

-   [My Five Favorite PowerShell 3.0 Tips and Tricks](http://blogs.technet.com/b/heyscriptingguy/archive/2012/08/20/my-five-favorite-powershell-3-0-tips-and-tricks.aspx)
