如何安装 Microsoft PowerShell PSResourceGet 模块
PSResourceGet 是一个模块,其中包含用于发现、安装、更新和发布 PowerShell 工件(例如模块、DSC 资源、角色功能和脚本)的命令。 PSResourceGet 是最新的包管理器,取代了 PowerShellGet v2 模块。在本文中,您将学习如何安装 Microsoft PowerShell PSResourceGet 模块。
Microsoft PowerShell PSResourceGet
PSResourceGet 模块提供了在 PowerShell Gallery 等存储库中安装、更新和定位模块和脚本的功能。
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。运行命令Install-Module PowershellGet -Force。当要求安装 NuGet 提供程序时,按 Y 并按 Enter 键。
Install-Module PowershellGet -Force如果您收到无法安装的错误,请阅读文章无法安装 PowerShell 的 NuGet 提供程序。
3. 安装 Microsoft PowerShell PSResourceGet 模块
安装 Microsoft PowerShell PSResourceGet 模块。
Install-Module -Name Microsoft.PowerShell.PSResourceGet -Force4.信任PowerShell Gallery存储库
安装 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 的列表。
- Find-PSResource
-
根据名称或其他包属性从存储库(本地或远程)搜索包。
- Get-InstalledPSResource
通过 PowerShellGet 返回计算机上安装的模块和脚本。
- Get-PSResourceRepository
查找并返回已注册的存储库信息。
- Get-PSScriptFileInfo
返回脚本的元数据。
- Import-PSGetRepository
查找使用 PowerShellGet 注册的存储库并将其注册到 PSResourceGet。
- Install-PSResource
从注册的存储库安装资源。
- New-PSScriptFileInfo
该 cmdlet 创建一个新的脚本文件,包括有关该脚本的元数据。
- Publish-PSResource
将指定模块从本地计算机发布到 PSResource 存储库。
- Register-PSResourceRepository
注册 PowerShell 资源的存储库。
- Save-PSResource
将注册存储库中的资源(模块和脚本)保存到计算机上。
- Set-PSResourceRepository
-
设置已注册存储库的信息。
- Test-PSScriptFileInfo
测试 .ps1 文件中基于评论的元数据,以确保其对于发布有效。
- Uninstall-PSResource
卸载使用 PowerShellGet 安装的资源。
- Unregister-PSResourceRepository
从本地计算机中删除已注册的存储库。
- Update-PSModuleManifest
更新模块清单文件。
- Update-PSResource
下载并安装本地计算机上已安装的软件包的最新版本。
- Update-PSScriptFileInfo
此 cmdlet 更新现有脚本 .ps1 文件中基于注释的元数据。
如何使用 Microsoft PowerShell PSResourceGet
让我们看看如何使用 PSResourceGet 安装模块。
安装-PSResource
在我们的示例中,我们希望安装这两个模块:
Microsoft Graph PowerShell 模块
Microsoft Graph Beta PowerShell 模块
我们不必在下面的命令中添加 -TrustReposority 参数,因为我们已经在之前的安装步骤中将 PSGallery 添加到了受信任的存储库中。但你可能忽略了它,所以让我们添加它。
注意: 使用命令中的-Reinstall 参数来安装最新版本的模块,即使已经安装了最新版本。安装的版本被覆盖。这允许您修复损坏的模块安装。如果安装了旧版本的模块,则新版本会并排安装在新版本特定的文件夹中。
Install-PSResource Microsoft.Graph -Reinstall -Scope AllUsers -TrustRepositoryInstall-PSResource Microsoft.Graph.Beta -Reinstall -Scope AllUsers -TrustRepository如果您想安装特定的模块版本,请使用-Version参数。
Install-PSResource Microsoft.Graph -Version 2.14.0 -Scope AllUsers -TrustRepositoryInstall-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*" }让我们获取模块安装的位置路径。
注意:当您运行Install-PSResource cmdlet 来安装模块时,可以将其安装在从 Windows PowerShell 5.1 或 PowerShell 7 运行时的不同位置。
Get-InstalledPSResource -Scope AllUsers | ft Name, InstalledLocation下面的文件夹路径是安装模块的默认位置。您可以在文件资源管理器中打开以下文件夹。
当前用户的 Windows PowerShell 5.1:
%USERPROFILE%\Documents\WindowsPowerShell\Modules适用于所有用户的 Windows PowerShell 5.1:
C:\Program Files\WindowsPowerShell\Modules当前用户的 PowerShell 7:
%USERPROFILE%\Documents\PowerShell\Modules适用于所有用户的 PowerShell 7:
C:\Program Files\PowerShell\Modules卸载-PSResource
如果我们想使用 PSResourceGet 卸载模块,则需要使用 Uninstall-PSResource cmdlet。
这将删除所有 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 -SkipDependencyCheckGet-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Where-Object { $_.Name -like "Microsoft.Graph.Beta*" } | Uninstall-PSResource -Scope AllUsers -SkipDependencyCheckPSResourceGet 与 PowerShellGet v2
让我们比较一下 PSResourceGet 和 PowerShellGet v2,看看哪一个在安装和删除模块方面更快。
在此示例中,我们将使用 Measure-Command cmdlet 安装和卸载 Microsoft Graph PowerShell 模块。这是一个非常好的测试,因为它有 39 个模块。
与 PowerShellGet v2 相比,PSResourceGet 速度要快得多。
| Cmdlets | PSRecourceGet (version 1.0.2) | PowerShellGet v2 (version 2.2.5) |
|---|---|---|
| Install-PSResource | 58 seconds | – |
| Uninstall-PSResource | 396 milliseconds | – |
| Install-Module | – | 3 minutes and 14 seconds |
| Uninstall-Module | – | 3 minutes and 48 seconds |
就是这样!
了解更多:如何安装和更新 PowerShell 7 »
结论
您学习了如何安装 Microsoft PowerShell PSResourceGet 模块。运行 Windows PowerShell 5.1 或 PowerShell 7 并逐步运行命令以安装最新的 PSResourceGet 模块。从现在开始,使用 PSResourceGet 来安装、更新和删除模块。
您喜欢这篇文章吗?您可能还喜欢连接到 Exchange Online PowerShell。不要忘记关注我们并分享这篇文章。