如何安裝 PowerShell 模組?


安裝 PowerShell 模組有兩種方法:線上和離線。

線上方法

此方法就像在 Unix 系統中透過 Yum 下載線上軟體包一樣。

我們首先需要使用 **Find-Module** 命令搜尋網際網路上可用的軟體包。如果您不知道完整的模組名稱,可以使用萬用字元。所有軟體包都從 PowerShell 庫下載 (https://www.powershellgallery.com/)

例如,如果您想要 **Vmware PowerCLI** 模組並且您不知道完整的模組名稱,只需在萬用字元 (*) 內使用名稱的一部分。

Find-Module *vmware* | Select Name, Version, Repository
Name                            Version          Repository
----                            -------          ----------
VMware.VimAutomation.Sdk       12.0.0.15939651    PSGallery
VMware.VimAutomation.Core      12.0.0.15939655    PSGallery
VMware.VimAutomation.Common    12.0.0.15939652    PSGallery
VMware.VimAutomation.Cis.Core  12.0.0.15939657    PSGallery
VMware.Vim                     7.0.0.15939650     PSGallery
VMware.VimAutomation.Vds       12.0.0.15940185    PSGallery
VMware.VimAutomation.Srm       11.5.0.14899557    PSGallery
VMware.VimAutomation.License   12.0.0.15939670    PSGallery
VMware.VimAutomation.vROps     12.0.0.15940184    PSGallery
VMware.VimAutomation.Cloud     12.0.0.15940183    PSGallery
VMware.ImageBuilder            7.0.0.15902843     PSGallery
VMware.VimAutomation.Nsxt      12.0.0.15939671    PSGallery
VMware.PowerCLI                12.0.0.15947286    PSGallery
VMware.VimAutomation.Horiz...  7.12.0.15718406    PSGallery
VMware.VimAutomation.Storage   12.0.0.15939648    PSGallery
VMware.DeployAutomation        7.0.0.15902843     PSGallery
VMware.VimAutomation.Vmc       12.0.0.15947287    PSGallery
VMware.VumAutomation           6.5.1.7862888      PSGallery
VMware.VimAutomation.Stora...  1.3.0.0            PSGallery
VMware.VimAutomation.Security  12.0.0.15939672    PSGallery
VMware.VimAutomation.Hcx       12.0.0.15939647    PSGallery
VMware.VimAutomation.HA        6.5.4.7567193      PSGallery
VMware.VimAutomation.PCloud    10.0.0.7893924     PSGallery
VMware.CloudServices           12.0.0.15947289    PSGallery
VMware.VimAutomation.Workl... 12.0.0.15947288     PSGallery

現在我們有了一個可用的 **VMware** 模組列表,我們需要其中 **Vmware.PowerCLI** 模組。要安裝模組,我們將使用 **Install-Module** cmdlet。

Find-Module Vmware.PowerCLI | Install-Module
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its InstallationPolicy value by running the SetPSRepository cmdlet.
Are you sure you want to install the modules from 'https://www.powershellgalle ry.com/api/v2/'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):

您可以使用 **Get-Module** 命令檢查模組是否已安裝。

PS C:\WINDOWS\system32> Get-Module -Name *vmware* -ListAvailable |
Select Name, Version

Name                         Version
----                         -------
VMware.CloudServices          12.0.0.15947289
VMware.DeployAutomation       7.0.0.15902843
VMware.DeployAutomation       6.5.1.5299608
VMware.ImageBuilder           7.0.0.15902843
VMware.ImageBuilder           6.5.1.5299608
vmware.powercli               12.0.0.15947286
VMware.Vim                    7.0.0.15939650
VMware.VimAutomation.Cis.Core 6.5.1.5374323
VMware.VimAutomation.Cloud    12.0.0.15940183
VMware.VimAutomation.Cloud    6.5.1.5375799
VMware.VimAutomation.Common   6.5.1.5335010

此方法的優點是,它還會安裝依賴模組。

離線方法

在伺服器沒有網路連線的情況下,您需要從伺服器或具有活動網路連線的桌面(從 PowerShell 庫)離線下載軟體包,並將軟體包複製到離線伺服器。

您可以從 PowerShell 庫下載軟體包 https://www.powershellgallery.com/,對於此示例,請搜尋軟體包“**Vmware.PowerCLI**”,如果軟體包存在,您將獲得軟體包名稱。

當您單擊此軟體包時,您將獲得如下所示的 **手動下載選項卡**。

您下載的軟體包將採用 **Nupkg** 格式(Nuget 軟體包)。此軟體包是 **ZIP** 格式檔案,一些瀏覽器(如 Internet Explorer)會自動將其轉換為 **ZIP**,但您也可以將其副檔名重新命名為 **ZIP**。您也可以使用 **7-Zip** 直接將 **Nupkg** 內容提取到資料夾中。資料夾的內容如下所示。

上述檔案/資料夾內容的說明。

  • **_rels 資料夾** - 包含列出依賴項的 .rels 檔案。

  • **Package 資料夾** - 包含 NuGet 特定的資料。

  • **[Content_Types]** - 描述 NuGet 擴充套件檔案如何與 PowerShellGet 一起使用。

  • **<name>.nuspec** - 包含大部分元資料

有關更多詳細資訊,請參閱 Microsoft 關於離線安裝方法的文章。

https://docs.microsoft.com/en-us/powershell/scripting/gallery/how-to/working-withpackages/manual-download?view=powershell-7

解壓縮資料夾後,資料夾名稱將類似於 **<Modulename.Version>**,從該資料夾名稱中刪除版本,現在資料夾名稱將變為模組名稱。

根據上述文章,我們需要從資料夾中刪除 Nuget 特定的元素,但我們可以直接將整個解壓縮的資料夾複製/貼上到 Powershell 模組路徑。以下是 Powershell 的模組路徑。

PS C:\WINDOWS\system32> $env:PSModulePath -split ';'
C:\Users\admin\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules\
C:\Program Files\PoSHServer\modules\
C:\Windows\System32\WindowsPowerShell\v1.0\Modules

我們將在這裡選擇“**C:\Program Files\WindowsPowerShell\Modules**”模組資料夾並將解壓縮的資料夾複製到那裡。現在重新開啟或開啟一個新的 PowerShell 會話,並檢查複製的新模組是否已正確載入,您會看到 **vmware.powercli** 模組。

PS C:\WINDOWS\system32> Get-Module -Name *vmware* -ListAvailable | Select
Name,Version
Name                            Version
----                            -------
VMware.CloudServices             12.0.0.15947289
VMware.DeployAutomation          7.0.0.15902843
VMware.DeployAutomation          6.5.1.5299608
VMware.ImageBuilder              7.0.0.15902843
VMware.ImageBuilder              6.5.1.5299608
vmware.powercli                  12.0.0.15947286
VMware.Vim                       7.0.0.15939650
VMware.VimAutomation.Cis.Core    6.5.1.5374323
VMware.VimAutomation.Cloud       12.0.0.15940183
VMware.VimAutomation.Cloud       6.5.1.5375799
VMware.VimAutomation.Common      6.5.1.5335010
VMware.VimAutomation.Core        6.5.1.5374329
VMware.VimAutomation.HA          6.0.0.5314477
VMware.VimAutomation.Hcx         12.0.0.15939647
VMware.VimAutomation.HorizonView 7.12.0.15718406
VMware.VimAutomation.HorizonView 7.1.0.5307191

此方法不會安裝依賴模組。您需要根據需要下載依賴模組並單獨安裝它們。

更新於: 2020-12-18

733 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告