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 PowerShellGet


The PowerShellGet module enables you to work with repositories, sites which contain scripts and modules to download and use. If you have a Linux background, you are familiar with repositories and tools like apt-get (On Ubuntu Linux) and RPM (on Red Hat Linux). PowerShellGet delivers similar functionality within PowerShell.

Note

Ensure you're running with administrator privileges so you can update PowerShellGet to the latest version.

How to do it...

  1. You begin by reviewing the commands available in the PowerShellGet module:
  Get-Command -Module PowerShellGet
  1. Before moving on, you should update to the latest NuGet to get the PackageManagement module current, then update the PowerShellGet module per the GitHub instructions at https://github.com/powershell/powershellget.PowerShellGet has a dependency on PackageManagement, which in turn relies on NuGet. PowerShellGet and PackageMangagement both come within Windows 10 and Server 2016, but Windows updates are less frequent than releases at the PowerShell gallery. Updating ensures you have the latest versions of all the dependencies. To update NuGet:
   Install-PackageProvider -Name NuGet -Force -Verbose
  1. Close your PowerShell session by running Exit and open a new PowerShell session.
  2. Check the version of the NuGetPackageProvider:
  Get-PackageProvider -Name NuGet |      Select-Object Version
  1. Update PowerShellGet:
  Install-Module -Name PowerShellGet -Force
  1. Close your PowerShell session by running Exit and reopen it again.
  2. Check the version of PowerShellGet:
  Get-Module -Name PowerShellGet |      Select-Object -ExpandProperty Version
  1. View the default PSGallery repository for PowerShellGet:
  Get-PSRepository
  1. Review the various providers in the repository:
  Find-PackageProvider |    Select-Object -Property Name, Source, Summary |     Format-Table -Wrap -AutoSize
  1. View available providers with packages in PSGallery:
  Find-PackageProvider -Source PSGallery |      Select-Object -Property Name, Summary |          Format-Table -Wrap -AutoSize
  1. Use the Get-Command cmdlet to find cmdlets in PowerShellGet:
  Get-Command -Module PowerShellGet -Verb Find
  1. Request all the commands in the PowerShellGet module, store them in a variable, and store the count as well:
      $CommandCount = Find-Command |
         Tee-Object -Variable 'Commands' |
            Measure-Object
      "{0} commands available in PowerShellGet" `
                              -f $CommandCount.Count
  1. Review the commands in Out-GridView and note the module names:
  $Commands | Out-GridView
  1. Request all the available PowerShellGet modules, store them in a variable and store the count as well:
      $ModuleCount = Find-Module |
         Tee-Object -Variable 'Modules' |
            Measure-Object
      "{0} Modules available in PowerShellGet" -f $ModuleCount.Count
  1. Review the modules in Out-GridView:
  $Modules | Out-GridView
  1. Request all available DSC resources, store them in a variable, and view them in Out-GridView:
    $DSCResourceCount = Find-DSCResource |
         Tee-Object -Variable 'DSCResources' |
            Measure-Object 
      "{0} DSCResources available in PowerShellGet" -f `
                              $DSCResourceCount.Count
      $DSCResources | Out-GridView
  1. Find the available scripts and store them in a variable. Then view them using Out-GridView:
      $ScriptCount = Find-Script |
         Tee-Object -Variable 'Scripts' |
            Measure-Object 
      "{0} Scripts available in PowerShellGet" -f $ScriptCount.Count
       $Scripts | Out-GridView
  1. When you discover a module you would like to simply install the module. This functionality is similar for Scripts, DSCResources, and so on:
  Get-Command -Module PowerShellGet -Verb Install
  1. Install the TreeSize module, as an example, or choose your own. As this is a public repository, Windows does not trust it by default, so you must approve the installation:
  Install-Module -Name TreeSize -Verbose
  1. If you choose to trust this repository, set the InstallationPolicy to Trusted, and you'll no longer need to confirm each installation: Use at your own risk, you are responsible for all software you install on servers you manage:
  Set-PSRepository -Name PSGallery  -InstallationPolicy Trusted
  1. Review and test the commands in the module:
  Get-Command -Module TreeSize  Get-Help Get-TreeSize -Examples  Get-TreeSize -Path $env:TEMP -Depth 1
  1. Remove the module just as easily:
  Uninstall-Module -Name TreeSize -Verbose
  1. If you would like to inspect the code before installation, download and review the module code:
      New-Item -ItemType Directory `
               -Path $env:HOMEDRIVE\downloadedModules
      Save-Module -Name TreeSize `
               -Path $env:HOMEDRIVE\downloadedModules” +
                     "$env:windirexplorer.exe" 
      $env:HOMEDRIVE\downloadedModules
  1. Import the downloaded module:
      $ModuleFolder = "$env:HOMEDRIVE\downloadedModules\TreeSize"
      Get-ChildItem -Path $ModuleFolder -Filter *.psm1 -Recurse |
         Select-Object -ExpandProperty FullName -First 1 |
            Import-Module -Verbose
  1. When you are done with discovering the new module, you can remove it from your system:
  Remove-Module -Name TreeSize  $ModuleFolder | Remove-Item -Recurse -Force

