Package An automated software install, including PowerShell items
Repository A storage location for packages, online or on-site
Package Manager A command line interface to install packages
Provider Code that knows how to talk to a repository
Source/Gallery A repository holding software to download and install
These are just standard glossary terms, but they are helpful in understand package management in
general.
In the next section, we dive into using Windows PowerShellGet and Nuget to facilitate the installation
of our packages and give you more technical detail about them.
Windows PowershellGet and NuGet
The traditional way to install a Windows PowerShell module is to search for it on the Internet and then
download and install it. Windows Server 2016 changes the way modules are managed on a machine.
It comes with the PowershellGet Windows PowerShell module built in. PowershellGet helps you to
find, download, install, and manage modules on a machine.
PowershellGet works with multiple providers. These providers are client tools that connect to the
module repository represented by Source (location - URI). The most important provider PowershellGet
works with as of Windows Server 2016 is NuGet. NuGet is the package manager for Windows; it
provides the ability to consume not only modules, but also applications and packages. NuGet can
work with multiple sources, but PSGallery is the most common and preferred repository source for
PowershellGet.
All the functionality for module management is included in the PowershellGet module. The first step
for managing modules is to import the module into the Windows PowerShell console. To do that,
start the Windows PowerShell Integrated Scripting Environment (ISE) and then run the following
command to load the PowershellGet module:
PS C:\users\me>> import-module PowershellGet
–
Verbose
VERBOSE: Loading module from path PS C:\WINDOWS\system32> import-module PowerShellGet -Verbose
VERBOSE: Loading module from path 'C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PowerShellGet.psd1'.
VERBOSE: Loading 'FormatsToProcess' from path 'C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSGet.Format.ps1xml'.
VERBOSE: Loading module from path 'C:\Program
Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1'.
VERBOSE: Importing function 'Find-Command'.
VERBOSE: Importing function 'Find-DscResource'.
VERBOSE: Importing function 'Find-Module'.
VERBOSE: Importing function 'Find-RoleCapability'.
VERBOSE: Importing function 'Find-Script'.
VERBOSE: Importing function 'Get-InstalledModule'.
VERBOSE: Importing function 'Get-InstalledScript'.
VERBOSE: Importing function 'Get-PSRepository'.
VERBOSE: Importing function 'Install-Module'.
VERBOSE: Importing function 'Install-Script'.
VERBOSE: Importing function 'New-ScriptFileInfo'.
VERBOSE: Importing function 'Publish-Module'.
VERBOSE: Importing function 'Publish-Script'.
VERBOSE: Importing function 'Register-PSRepository'.
VERBOSE: Importing function 'Save-Module'.
VERBOSE: Importing function 'Save-Script'.
VERBOSE: Importing function 'Set-PSRepository'.
VERBOSE: Importing function 'Test-ScriptFileInfo'.
VERBOSE: Importing function 'Uninstall-Module'.
VERBOSE: Importing function 'Uninstall-Script'.
134
CHAPTER 5 | Systems management
VERBOSE: Importing function 'Unregister-PSRepository'.
VERBOSE: Importing function 'Update-Module'.
VERBOSE: Importing function 'Update-ModuleManifest'.
VERBOSE: Importing function 'Update-Script'.
VERBOSE: Importing function 'Update-ScriptFileInfo'.
VERBOSE: Importing alias 'fimo'.
VERBOSE: Importing alias 'inmo'.
VERBOSE: Importing alias 'pumo'.
VERBOSE: Importing alias 'upmo'.
Using the
Verbose
switch with the Import-Module cmdlet displays all imported functions, cmdlets, and
aliases on the console. There are eight functions, two variables, and four aliases available in this
module.
The first time you use the PowershellGet cmdlet, it verifies the installation of NuGet on the machine. If
NuGet is not installed, a confirmation message box appears, as follows:
PowershellGet requires NuGet_anycpu.exe to interact with NuGet-based galleries. NuGet_anycpu.exe must be
available in 'C:\ProgramData\OneGet\ProviderAssemblies' or
'C:\Users\\AppData\Local\OneGet\ProviderAssemblies'. For more information about NuGet, see
http://www.nuget.org. Do you want PowershellGet to download NuGet_anycpu.exe now?
Clicking Yes downloads and installs NuGet on the machine.
The cmdlets provided by PowershellGet are divided into two broad categories: modules and
repository cmdlets. PowershellGet provides cmdlets for finding, installing, publishing, and updating
modules from the repository. It also provides cmdlets for reading current repository settings as well as
updating, registering, and unregistering them.
Two repositories are available by default: PSGallery and MSPSGallery. PowershellGet uses the NuGet
provider to connect to these repositories. Running Get-PSRepository displays all existing repositories
on the machine.
PS C:\users\me>> Get-PSRepository
Name SourceLocation OneGetProvider InstallationPolicy
---- -------------- -------------- ------------------
PSGallery https://msconfiggallery.cloudapp.net/api/v2/ NuGet Untrusted
MSPSGallery http://www.microsoft.com/ NuGet Trusted
Running the Get-PSRepository cmdlet with a repository name displays configurations related to that
repository.
PS C:\WINDOWS\system32> get-PSRepository -name PSGallery |Format-list *
Name : PSGallery
SourceLocation : https://www.powershellgallery.com/api/v2/
Trusted : False
Registered : True
InstallationPolicy : Untrusted
PackageManagementProvider : NuGet
PublishLocation : https://www.powershellgallery.com/api/v2/package/
ScriptSourceLocation : https://www.powershellgallery.com/api/v2/items/psscrip
ScriptPublishLocation : https://www.powershellgallery.com/api/v2/package/
ProviderOptions : {}
Within the output,
SourceLocation
indicates the URL of the repository location,
PackageManagementProvider
identifies the package provider used to connect to the repository (NuGet
in this case),
Trusted
indicates whether the repository is trusted, and
PublishLocation
shows the URL
used for submission of modules.
Running the Set-PSRepository cmdlet sets the configuration values of a repository. For example, the
Set-PSRepository cmdlet that follows changes the configuration value
Untrusted
to
Trusted
for the
PSGallery
repository. After changing a value, you can review the new configuration by running the
Get-PSRepository cmdlet.
PS C:\Users\me> Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted
PS C:\Users\me> Get-PSRepository
135
CHAPTER 5 | Systems management
Name InstallationPolicy SourceLocation
---- ------------------ --------------
PSGallery Trusted https://www.powershellgallery.com/api/v2/
You use the Register-PSRepository cmdlet to add and register a new repository. To work properly
with the repository, the cmdlet needs the name of the repository, its source location for downloading
modules, the publish location for publishing new modules to the repository, an installation policy, and
the package manager. Running this cmdlet with
Chocolatey
as the name,
http://chocolatey.org/
api/v2/
as both source and publish location,
Trusted
as the installation policy value, and
NuGet
as the
package manager
Name
adds a new module repository to the machine, as follows:
Register-PSRepository -Name "Chocolatey" -SourceLocation "http://chocolatey.org/api/v2/" `
-PublishLocation "http://chocolatey.org/api/v2/" -InstallationPolicy Trusted `
-PackageManageMentProvider NuGet
After registering, the Find-Module cmdlet can search repositories, and the Install-Module cmdlet can
download and install modules.
Chocolatey
is shown here just as an example; it can be any repository
hosting Windows PowerShell modules.
Running Unregister-PSRepository with the
Name
parameter removes a previously registered repository.
PS C:\Users\me> Unregister-PSRepository -Name Chocolatey
The most important function of the PowershellGet module is to find and install modules. Running the
Find-Module cmdlet without any parameter outputs all of the modules available within all the
repositories, as shown here:
PS C:\Users\me> Find-Module
Version Name Repository Description
------- ---- ---------- -----------
2.0.1 AzureRM.profile PSGallery Microsoft Azure PowerShell - Profile credential ...
2.0.1 Azure.Storage PSGallery Microsoft Azure PowerShell - Storage service cmd...
1.7.6 Posh-SSH PSGallery PowerShell module for automating tasks using the...
2.0.1 AzureRM PSGallery Azure Resource Manager Module
..
..
Running the Find-Module cmdlet with the
Name
parameter outputs modules related to that name.
Running this cmdlet with
Bing
as the value for the
Name
parameter provides information about Bing,
as shown here:
PS C:\Users\me> Find-Module -Name "Bing"
Version Name Repository Description
------- ---- ---------- -----------
5.0 Bing PSGallery A few functions for working with the new Bing APIs
5.0 Bing Choclatey A few functions for working with the new Bing APIs
PS C:\Users\me> Find-Module -Name "*ing"
Version Name Repository Description
------- ---- ---------- -----------
2.11.0.0 xNetworking PSGallery Module with DSC Resources for Networking area
0.9.4 AzureRM.MachineLearning PSGallery Microsoft Azure PowerShell - Machine Learning We..
1.0.0.0 xWindowsEventForwarding PSGallery This module can be used to manage configuration ..
2.5.2 PSLogging PSGallery Creates and manages log files for your scripts.
1.2.1 PowerShellLogging PSGallery Captures PowerShell console output to a log file.
5.0 Bing PSGallery A few functions for working with the new Bing APIs
2.0.1 Remote_PSRemoting PSGallery Enable PSRemoting Remotely using WMI
Note The
Name
parameter also accepts wildcard characters.
The Find-Module cmdlet takes the additional parameters
MinimumVersion
and
RequiredVersion
. You
cannot use them both at the same time. To download a specific version, use the
RequiredVersion
parameter. Specify
MinimumVersion
to download the most recent version higher or equal to the
136
CHAPTER 5 | Systems management
MinimumVersion
. Running the Find-Module cmdlet with
Bing
as the value for the
Name
parameter and
4.0
as the value for the
MinimumVersion
parameter finds the Bing module with 5.0 as the output.
PS C:\Users\me> Find-Module -Name "Bing" -MinimumVersion "4.0"
Version Name Repository Description
------- ---- ---------- -----------
5.0 Bing PSGallery A few functions for working with the new Bing APIs
5.0 Bing Choclatey A few functions for working with the new Bing APIs
Running the Find-Module cmdlet with
Bing
as the value for the
Name
parameter and
4.0
as the value
for the
RequiredVersion
parameter results in an error:
PS C:\Users\me> Find-Module -Name "Bing" -RequiredVersion "6.0"
PackageManagement\Find-Package : No match was found for the specified search criteria and module name 'Bing'.
Try
Get-PSRepository to see all available registered module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1249 char:3
+ PackageManagement\Find-Package @PSBoundParameters | Microsoft ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Microsoft.Power...ets.FindPackage:FindPackage) [Find-Package],
Exceptio
n
+ FullyQualifiedErrorId :
NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.FindPackage
However, running the Find-Module cmdlet with
Bing
as the value for the
Name
parameter and
5.0
as
the value for the
RequiredVersion
parameter results in output with details about the Bing module:
PS C:\Users\me> Find-Module -Name "Bing" -RequiredVersion "5.0"
Version Name Repository Description
------- ---- ---------- -----------
5.0 Bing PSGallery A few functions for working with the new Bing APIs
5.0 Bing Choclatey A few functions for working with the new Bing APIs
After finding the relevant modules, the next step is to install the module. PowershellGet provides the
Install-Module cmdlet specifically for this purpose. This cmdlet is very similar to Find-Module. It also
takes
Name
,
RequiredVersion
, and
MinimumVersion
as parameters.
The code block that follows demonstrates running Install-Module with the
Name
parameter and the
Verbose
switch. You can use
MinimumVersion
or
RequiredVersion
along with the
Name
parameter.
Notice that the last line in the output suggests that the module is installed. Also, note that the
modules are downloaded by default at the $env:ProgramFiles\WindowsPowershell\Modules folder
location. Windows PowerShell uses this folder location to install modules.
PS C:\Users\me> Install-Module -Name bing -Repository PSGallery -Verbose -AllowClober
VERBOSE: Repository details, Name = 'PSGallery', Location = 'https://www.powershellgallery.com/api/v2/';
IsTrusted =
'True'; IsRegistered = 'True'.
VERBOSE: Using the provider 'PowerShellGet' for searching packages.
VERBOSE: Using the specified source names : 'PSGallery'.
VERBOSE: Getting the provider object for the PackageManagement Provider 'NuGet'.
VERBOSE: The specified Location is 'https://www.powershellgallery.com/api/v2/' and PackageManagementProvider
is 'NuGet'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='bing'' for ''.
VERBOSE: Total package yield:'1' for the specified package 'bing'.
VERBOSE: Performing the operation "Install-Module" on target "Version '5.0' of module 'Bing'".
VERBOSE: The installation scope is specified to be 'AllUsers'.
VERBOSE: The specified module will be installed in 'C:\Program Files\WindowsPowerShell\Modules'.
VERBOSE: The specified Location is 'NuGet' and PackageManagementProvider is 'NuGet'.
VERBOSE: Downloading module 'Bing' with version '5.0' from the repository
'https://www.powershellgallery.com/api/v2/'.
VERBOSE: Searching repository 'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='Bing'' for ''.
VERBOSE: Searching repository
'https://www.powershellgallery.com/api/v2/FindPackagesById()?id='BetterCredentials'' for
''.
VERBOSE: InstallPackage' - name='BetterCredentials',
version='4.4',destination='C:\Users\johm\AppData\Local\Temp\1152878524'
VERBOSE: DownloadPackage' - name='BetterCredentials',
version='4.4',destination='C:\Users\johm\AppData\Local\Temp\1152878524\BetterCredentials\BetterCredentials.nu
pkg',
137
CHAPTER 5 | Systems management
uri='https://www.powershellgallery.com/api/v2/package/BetterCredentials/4.4.0'
VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/BetterCredentials/4.4.0'.
VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/BetterCredentials/4.4.0'.
VERBOSE: Completed downloading 'BetterCredentials'.
VERBOSE: InstallPackageLocal' - name='BetterCredentials',
version='4.4',destination='C:\Users\johm\AppData\Local\Temp\1152878524'
VERBOSE: InstallPackage' - name='Bing',
version='5.0',destination='C:\Users\johm\AppData\Local\Temp\1152878524'
VERBOSE: DownloadPackage' - name='Bing',
version='5.0',destination='C:\Users\johm\AppData\Local\Temp\1152878524\Bing\Bing.nupkg',
uri='https://www.powershellgallery.com/api/v2/package/Bing/5.0.0'
VERBOSE: Downloading 'https://www.powershellgallery.com/api/v2/package/Bing/5.0.0'.
VERBOSE: Completed downloading 'https://www.powershellgallery.com/api/v2/package/Bing/5.0.0'.
VERBOSE: Completed downloading 'Bing'.
VERBOSE: InstallPackageLocal' - name='Bing',
version='5.0',destination='C:\Users\johm\AppData\Local\Temp\1152878524'
VERBOSE: Catalog file 'BetterCredentials.cat' is not found in the contents of the module 'BetterCredentials'
being installed.
VERBOSE: Installing the dependency module 'BetterCredentials' with version '4.4' for the module 'Bing'.
VERBOSE: Module 'BetterCredentials' was installed successfully to path 'C:\Program
Files\WindowsPowerShell\Modules\BetterCredentials\4.4'.
VERBOSE: Catalog file 'Bing.cat' is not found in the contents of the module 'Bing' being installed.
VERBOSE: Module 'Bing' was installed successfully to path 'C:\Program
Files\WindowsPowerShell\Modules\Bing\5.0'.
At this point, you can use the Bing module by importing it into the current Windows PowerShell
runspace by using the Import-Module cmdlet. After the initial installation, running Update-Module
updates the existing modules. This module takes the Name and RequiredVersion parameters, but it
does not take the MinimumVersion parameter, as shown here:
PS C:\Users\me> Update-Module -Name Bing
PS C:\Users\me> Update-Module -Name "Bing" -RequiredVersion "5.0"
There is also a Publish-Module cmdlet for adding newer modules to the repository.
Windows PowerShell Classes
Windows PowerShell Classes provides a new method to extend the management surfaces of Windows
PowerShell for developers and IT professionals alike. Using PowerShell Classes, these audiences can
create Windows PowerShell artifacts in a traditional manner using formal syntax and semantics from
object-orientated programming.
For example, developers would be familiar with such constructs as classes or methods. Now Windows
PowerShell makes it possible for you to define these natively within the language for future use.
Windows PowerShell Classes, although not a level 200 topic that this book mainly covers, is an
important improvement in the journey of Windows PowerShell so that its appeal to wider audiences
becomes more apparent.
Here are some other elements that Windows PowerShell Classes support:
Define Desired State Configuration resources by using the native Windows PowerShell language
Define custom types (i.e., classes, properties, and methods)
Support debug types
Generate and handle exceptions using formal methods
Now this might seem extremely advanced right now, but we include it today to ensure that you are
aware of the evolution of Windows PowerShell and how it can become an underpinning technology
to line-of-business applications today.
138
CHAPTER 5 | Systems management
Do'stlaringiz bilan baham: |