Table of contents

PackageManagement Cmdlet

JuanPablo Jofre|上次更新日期: 2016/12/14
2 参与人员

这是 PackageManagement 用以支持软件发现、安装和盘存 (SDII) 的核心。 尝试进行这些操作试用以下 cmdlet:

  • Find-Package
  • Find-PackageProvider
  • Get-Package
  • Get-PackageProvider
  • Get-PackageSource
  • Import-PackageProvider
  • Install-Package
  • Install-PackageProvider
  • Register-PackageSource
  • Save-Package
  • Set-PackageSource
  • Uninstall-Package
  • Unregister-PackageSource

由于 PackageManagement 是一个 PowerShell 模块,因此可以执行以下操作来更新 PackageManagement 本身:

PS C:\> Install-Module PackageManagement –Force

在此示例中,必须重新输入 PowerShell 会话以切换到新版本的 PackageManagement。

Find-Package Cmdlet

此 cmdlet 允许使用已加载的包提供程序发现可用包源中的软件包。

# Find all available Windows PowerShell module packages from galleries registered
# with PowerShellGet provider
Find-Package -Provider PowerShellGet -Source PSGallery

# Find a package from a provider that is not yet installed
# This will bootstrap NuGet provider and then search for jquery package using NuGet
# with <> as source
Find-Package -Name jquery –Provider NuGet -Source

# Find package with name and version
# Here we are assuming that the user already registered using
# Register-PackageSource. You can specify either the provider or the source, or
# neither. For the latter, performance may be less optimal as it searches through all
# the providers and registered sources.
Find-Package -Name jquery –Provider NuGet –RequiredVersion 2.1.4 -Source

Find-PackageProvider Cmdlet

Find-PackageProvider cmdlet 用于查找匹配的 PackageManagement 提供程序,这些提供程序在已向 PowerShellGet 注册的包源中可用。 这些包提供程序都能用 Install-PackageProvider cmdlet 直接进行安装。 默认情况下,这包括 PowerShell 库中带“PackageManagement”和“Provider”标记的可用模块。

Find-PackageProvider 还可以查找匹配的 PackageManagement 提供程序,这些提供程序在 PackageManagement Azure Blob 存储区可用,通过该存储区我们可以使用 PackageManagement Boostrapper 提供程序查找并安装它们。

#Find all available package providers in PackageManagement azure blob store as well as in

#Find all versions of a provider
Find-PackageProvider -Name "Nuget" -AllVersions

#Find a provider from a specified source
Find-PackageProvider -Name "Gistprovider" -Source "PSGallery"

#For machines without internet, you can download the nuget provider first, put it to you file share and then use the following to install the nuget provider (TP5 or later).

Find-PackageProvider -Source  C:\sharedfolder\Providers\
Install-PackageProvider -Source C:\sharedfolder\Providers\ -Name nuget -force

Get-Package Cmdlet

此 cmdlet 可以返回所有已使用 PackageManagement 安装的软件包列表。

# Get all the packages installed by Programs provider
Get-Package –Provider Programs

# Get all the packages installed by NuGet provider at c:\test using the dynamic
# parameter destination
Get-Package –Provider NuGet -Destination c:\test

Get-PackageProvider Cmdlet

可以通过使用 cmdlet 列出已加载并可用于本地计算机的包提供程序清单。

# Get all currently loaded package providers

# The following cmdlet will show all the package providers available on the machine (including those that are not loaded):
Get-PackageProvider -ListAvailable

Get-PackageSource Cmdlet

此 cmdlet 用于获取已注册包提供程序的包源列表。

# Get all package sources

# Get all package sources for a specific provider
Get-PackageSource –ProviderName PowerShellGet

Import-PackageProvider Cmdlet

此 cmdlet 用于将 Package Management 包提供程序添加到当前会话。

# Import a package provider from the local machine
Import-PackageProvider –Name MyProvider

#The -Name parameter can be either the name of the provider or the full path to the provider. Currently, we support .dll, .exe and.psm1 for the full path case. If the name of the provider is used for the -Name parameter, then additional version parameters such as -RequiredVersion, -MinimumVersion and -MaximumVersion may be specified. Otherwise, the latest version of the provider will be imported.