How it works...

In step 1, you start by reviewing the cmdlets in the PowerShellGet module:

In steps 2-7, you ensure PowerShellGet and its dependency PackageManagement are up to date by updating the NuGet provider, verifying the version, then restarting your PowerShell session and updating PowerShellGet and verifying its version.

The -Verbose flag gives you more details on the installation, but it is not required. Note that you must Exit your session after running this command and reopen to continue with the latest version.

Check our NuGet provider version after reopening our PowerShell session:

In step 6-7, you update the PowerShellGetmodule:

Note that you must exit your session after running this command and reopen to continue with the latest version.

In step 8, check your PowerShellGet version after reopening your PowerShell session:

In step 9, you use Get-PSRepository. PowerShellGet starts with a single repository PSGallery installed by default:

In step 10, review the package providers available:

Note the source column; the first three providers listed correspond to NuGet, OneGet, and Chocolatey providers. NuGet is a repository devoted to developer libraries. OneGet was the name of this module (and repository) but has been deprecated and replaced by PackageManagement. You explore Chocolatey in a later recipe. The remaining rows are the available providers in the PSGallery repository.

In step 11, you limit your repository search with Find-PSRepository by specifying the -Source PSGallery parameter:

In step 12, you discover the PowerShellGet commands containing the verb Find:

In steps 13 - 18, you use the Find-* commands to store the available commands, modules, DSC resources, and scripts into variables, then explore what is available using Out-GridView (including using the built-in filter capability to search for a module), for example:

In step 19, you review the install commands in the PowerShellGet module. Their functions are very similar:

In step 20, the TreeSize module looks like an interesting tool to inspect folders and their sizes. Install it by using the Install-Module cmdlet. You use the -Verbose switch to get more information about what the cmdlet is doing:

After confirming the Untrusted repository pop up dialog, PowerShell installs the module.

In step 21, you see that the code available on PSGallery, as well as other public repositories, is just that, public. You must choose to trust the code you download from the internet to take advantage of the functionality provided by that code. To trust this repository and disable prompting, use the command (at your own risk and responsibility):

Set-PSRepository -Name PSGallery -InstallationPolicy Trusted

In step 22, you evaluate and test the module:

In step 23, uninstalling a module is simple:

In step 24, if you prefer, download code and inspect it before installing, using Save-Module, then browse the module's files in Windows Explorer:

In step 25, after reviewing the code, import the module by locating the .psm1 file which defines the module, using Get-ChildItem, then piping that filename to Import-Module:

In step 26, you uninstall the module from your session and delete the module's folder. You may, of course, wish to keep the module!

There's more...

There are a wealth of other resources in the PSGallery—you use the Find-* cmdlets to explore the online resources you can download and use:

The PowerShellGet module enables search for commands, DSC resources, modules, role capabilities, a feature of Just Enough Administration (JEA), and scripts. You can download and use these various tools, or leverage them to build your own custom scripts.

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