Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By : Thomas Lee, Ed Goad
Book Image

Windows Server 2016 Automation with PowerShell Cookbook - Second Edition

By: Thomas Lee, Ed Goad

Overview of this book

This book showcases several ways that Windows administrators can use to automate and streamline their job. You'll start with the PowerShell and Windows Server fundamentals, where you'll become well versed with PowerShell and Windows Server features. In the next module, Core Windows Server 2016, you'll implement Nano Server, manage Windows updates, and implement troubleshooting and server inventories. You'll then move on to the Networking module, where you'll manage Windows network services and network shares. The last module covers Azure and DSC, where you will use Azure on PowerShell and DSC to easily maintain Windows servers.
Table of Contents (21 chapters)
Title Page
Credits
About the Author
Acknowledgment
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

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.