Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Server 2016 Automation with PowerShell Cookbook

You're reading from   Windows Server 2016 Automation with PowerShell Cookbook Powerful ways to automate and manage Windows administrative tasks

Arrow left icon
Product type Paperback
Published in Sep 2017
Publisher
ISBN-13 9781787122048
Length 660 pages
Edition 2nd Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Thomas Lee Thomas Lee
Author Profile Icon Thomas Lee
Thomas Lee
Arrow right icon
View More author details
Toc

Table of Contents (14) Chapters Close

Preface 1. What's New in PowerShell and Windows Server FREE CHAPTER 2. Implementing Nano Server 3. Managing Windows Updates 4. Managing Printers 5. Managing Server Backup 6. Managing Performance 7. Troubleshooting Windows Server 2016 8. Managing Windows Networking Services 9. Managing Network Shares 10. Managing Internet Information Server 11. Managing Hyper-V 12. Managing Azure 13. Using Desired State Configuration

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.

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 NuGet PackageProvider:
      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.

You have been reading a chapter from
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
Published in: Sep 2017
Publisher:
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.
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 $19.99/month. Cancel anytime
Banner background image