使用私人 PowerShellGet 存放庫

PowerShellGet 模組支援 PowerShell 資源庫以外的存放庫。 這些 Cmdlet 會啟用下列案例:

  • 支援一組受信任且預先驗證的 PowerShell 模組,以便在您的環境中使用
  • 測試建置 PowerShell 模組或指令碼的 CI/CD 管線
  • 將 PowerShell 指令碼和模組傳遞給無法存取網際網路的系統
  • 傳遞僅供您組織使用的 PowerShell 指令碼和模組

本文說明如何設定本機 PowerShell 存放庫。 本文亦涵蓋可從 PowerShell 資源庫取得的 OfflinePowerShellGetDeploy 模組。 此模組所包含的 Cmdlet 可將最新版的 PowerShellGet 安裝到您的本機存放庫。

本機存放庫類型

有兩種方式可用來建立本機 PSRepository:NuGet 伺服器或檔案共用。 每個類型都有優點和缺點:

NuGet 伺服器

優點 缺點
模擬 PowerShellGallery 功能 多層式應用程式需要作業規劃與支援
NuGet 整合了 Visual Studio,其他工具 需要驗證模型和 NuGet 帳戶管理
NuGet 支援 .Nupkg 套件的中繼資料 發佈需要 API 金鑰管理與維護
提供搜尋、套件管理等。

檔案共用

優點 缺點
易於設定、備份和維護 除基本的檔案共用之外沒有任何 UI
簡單的安全性模式:共用上的使用者權限 安全性有限,而且不會記錄誰負責更新哪些項目
沒有取代現有項目之類的條件約束

PowerShellGet 適用於任一個類型,並支援尋找版本和相依性安裝。 不過,部分適用於 PowerShell 資源庫的功能不適用基底 NuGet 伺服器或檔案共用。 腳本、模組、DSC 資源或角色功能沒有差異。

建立 NuGet.Server 存放庫

下列文章列出設定您自己 NuGet 伺服器的步驟。

遵循步驟來新增套件。 發佈套件的步驟將於本文稍後討論。

針對以檔案共用為基礎的存放庫,確定您的使用者具有存取檔案共用的權限。

註冊本機存放庫

您必須先使用 Register-PSRepository 命令來註冊存放庫,才能加以使用。 在下列範例中, InstallationPolicy 會設定 Trusted為 ,假設您信任自己的存放庫。

# Register a NuGet-based server
$registerPSRepositorySplat = @{
    Name = 'LocalPSRepo'
    SourceLocation = 'http://MyLocalNuget/Api/V2/'
    ScriptSourceLocation = 'http://MyLocalNuget/Api/V2'
    InstallationPolicy = 'Trusted'
}
Register-PSRepository @registerPSRepositorySplat

# Register a file share on my local machine
$registerPSRepositorySplat = @{
    Name = 'LocalPSRepo'
    SourceLocation = '\\localhost\PSRepoLocal\'
    ScriptSourceLocation = '\\localhost\PSRepoLocal\'
    InstallationPolicy = 'Trusted'
}
Register-PSRepository @registerPSRepositorySplat

請記下這兩個命令如何處理 ScriptSourceLocation 之間的差異。 針對以檔案共用為基礎的存放庫,SourceLocationScriptSourceLocation 必須相符。 針對以 Web 為基礎的存放庫,它們必須不同,因此在此範例中,會在 SourceLocation 尾端新增 "/"。

使用 NFS 或 SMB 等檔案共用通訊協定時,請務必遵循保護通訊協議的建議指引。 如需在 Windows 上保護 SMB 的詳細資訊,請參閱 [SMB 安全性增強功能][09]。

如果您想要使新建的 PSRepository 成為預設存放庫,就必須取消註冊所有其他 PSRepository。 例如:

Unregister-PSRepository -Name PSGallery

注意

存放庫名稱 'PSGallery' 會保留,以供 PowerShell 資源庫使用。 您可以取消註冊 PSGallery,但無法重複使用任何其他存放庫的名稱 PSGallery。

如果您需要還原 PSGallery,請執行下列命令:

Register-PSRepository -Default

發佈至本機存放庫

一旦註冊本機 PSRepository 之後,就能發佈到您的本機 PSRepository。 有兩個主要發佈案例:發佈您自己的模組和從 PSGallery 發佈模組。

發佈您所撰寫的模組

使用 Publish-ModulePublish-Script,以您針對 PowerShell 資源庫所做的相同方式來將模組發佈到本機 PSRepository。

  • 指定程式碼的位置
  • 提供 API 金鑰
  • 指定存放庫名稱。 例如, -PSRepository LocalPSRepo

注意

您必須在 NuGet 伺服器中建立帳戶,然後登入以產生並儲存 API 金鑰。 針對檔案共用,為 NuGetApiKey 值使用任何非空白的字串。

範例:

