Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By : Thomas Lee
Book Image

Windows Server 2019 Automation with PowerShell Cookbook - Third Edition

By: Thomas Lee

Overview of this book

Windows Server 2019 is the latest version of Microsoft’s flagship server operating system. It also comes with PowerShell Version 5.1 and offers a number of additional features that IT professionals will find useful. This book is designed to help you learn how to use PowerShell and manage the core roles, features, and services of Windows Server 2019. You will begin by creating a PowerShell Administrative Environment that features updated versions of PowerShell, the Windows Management Framework, .NET Framework, and third-party modules. Next, you will learn to use PowerShell to set up and configure Windows Server 2019 networking and understand how to manage objects in the Active Directory (AD) environment. The book will also guide you in setting up a host to utilize containers and deploying containers. Further along, you will be able to implement different mechanisms to achieve Desired State Configuration. The book will then get you up to speed with Azure infrastructure, in addition to helping you get to grips with setting up virtual machines (VMs), websites, and file share on Azure. In the concluding chapters, you will be able to deploy some powerful tools to diagnose and resolve issues with Windows Server 2019. By the end of this book, you will be equipped with a number of useful tips and tricks to automate your Windows environment with PowerShell.
Table of Contents (19 chapters)
Windows Server 2019 Automation with PowerShell Cookbook Third Edition
Foreword
Contributors
Preface
Index

Exploring PowerShellGet and the PSGallery


PowerShellGet is a module that enables you to work with external repositories. A repository is a site, either on the internet or internally, that hosts software packages. You use the cmdlets in this module to access one or more repositories that enable you to find, download, and use third-party packages from a package repository.

PowerShellGet leverages mainly the PSGallery repository. This repository, often referred to as a repo, is sponsored by Microsoft and contains a wealth of PowerShell functionalities, including PowerShell modules, DSC resources, PowerShell scripts, and so on. Many of the recipes in this book utilize PSGallery resources.

To some extent, the PowerShellGet module is similar to tools in the Linux world such as apt-get in Ubuntu or RPM in Red Hat Linux.

The PowerShellGet module implements a number of additional *-Module cmdlets that extend the module management cmdlets provided in the Microsoft.PowerShell.Core module.

It's simple and easy to find and install modules from the PSGallery. In some cases, you may wish to download the module to a separate folder. This would allow you to inspect the module, loading it manually before putting it into a folder in $env:PSModulePath (where commands might be auto-loaded).

Getting ready

This recipe runs on the SRV1 server. The recipe also assumes you have performed the previous recipes in this chapter. In particular, you should have added the latest version of the NuGet package provider to your system. If you have not already done so, ensure the provider is installed by performing the following:

Install-PackageProvider -Name NuGet -ForceBootstrap

How to do it...

  1. Review the commands available in the PowerShellGet module:

    Get-Command -Module PowerShellGet
  2. View the NuGet package provider version:

    Get-PackageProvider -Name NuGet |
        Select-Object -Property Version
  3. View the current version of PowerShellGet:

    Get-Module -Name PowerShellGet -ListAvailable
  4. Install the PowerShellGet module from PSGallery:

    Install-Module -Name PowerShellGet -Force
  5. Check the version of PowerShellGet:

    Get-Module -Name PowerShellGet -ListAvailable
  6. View the default PSGallery repositories currently available to PowerShell:

    Get-PSRepository
  7. Review the package providers in the PSGallery repository:

    Find-PackageProvider -Source PSGallery |
      Select-Object -Property Name, Summary |
        Format-Table -Wrap -autosize
  8. Use the Get-Command cmdlet to find Find-* cmdlets in the PowerShellGet module:

    Get-Command -Module PowerShellGet -Verb Find
  9. Get the commands in the PowerShellGet module:

    $Commands     = Find-Command -Module PowerShellGet
    $CommandCount = $Commands.Count
  10. Get the modules included:

    $Modules     = Find-Module -Name *
    $ModuleCount = $Modules.Count
  11. Get the DSC resources available in the PSGallery repository:

    $DSCResources      = Find-DSCResource
    $DSCResourcesCount = $DSCResources.Count
  12. Display the counts:

    "$CommandCount commands available in PowerShellGet"
    "$DSCResourcesCount DSCResources available in PowerShell Gallery"
    "$ModuleCount Modules available in the PowerShell Gallery"
  13. Install the TreeSize module. As this is a public repository, Windows does not trust it by default, so you must approve installation or use -Force:

    Install-Module -Name TreeSize -Force
  14. Review and test the commands in the module:

    Get-Command -Module TreeSize
    Get-Help Get-TreeSize
    Get-TreeSize -Path C:\Windows\System32\Drivers -Depth 1
    
    Uninstall the module:
    Uninstall-Module -Name TreeSize
  15. To inspect prior to installing, first create a download folder:

    $NIHT = @{
      ItemType = 'Directory'
      Path     = "$env:HOMEDRIVE\DownloadedModules"
    }
    New-Item @NIHT
  16. Save the module to the folder:

    $Path = "$env:HOMEDRIVE\DownloadedModules"
    Save-Module -Name TreeSize -Path $Path
    Get-ChildItem -Path $Path -Recurse | format-Table Fullname
  17. To test the downloaded TreeSize module, import it:

    $ModuleFolder = "$env:HOMEDRIVE\downloadedModules\TreeSize"
      Get-ChildItem -Path $ModuleFolder -Filter *.psm1 -Recurse |
        Select-Object -ExpandProperty FullName -First 1 |
          Import-Module -Verbose

