如何安装 Microsoft PowerShell PSResourceGet 模块

Jacki

PSResourceGet 是一个模块,其中包含用于发现、安装、更新和发布 PowerShell 工件(例如模块、DSC 资源、角色功能和脚本)的命令。 PSResourceGet 是最新的包管理器,取代了 PowerShellGet v2 模块。在本文中,您将学习如何安装 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 PSResourceGet 模块,请按照以下步骤操作。

1. 设置 Windows PowerShell 执行策略

默认情况下,我们无法安装脚本。要要求从 Internet 下载的所有 PowerShell 脚本均由受信任的发布者签名,请以管理员身份运行 PowerShell,然后运行 ​​cmdlet。

Set-ExecutionPolicy RemoteSigned -Force

重要的:关闭并重新打开提升的 Windows PowerShell 窗口以使更改生效。

2.安装PowerShellGet模块

以管理员身份运行 PowerShell。运行命令安装模块 PowershellGet -Force。当要求安装 NuGet 提供程序时,请按并跟随进入

Install-Module PowershellGet -Force

如果出现无法安装的错误,请阅读文章无法安装 PowerShell 的 NuGet 提供程序。

3. 安装 Microsoft PowerShell PSResourceGet 模块

安装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 cmdlet

请参阅以下所有 cmdlet 的列表。

PSResourceGet cmdlet 描述
查找 PSResource 根据名称或其他包属性从存储库(本地或远程)搜索包。
获取安装的 PS 资源 通过 PowerShellGet 返回计算机上安装的模块和脚本。
获取 PSResourceRepository 查找并返回已注册的存储库信息。
获取 PSScriptFileInfo 返回脚本的元数据。
导入-PSGetRepository 查找使用 PowerShellGet 注册的存储库并将其注册到 PSResourceGet。
安装-PSResource 从注册的存储库安装资源。
新的PSScriptFileInfo 该 cmdlet 创建一个新的脚本文件,包括有关该脚本的元数据。
发布-PSResource 将指定模块从本地计算机发布到 PSResource 存储库。
注册-PSResourceRepository 注册 PowerShell 资源的存储库。
保存-PS资源 将注册存储库中的资源(模块和脚本)保存到计算机上。
设置 PSResourceRepository 设置已注册存储库的信息。
测试-PSScriptFileInfo 测试 .ps1 文件中基于评论的元数据,以确保其对于发布有效。
卸载-PSResource 卸载使用 PowerShellGet 安装的资源。
取消注册-PSResourceRepository 从本地计算机中删除已注册的存储库。
更新-PSModuleManifest 更新模块清单文件。
更新-PSResource 下载并安装本地计算机上已安装的软件包的最新版本。
更新-PSScriptFileInfo 此 cmdlet 更新现有脚本 .ps1 文件中基于注释的元数据。

如何使用 Microsoft PowerShell PSResourceGet

让我们看看如何使用 PSResourceGet 安装模块。

安装-PSResource

在我们的示例中,我们希望安装这两个模块:

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

我们不必添加-TrustRepository参数到下面的命令,因为我们已经在之前的安装步骤中将 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

获取安装的 PS 资源

检查机器上安装了哪些模块。

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*" }

让我们获取模块安装的位置路径。

笔记:当你运行安装-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

卸载-PSResource

如果我们想使用 PSResourceGet 卸载模块,我们需要使用卸载-PSResourcecmdlet。

这将删除所有 Microsoft Graph 模块。

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 与 PowerShellGet v2

让我们比较一下 PSResourceGet 和 PowerShellGet v2,看看哪一个在安装和删除模块方面更快。

在此示例中,我们将使用测量命令用于安装和卸载 Microsoft Graph PowerShell 模块的 cmdlet。这是一个非常好的测试,因为它有 39 个模块。

与 PowerShellGet v2 相比,PSResourceGet 速度要快得多。

阅读更多:如何修复 Windows 中的 igfxEM 模块已停止工作错误

cmdlet PSRecourceGet(版本 1.0.2) PowerShellGet v2(版本 2.2.5)
安装-PSResource 58秒
卸载-PSResource 396 毫秒
安装模块 3分14秒
卸载模块 3分48秒

就是这样!

结论

您学习了如何安装 Microsoft PowerShell PSResourceGet 模块。运行 Windows PowerShell 5.1 或 PowerShell 7 并逐步运行命令以安装最新的 PSResourceGet 模块。从现在开始,使用 PSResourceGet 来安装、更新和删除模块。

您喜欢这篇文章吗?您可能还喜欢连接到 Exchange Online PowerShell。不要忘记关注我们并分享这篇文章。