如何安裝Microsoft Powershell Psresourcegette模塊

Psresourceget是一個模塊,其命令用於發現,安裝,更新和發布PowerShell工件,例如模塊,DSC資源,角色功能和腳本。 PSRESOURCEGET是最新的軟件包管理器,並替換了PowerShellget V2模塊。在本文中,您將學習如何安裝Microsoft PowerShell Psresourcegette模塊。

Microsoft Powershell Psresourceget

PSRESOURCEGET是一個模塊,它提供了在像庫中安裝,更新和定位模塊和腳本的能力Powershell畫廊

Psresourceget是PowerShell的新軟件包經理。它取代了PowerShellget V2模塊。現在您可能會問為什麼它沒有成為V3?在項目的初始階段。最終,決定在許多破裂變化中更改名稱。您將在PowerShell畫廊中找到V3作為兼容模塊。

Windows PowerShell 5.1帶有PowerShellget版本1.0.0.1版本。此版本的PowerShellget具有有限的功能,並且不支持PowerShell畫廊的更新功能。要安裝Psresourceget,您必須首先更新到最新版本的PowerShellget。

安裝Microsoft Powershell Psresourceget模塊

要在系統上安裝Microsoft Powershell Psresourcegetet模塊,請按照以下步驟操作。

1。設置Windows PowerShell執行策略

默認情況下,我們無法安裝腳本。為了需要您從Internet下載的所有PowerShell腳本,由受信任的發布者簽名,將PowerShell作為管理員運行,然後運行CMDLET。

Set-ExecutionPolicy RemoteSigned -Force

重要的:關閉並重新打開高架的Windows PowerShell窗口,以應用更改。

2。安裝PowerShellget模塊

運行PowerShell作為管理員。運行命令安裝模塊powershellget -force。當要求安裝Nuget提供商時,請按y並跟隨進入

Install-Module PowershellGet -Force

如果您遇到無法安裝的錯誤,請閱讀無法為PowerShell安裝Nuget提供商的文章。

3。安裝Microsoft PowerShell Psresourcegetget模塊

安裝Microsoft Powershell Psresourceget模塊。

Install-Module -Name Microsoft.PowerShell.PSResourceGet -Force

安裝了Microsoft Powershell Psresourceget之後,您應該打開一個新的PowerShell會話。當您使用PowerShell CMDLET時,PowerShell會自動加載模塊的最新版本。

我們還建議您將PowerShell畫廊註冊為可信賴的存儲庫。這不會提示您每次要下載模塊時都會信任存儲率。

Set-PSResourceRepository -Name PSGallery -Trusted

獲取psresourceget模塊版本

驗證是否已安裝了PSResourceget模塊。

Get-PSResource Microsoft.PowerShell.PSResourceGet -Scope AllUsers | ft -AutoSize

出現以下Powershell輸出。

Version Name                               Repository Description
------- ----                               ---------- -----------
1.0.2   Microsoft.PowerShell.PSResourceGet PSGallery  PowerShell module with commands for discovering, installing, updating and publishing the PowerShell artifacts like Modules, Scripts, and DSC Resources.

查找psresourceget模塊最新可用版本

Find-Module Microsoft.PowerShell.PSResourceGet | ft -AutoSize

下面的輸出出現。

Version Name                               Repository Description
------- ----                               ---------- -----------
1.0.2   Microsoft.PowerShell.PSResourceGet PSGallery  PowerShell module with commands for discovering, installing, updating and publishing the PowerShell artifacts like Modules, Scripts, and DSC Resources.

Microsoft Powershell psresourceget cmdlets

請參閱所有CMDLET的下面列表。

psresourceget cmdlet描述
find-psresource根據名稱或其他軟件包屬性,從存儲庫(本地或遠程)搜索包裝。
加入安裝通過PowerShellget返回機器上安裝的模塊和腳本。
get-psresourcerepository查找並返回註冊的存儲庫信息。
獲取psscriptfileinfo返回腳本的元數據。
Import-psgetRepository找到在PowerShellget註冊的存儲庫,並將其註冊為Psresourceget。
install-psresource從註冊存儲庫中安裝資源。
new-psscriptfileinfoCMDLET創建一個新的腳本文件,包括有關腳本的元數據。
Publish-psresource從本地計算機到PSRESOURCE存儲庫發布了指定的模塊。
寄存器psresourcerepository註冊一個用於Powershell資源的存儲庫。
Save-psresource將資源(模塊和腳本)從註冊的存儲庫保存到計算機上。
set-psresourcerepository為註冊存儲庫設置信息。
test-psscriptfileinfo在.ps1文件中測試基於評論的元數據,以確保其有效發布。
卸載-psource卸載使用PowerShellget安裝的資源。
Unregister-psresourcerceritory從本地機器中刪除註冊存儲庫。
update-psmodulemanifest更新模塊清單文件。
Update-psresource下載並安裝已經在本地計算機上安裝的軟件包的最新版本。
更新psscriptfileinfo此CMDLET在現有腳本.ps1文件中更新基於註釋的元數據。