How it works...

This recipe uses the cmdlets in the PowerShellGet module to demonstrate how you can obtain and leverage modules and other PowerShell resources from the public PSGallery site (https://www.powershellgallery.com/).

In step 1, you review the commands contained in the PowerShellGet module, which looks like this:

Because the NuGet package provider is required to use the PowerShell Gallery, you need to have this provider loaded. In step 2, you check the version of the provider, which looks like this:

PowerShellGet requires NuGet provider version 2.8.5.201 or newer to interact with NuGet-based repositories, including PSGallery. In this case, you have a later version of the NuGet provider.

In step 3, you check what version of PowerShellGet is currently installed on SRV1, which looks like this:

In step 4, you install the latest version of the PowerShellGet module from PSGallery, which produces no output. In step 5, you view the versions of PowerShellGet that are now available on SRV1, like this:

In step 6, you examine the repositories PowerShell knows about (thus far), like this:

In step 7, you examine other providers contained in the PSGallery, which you can download and use as needed:

To discover some of the things you can find using PowerShellGet, in step 8 you get the commands in the module that use the Find verb, like this:

There are a variety of resources you can obtain from the PSGallery. In step 9, step 10, and step 11, you get the command, modules, and DSC resources respectively that are in the PSGallery. This generates no output. In step 12, you display those counts, which looks like this:

In step 13, you install the TreeSize module from the PSGallery, which generates no output. In step 14, you look at the commands contained in the module (there is only one), then you run the command, which looks like this:

In step 15, you remove the module—this generates no output.

In some cases, you may wish to download the module to a separate folder to enable you to test the module before formally installing it. In step 16, you create a folder in your home drive, generating no output. Next, in step 17, you save the module into that folder and look at what's in the downloaded files folder, which looks like this:

In step 18, you load the module from the download folder. Using the -Verbose switch enables you to see what Import-Module is actually doing. The output is as follows:

Once you have imported the module you can then use either the Get-Treesize function or its alias, TreeSize.

There's more...

In step 3, you discover that the version of the PowerShellGet module on the host is version 1.0.0.1 which ships with Windows 10. Since the initial release of Windows 10, PowerShellGet has become a community-developed project, based at GitHub. The latest version of the module is available both from GitHub or via PSGallery, with the latter being easier to work with for most IT pros. Visit the GitHub site to get more information: https://github.com/PowerShell/PowerShellGet.

In step 4, you added the latest version of the PowerShellGet module onto your system and in step 5, you saw you now had two versions. PowerShell, by default, uses the later version, unless you explicitly load an earlier version prior to using the commands in the module.

In this recipe, you downloaded, used, and removed the TreeSize module—one of thousands of modules you can freely download and use. Other popular modules in the PSGallery include:

  • Azure modules (including MSOnline): Azure provides a large number of smaller modules and most of these are frequently downloaded

  • PowerShellGet and PackageManagement

  • PSWindowsUpdate

  • PSSlack

  • IISAdministration

  • SQLServer

  • CredentialManager

  • Posh-SSH

See also…

For most IT pros, PSGallery is the go-to location for obtaining useful modules that avoid you having to re-invent the wheel. In some cases, you may develop a particularly useful module (or script or DSC resource), which you can publish to the PSGallery to share with others.

See https://docs.microsoft.com/en-us/powershell/gallery/concepts/publishing-guidelines for guidelines regarding publishing to the PSGallery. And, while you are looking at that page, consider implementing best practices in any production script you develop.