Reader small image

You're reading from  Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

Product typeBook
Published inSep 2017
Reading LevelBeginner
Publisher
ISBN-139781787122048
Edition2nd Edition
Languages
Right arrow
Authors (2):
Thomas Lee
Thomas Lee
author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

 Ed Goad
Ed Goad
author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad

View More author details
Right arrow

Exploring PackageManagement


PowerShellGet is a powerful resource for PowerShell, built on top of the core PackageManagement capabilities of PowerShell 5. It is one of many PackageManagment providers available, as shown here:

Image Source: https://blogs.technet.microsoft.com/packagemanagement/2015/04/28/introducing-packagemanagement-in-windows-10/

PackageManagement is a unified interface for software package management systems, a tool to manage package managers. You use the PackageManagement cmdlets to perform software discovery, installation, and inventory (SDII) tasks. PackageManagement involves working with package providers, package sources, and the software packages themselves.

Within the PackageManagement architecture, PackageManagement providers represent the various software installers that provide a means to distribute software via a standard plug-in model using the PackageManagement APIs. Each PackageManagement provider manages one or more package sources or software repositories. Providers may be publicly available or can be created within an organization to enable developers and system administrators to publish or install propriety or curated software packages.

PackageManagement Core is effectively an API. The core includes a set of PowerShell cmdlets that enable you to discover available software packages, as well as to install, uninstall, update, and inventory packages using PackageManagement.

Each PackageManagement provider is a different installer technology or package manager that plugs-in via the PackageManagement API. PowerShellGet, NuGet, and Chocolatey are examples of PackageManagement providers.

Each provider is made up of one or more sources, which may be public or private. For example, NuGet has a public source, but your organization may add private sources for the NuGet provider, enabling curation of approved software to make it available to corporate developers.

How to do it...

You use the cmdlets within the PackageManagement module to explore the capabilities it provides.

  1. Review the cmdlets in the PackageManagement module:
  Get-Command -Module PackageManagement
  1. Review the installed providers with Get-PackageProvider:
  Get-PackageProvider | Select-Object -Property Name, Version
  1. The provider list includes msi, msu, and Programs package providers. These providers expose applications and updates installed on your computer which you can explore:
      Get-Package -ProviderName msi |
         Select-Object -ExpandProperty Name
      Get-Package -ProviderName msu | 
         Select-Object -ExpandProperty Name
      Get-Package -ProviderName Programs | 
         Select-Object -ExpandProperty Name
  1. The NuGet source contains developer library packages. This functionality is outside the scope of this book, but worth exploring if you do Windows or web development:
  Get-PackageProvider -Name NuGet
  1. There are also other package providers you can explore:
     Find-PackageProvider |
         Select-Object -Property Name,Summary |
             Format-Table -Wrap -AutoSize
  1. Notice Chocolatey, which is a very useful tool for Windows administrators and power users. Those with some Linux background may think of Chocolatey as apt-get for Windows. You cannot use this provider until you install it and confirm the installation:
  Install-PackageProvider -Name Chocolatey -Verbose
  1. Verify Chocolatey is now in the list of installed providers:
  Get-PackageProvider | Select-Object Name,Version
  1. Look for available software packages from the Chocolatey package provider. Store these in a variable so you don't request the collection more than once, and explore it:
      $AvailableChocolateyPackages = `
          Find-Package -ProviderName Chocolatey
      # How many software packages are available at Chocolatey?
      $AvailableChocolateyPackages | Measure-Object
  1. Pipe to Out-GridView to search for interesting software packages from Chocolatey:
      $AvailableChocolateyPackages |
         Sort-Object Name,Version | 
            Select-Object Name, Version, Summary |
               Out-GridView
  1. Install one or more packages. sysinternals is a good example to use. Use -Verbose to get details on the installation:
    Install-Package -ProviderName Chocolatey `
                      -Name sysinternals `
                      -Verbose
  1. Review installed Chocolatey packages, stored to C:\chocolatey\ by default, this path is stored in the $env:ChocolateyPath environment variable. Then review the executable files included with the sysinternals package:
      Get-ChildItem -Path $env:ChocolateyPath\lib |
         Select-Object -Property Name
      Get-ChildItem -Path `
         $env:ChocolateyPath\lib\sysinternals.2016.11.18\tools `
                    -Filter *.exe | 
         Select-Object -Property Name
  1. Run any installed command included with sysinternals:
      $PSInfoCommand = `
      ‘C:\Chocolatey\lib\sysinternals.2016.11.18\tools\PsInfo.exe’
      Invoke-Expression -Command $PSInfoCommand
  1. Installed packages are enumerated with Get-Package and updated using the same command to install them, Install-Package:
  Get-Package -ProviderName Chocolatey | 
         Install-Package -Verbose

