PowerShell 在工作環境應用日益廣泛,陸續寫了不少小工具。初期我會把工具 .ps1 複製一份作為每次作業的附屬檔案,發現問題就改進,歷經幾次改版逐漸成熟,後來變成每次都複製同一版 .ps1,代表時候到了, 可以開放其他團隊成員安裝到電腦共用,比照 PowerShell 內建指令,不用每次附上 .ps1,並且在電腦的每個角落都能呼叫。

要安裝 PowerShell 腳本(.ps1) 可使用 Install-Script 指令。Install-Script 下載安裝來源需來自 PowerShell Repository, 官方的 Repository 在 https://www.powershellgallery.com/api/v2,背後也是採用 NuGet 機制,故也可在企業內部建立私人 NuGet Repository 來源,而且不一定要架網站,用網路分享資料夾甚至放在本機磁碟機上都成。

這篇將介紹透過分享資料夾建立 PowerShell Repository、發佈 PowerShell 腳本、安裝/升級/移除 PowerShell 腳本。

在開始前發佈或安裝腳本前要先註冊 PowerShell Repository,用到的指令是 Register-PSRepository。 假設檔案伺服器主機名稱為 ps-repo-svr,網路分享資料夾名稱為 \ps-repo-svr\psrepo,語法如下:

$localRSRepo = @{
	Name = 'MyRSRepo'
	SourceLocation = '\\ps-repo-svr\psrepo'
	PublishLocation = '\\ps-repo-svr\psrepo'
	InstallationPolicy = 'Trusted'
}
Register-PSRepository @localRSRepo

注意:實務上 PublishLocation 可與 SourceLocation 不同路徑並限制特定人員存取,SourceLocation 則開放多數使用者讀取,藉此實現基本權限控管。

註冊完用 Get-PSRepository 查詢,應該要在結果看到 MyRSRepo:

要放上 PowerShell Repository 的腳本必須打包成 .nupkg 檔,打包及上傳程序 Publish-Script 會幫忙搞定,不需我們操煩。 但 .ps1 必須加入註解提供版本、作者、說明等資訊,這部分 PowerShell 也設想好了,呼叫 New-ScriptFileInfo 填好資料, 可以產生一個內含必要註解的 .ps1 框架。例如,執行New-ScriptFileInfo -Path '.\New-Random.ps1' -Version '1.0' -Author 'Jeffrey' -Description 'Generate a random number' 會得到以下 New-Random.ps1 空殼檔案:

<#PSScriptInfo

.VERSION 1.0

.GUID 048c960d-7712-4636-89ab-4858ae957595

.AUTHOR Jeffrey

.COMPANYNAME 

.COPYRIGHT 

.TAGS 

.LICENSEURI 

.PROJECTURI 

.ICONURI 

.EXTERNALMODULEDEPENDENCIES 

.REQUIREDSCRIPTS 

.EXTERNALSCRIPTDEPENDENCIES 

.RELEASENOTES


#>

<# 

.DESCRIPTION 
 Generate a random number

#> 
Param()

補上程式內容(這裡用沒什麼意義的 .NET Random.Next() 示範:

<#PSScriptInfo

.VERSION 1.0

.GUID 048c960d-7712-4636-89ab-4858ae957595

.AUTHOR Jeffrey

.COMPANYNAME 

.COPYRIGHT 

.TAGS 

.LICENSEURI 

.PROJECTURI 

.ICONURI 

.EXTERNALMODULEDEPENDENCIES 

.REQUIREDSCRIPTS 

.EXTERNALSCRIPTDEPENDENCIES 

.RELEASENOTES


#>

<# 

.DESCRIPTION 
 Generate a random number

#> 
Param ([int]$range)
if ($range -gt 0) {
	(New-Object System.Random).Next($range)
}
else {
	(New-Object System.Random).Next()
}

New-Random.ps1 修改完成,執行 Publish-Script -Path .\New-Random.ps1 -Repository MyPSRepo 即可上傳到 Repository,透過 Get-PSRepository 查詢會看到它:

註:首次 Publish 可能出現 NuGet 安裝提示

需要 NuGet.exe 才能繼續
PowerShellGet 需要 NuGet.exe 才能將項目發行到 NuGet 型存放庫。NuGet.exe 在 'C:\ProgramData\Microsoft\Windows\PowerShell\PowerShellGet' 或 'C:\Users\user-name\AppData\Local\Microsoft\Windows\PowerShell\PowerShellGet' 中必須可用,或必須位於 PATH 環境變數值所指定的其中一個路徑。您可以從 https://nuget.org/nuget.exe 下載 NuGet.exe。是否要讓 PowerShellGet 立即安裝 NuGet.exe?

發佈到 Repository 後,要安裝該腳本的客戶端也需先註冊這個私有 Repository,之後再透過 Install-Script 安裝它。

Register-PSRepository -Name 'MyRSRepo' -SourceLocation '\\ps-repo-svr\psrepo' -InstallationPolicy 'Trusted'
Install-Script -Repository MyPSRepo -Name New-Random -Scope CurrentUser

安裝指定 -Scope CurrentUser,腳本會裝在 $home\Documents\WindowsPowerShell\Scripts,僅供目前登入的使用者使用;若要與其他登入帳號共用,則需使用管理者權限安裝。

註:首次安裝可能會出現以下訊息

PATH 環境變數變更 尚未使用預設指令碼安裝路徑設定您的系統,這表示您只能透過指定指令碼檔案的完整路徑來執行指令碼。 此動作會將指令碼放到資料夾 'C:\Users\user-name\Documents\WindowsPowerShell\Scripts' 並將該資料夾新增到您的 PATH 環境變數。 是否要將指令碼安裝路徑 'C:\Users\user-name\Documents\WindowsPowerShell\Scripts' 新增到 PATH 環境變數?

若腳本有新版本,則可執行 Update-Script -Name New-Random 更新,如有新版會閃過下載進度,無新版時則不會有訊息,執行結果很隱晦。 另外,Update-Script 還有 RequiredVersion、MaximumVersion 等參數可指定版本。

移除版本可用 Uninstall-ScriptUninstall-Script -Name New-Random

學會用 PowerShell Repository 管理小組共用腳本,象徵自己的 PowerShell 應用又更深了幾吋,呵。

Tutorial of how to install PowerShell script from private PowerShell repository.


Comments

Be the first to post a comment

Post a comment