#If a package provider is not yet loaded to your system, we can discover and install on-demand. You can use explicit discovery and install cmdlets to do so:
 Install-PackageProvider –Name MyProvider

#After installed, follow the Import-PackageProvider to load it to your system.

# Import a specific version of a package provider. PackageManagement supports installations of multiple versions of a package provider using PackageProvider cmdlets (not by bootstrapper provider). You can install another version of a package provider given that you already have one up running by:
Find-PackageProvider –Name "Nuget" -AllVersions
Install-PackageProvider -Name "Nuget" -RequiredVersion "" -Force
Get-PackageProvider –ListAvailable
Import-PackageProvider –Name "Nuget" -RequiredVersion "" -Verbose
Import-PackageProvider –Name MyProvider –RequiredVersion xxxx -force

As of the Windows Server Technical Preview(TP5), Install-PackageProvider does install as well as import the provider. Hence after you run find-packageprovider and install-packageprovider, the provider should be ready to use

Install-Package Cmdlet

此 cmdlet 允许使用已加载的包提供程序在可用包源中安装软件包。

# Install a package by name.
# NuGet provider requires us to provide the dynamic parameter destination path
# when we use this provider to install. Not all providers will require you to supply
# dynamic parameters for PackageManagement cmdlets.
Install-Package -Name jquery -Source -Destination c:\test

# Install a package by piping.
Find-Package -Name jquery –Provider NuGet | Install-Package -Destination c:\test

Install-PackageProvider Cmdlet

此 cmdlet 用于安装一个或多个 Package Management 包提供程序。

# Install a package provider from the PowerShell Gallery
Install-PackageProvider –Name "Gistprovider" -Verbose

# Install a specified version of a package provider
Find-PackageProvider –Name "Nuget" -AllVersions
Install-PackageProvider -Name "Nuget" -RequiredVersion "" -Force

# Find a provider and install it
Find-PackageProvider –Name "Gistprovider" | Install-PackageProvider -Verbose

# Install a provider to the current user’s module folder
Install-PackageProvider –Name Gistprovider –Verbose –Scope CurrentUser

Register-PackageSource Cmdlet

此 cmdlet 用于为指定的包提供程序添加包源。 每个 PackageManagement 提供程序可能有一个或多个软件源或存储库。 PackageManagement 可提供 PowerShell cmdlet 来添加/删除/查询源。 例如,你可以为 NuGet 提供程序注册包源:

Register-PackageSource -Name "NugetSource" -Location "" –ProviderName nuget

Save-Package Cmdlet

此 cmdlet 用于将包保存到本地计算机,但不安装它们。

# Saves jquery package to c:\test using NuGetProvider
# Notes that the -Path parameter must point to an existing location
Save-Package -Name jquery –Provider NuGet -Path c:\test

# Save a package by piping.
Find-Package -Name jquery -Source | Save-Package -Path c:\test
Find-Package -source c:\test

Set-PackageSource Cmdlet

此 cmdlet 可更改关于现有包源的信息。

#Set-PackageSource changes the values for a source that has already been registered by running the Register-PackageSource cmdlet. By #running Set-PackageSource, you can change the source name and location.
Set-PackageSource  -Name -Location -NewName nuget2 -NewLocation

Uninstall-Package Cmdlet

此 cmdlet 可卸载安装在本地计算机上的包。

# Uninstall jquery using nuget
Uninstall-Package -Name jquery –Provider NuGet -Destination c:\test

# Uninstall a package with by piping with Get-Package
Get-Package -Name jquery –Provider NuGet -Destination c:\test | Uninstall-Package

Unregister-PackageSource Cmdlet

# Unregister a package source for the NuGet provider. You can use command Unregister-PackageSource, to disconnect with a repository, and Get-PackageSource, to discover what the repositories are associated with that provider.
Unregister-PackageSource  -Name "NugetSource"
© 2017 Microsoft