# Publish to a NuGet Server repository using my NuGetAPI key
$publishModuleSplat = @{
    Path = 'c:\projects\MyModule'
    Repository = 'LocalPsRepo'
    NuGetApiKey = $nuGetApiKey
}
Publish-Module @publishModuleSplat

重要

為了確保安全性,API 密鑰不應在腳本中硬式編碼。 使用安全金鑰管理系統。 手動執行命令時,API 金鑰不應以純文字形式傳遞,以避免記錄它, Read-Host Cmdlet 可用來安全地傳遞 API 金鑰的值。

# Publish to a file share repo - the NuGet API key must be a non-blank string
$publishModuleSplat = @{
    Path = 'c:\projects\MyModule'
    Repository = 'LocalPsRepo'
    NuGetApiKey = 'AnyStringWillDo'
}
Publish-Module @publishModuleSplat

從 PSGallery 發佈模組

若要將模組從 PSGallery 發佈至本機 PSRepository,您可以使用 Save-Package Cmdlet。

  • 指定套件名稱
  • 指定 'NuGet' 作為提供者
  • 將 PSGallery 位置指定為來源 (https://www.powershellgallery.com/api/v2)
  • 指定本機存放庫的路徑

範例:

# Publish from the PSGallery to your local Repository
$savePackageSplat = @{
    Name = 'PackageName'
    ProviderName = 'NuGet'
    Source = 'https://www.powershellgallery.com/api/v2'
    Path = '\\localhost\PSRepoLocal\'
}
Save-Package @savePackageSplat

如果您的本機 PSRepository 是以 Web 為基礎,則需要用來發佈的其他步驟 nuget.exe 。 請參閱使用 nuget.exe 的文件。

在中斷連線的系統上安裝 PowerShellGet

在要求系統中斷與網路網路連線的環境內,很難部署 PowerShellGet。 PowerShellGet 有一個啟動程序流程,可在第一次使用時安裝最新版本。 PowerShell 資源庫中的 OfflinePowerShellGetDeploy 模組提供支援此啟動程序流程的 Cmdlet。

若要啟動離線部署,您需要:

  • 下載 OfflinePowerShellGetDeploy 並安裝至您連線到網際網路的系統和中斷連線的系統
  • 在連線到網際網路的系統上,使用 Save-PowerShellGetForOffline Cmdlet 來下載 PowerShellGet 及其相依性
  • 將 PowerShellGet 及其相依性從連線到網際網路的系統複製到中斷連線的系統
  • 在中斷連線的系統上使用 Install-PowerShellGetOffline,以將 PowerShellGet 及其相依性放置於適當的資料夾

下列命令會使用 Save-PowerShellGetForOffline,將所有元件放入 f:\OfflinePowerShellGet 資料夾

# Requires -RunAsAdministrator
#Download the OfflinePowerShellGetDeploy to a location that can be accessed
# by both the connected and disconnected systems.
Save-Module -Name OfflinePowerShellGetDeploy -Path 'F:\' -Repository PSGallery
Import-Module F:\OfflinePowerShellGetDeploy

# Put PowerShellGet somewhere locally
Save-PowerShellGetForOffline -LocalFolder 'F:\OfflinePowerShellGet'

此時,您必須讓 F:\OfflinePowerShellGet 的內容可供中斷連線的系統使用。 執行 Install-PowerShellGetOffline Cmdlet,以在中斷連線的系統上安裝 PowerShellGet。

注意

在執行這些命令之前,您必須先在PowerShell工作階段中執行 PowerShellGet。 將 PowerShellGet 載入工作階段後,就無法更新元件。 如果您不小心啟動了 PowerShellGet,請結束並重新啟動 PowerShell。

Import-Module F:\OfflinePowerShellGetDeploy
Install-PowerShellGetOffline -LocalFolder 'F:\OfflinePowerShellGet'

執行這些命令之後,您就已經準備好將 PowerShellGet 發佈至本機存放庫。

# Publish to a NuGet Server repository using my NuGetAPI key
$publishModuleSplat = @{
    Path = 'F:\OfflinePowershellGet'
    Repository = 'LocalPsRepo'
    NuGetApiKey = $nuGetApiKey
}
Publish-Module @publishModuleSplat

重要

為了確保安全性,API 密鑰不應該在腳本中硬式編碼。 使用安全金鑰管理系統。 手動執行命令時,不應以純文本形式傳遞 API 金鑰,以避免記錄它, Read-Host Cmdlet 可以用來安全地傳遞 API 金鑰的值。

# Publish to a file share repo - the NuGet API key must be a non-blank string
$publishModuleSplat = @{
    Path = 'F:\OfflinePowerShellGet'
    Repository = 'LocalPsRepo'
    NuGetApiKey = 'AnyStringWillDo'
}
Publish-Module @publishModuleSplat

使用封裝解決方案來裝載 PowerShellGet 存放庫

您也可以使用封裝解決方案 (例如 Azure Artifacts) 來裝載私人或公用 PowerShellGet 存放庫。 如需詳細資訊和指示,請參閱 Azure Artifacts 文件 \(英文\)。