How it works...

In step 1, you review the cmdlets available in the PackageManagement module:

In step 2, you use the Get-PackageProvider cmdlets to display the currently installed package providers:

In step 3, you use Get-Package with the -ProviderName parameter to review packages installed via the msi, msu, and Programs package providers:

In step 4, review the NuGet provider:

In step 5, search for other package providers:

In step 6, you use Install-PackageProvider to install the Chocolatey provider. Since it is untrusted as a public source, you must approve the installation (at your own risk and responsibility):

In this example, you run Install-PackageProvider from within the ISE. By default, this pops up a confirmation dialog. If you run this cmdlet from the PowerShell console, you see a prompt there. You can suppress these confirmation requests by including the parameter -Confirm:$False.

In step 7, you verify Chocolatey is now installed as a package provider:

In step 8, retrieve a list of available software packages from the ChocolateyPackageProvider, store as a variable, and count the available packages:

In step 9, pipe the variable to Out-GridView and use the filter feature to explore what is available. This example is filtering for the Sysinternals package:

In step 10, you install this package (or any package you choose):

In step 11, you review the installed Chocolatey packages, and the files contained within the sysinternals package folder:

In step 12, run any Sysinternals command, for example, PsInfo.Exe :

In step 13, you enumerate the installed packages with Get-Package. As time goes by, packages can be updated with bug fixes, new features, and so on. You can update all the installed packages if any updates exist, as follows:

There's more...

Details of NuGet package and its functionality are outside the scope of this book, but worth exploring if you do Windows or web development. More information on NuGet packages is available from https://www.nuget.org/Packages.

Chocolatey has both a command-line interface and a PowerShell module. The command line interface offers functionality comparable to the PackageManagement module, targeted toward end users and system administrators. Chocolatey is supported on any Windows PC running Windows 7 or later. You can get more information on installing and using Chocolatey via the command line from https://chocolatey.org/install.

Sysinternals is a must-have toolkit for Windows administrators. You can find additional training on the Sysinternals tools on the Channel 9 website at https://channel9.msdn.com/Series/sysinternals.

Previous PageNext Page
You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017Publisher: ISBN-13: 9781787122048
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Authors (2)

author image
Thomas Lee

Thomas Lee is a consultant/trainer/writer based in the UK and has been in the IT business since the late 1960s. After graduating from Carnegie Mellon University, Thomas joined ComShare where he was a systems programmer building the Commander II time-sharing operating system, a forerunner of today's cloud computing paradigm. In the mid-1970s, he moved to ICL to work on the VME/K operating system. After a sabbatical in 1980/81, he joined Accenture, leaving in 1988 to run his own consulting and training business, which is still active today. Thomas holds numerous Microsoft certifications, including MCSE (one of the first in the world) and later versions, MCT (25 years), and was awarded Microsoft's MVP award 17 times.
Read more about Thomas Lee

author image
Ed Goad

Ed Goad is a systems architect who has been working in various roles in the IT field for 16 years. He first became interested in scripting and automation when presented with a task to uninstall software from over 1,000 systems with limited time and resources. He has worked with scripting and automation on multiple platforms and languages including PowerShell, VBscript, C#, and BASH scripting. Ed currently holds multiple Microsoft certifications, most recently including the Microsoft Certified IT Professional Enterprise Administrator. Additional non-Microsoft certifications include VMware Certified Professional (VCP), Red Hat Certified System Administrator (RHCSA), EMC Proven Professional, Brocade Certified Network Engineer (BCNE), and Cisco Certified Network Associate (CCNA). Ed is currently on a sabbatical and volunteering full time at the Amor Fe y Esperanza school in Honduras(http://www.afehonduras.org). There he is teaching computer and math classes to the kids who live and work in the garbage dump outside of the capital city of Tegucigalpa.
Read more about Ed Goad