如何使用Microsoft Powershell Psresourceget

讓我們看一下如何使用psresourceget安裝模塊。

install-psresource

在我們的示例中,我們喜歡安裝兩個模塊:

  1. Microsoft Graph PowerShell模塊
  2. Microsoft Graph Beta Beta PowerShell模塊

我們不必添加- 流浪運動以下命令的參數是因為我們已經在上一個安裝步驟中將PSGallery添加到了受信任的存儲庫中。但是您可能會忽略它,所以讓我們添加它。

筆記:使用- 重新安裝命令中的參數即使已經安裝了最新版本,要安裝模塊的最新版本。安裝版本被覆蓋。這使您可以修復損壞的模塊安裝。如果安裝了模塊的較舊版本,則將新版本並排安裝在新版本特定的文件夾中。

Install-PSResource Microsoft.Graph -Reinstall -Scope AllUsers -TrustRepository
Install-PSResource Microsoft.Graph.Beta -Reinstall -Scope AllUsers -TrustRepository

如果您想安裝特定的模塊版本,請使用-版本範圍。

Install-PSResource Microsoft.Graph -Version 2.14.0 -Scope AllUsers -TrustRepository
Install-PSResource Microsoft.Graph.Beta -Version 2.14.0 -Scope AllUsers -TrustRepository

加入安裝

檢查機器上安裝了哪些模塊。

Get-InstalledPSResource -Scope AllUsers

過濾安裝的模塊。

Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Where-Object { $_.Name -notlike "Microsoft.Graph.Beta*" }
Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Where-Object { $_.Name -like "Microsoft.Graph.Beta*" }

讓我們獲取安裝模塊的位置路徑。

筆記:當您運行install-psresourceCMDLET安裝模塊,從Windows PowerShell 5.1或PowerShell 7運行時,它可以安裝在其他位置。

Get-InstalledPSResource -Scope AllUsers | ft Name, InstalledLocation

下面的文件夾路徑是安裝模塊安裝的默認位置。您可以打開文件資源管理器中的以下文件夾。

當前用戶的Windows PowerShell 5.1:

%USERPROFILE%DocumentsWindowsPowerShellModules

Windows PowerShell 5.1適用於所有用戶:

C:Program FilesWindowsPowerShellModules

當前用戶的PowerShell 7:

%USERPROFILE%DocumentsPowerShellModules

所有用戶的PowerShell 7:

C:Program FilesPowerShellModules

卸載-psource

如果我們想用PSResourceget卸載一個模塊,我們需要使用卸載-psourcecmdlet。

這將刪除所有Microsoft圖形模塊。

Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Uninstall-PSResource -Scope AllUsers -SkipDependencyCheck

您始終可以過濾和刪除不需要的模塊。

Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Where-Object { $_.Name -notlike "Microsoft.Graph.Beta*" } | Uninstall-PSResource -Scope AllUsers -SkipDependencyCheck
Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Where-Object { $_.Name -like "Microsoft.Graph.Beta*" } | Uninstall-PSResource -Scope AllUsers -SkipDependencyCheck

psresourceget vs. PowerShellget V2

讓我們比較psresourceget和powershellget V2,以查看哪個在安裝和刪除模塊時更快。

在此示例中,我們將使用措施命令CMDLET安裝和卸載Microsoft Graph PowerShell模塊。這是一個很好的測試,因為它具有39個模塊。

與PowerShellget V2相比,Psresourceget的速度要快得多。

更多閱讀:FIX PACKAGEMARGAIGH \ install-package:針對指定的搜索標準和模塊名稱找到了匹配

cmdletPSRECOURCEGET(版本1.0.2)PowerShellget V2(版本2.2.5)
install-psresource58秒-
卸載-psource396毫秒-
安裝模塊-3分14秒
卸載模塊-3分48秒

就是這樣!

結論

您學會瞭如何安裝Microsoft Powershell Psresourcegette模塊。運行Windows PowerShell 5.1或PowerShell 7,然後逐步運行命令以安裝最新的PsResourceget模塊。從現在開始,請使用psresourceget進行安裝,更新和刪除模塊。

您喜歡這篇文章嗎?您可能還喜歡連接到在線PowerShell。不要忘記關注我們並分享這篇文章。