Book Image

Windows Server Automation with PowerShell Cookbook - Fifth Edition

By : Thomas Lee
Book Image

Windows Server Automation with PowerShell Cookbook - Fifth Edition

By: Thomas Lee

Overview of this book

The Windows Server Automation with PowerShell Cookbook is back with a new edition, featuring over 100 PowerShell recipes that will make your day-to-day work easier. This book is designed to help you learn how to install, configure and use PowerShell 7.2 effectively. To start with, we’ll look at how to install and configure PowerShell 7.2, along with useful new features and optimizations, and show you how the PowerShell compatibility solution bridges the gap to older versions of PowerShell. We’ll also be covering a wide range of fundamental and more advanced use cases, including how to create a VM and set up an Azure VPN, as well as looking at how to back up to Azure. As you progress, you’ll explore topics such as using PowerShell to manage networking and DHCP in Windows Server, objects in Active Directory, Hyper-V, and Azure. We’ll also take a closer look at WSUS, containers and see how to handle modules that are not directly compatible with PowerShell 7. Finally, you’ll also learn how to use some powerful tools to diagnose and resolve issues with Windows Server. By the end of this PowerShell book, you’ll know how to use PowerShell 7.2 to automate tasks on Windows Server 2022 with ease, helping your Windows environment to run faster and smoother.
Table of Contents (17 chapters)
15
Other Books You May Enjoy
16
Index

Exploring Installation Artifacts

Installing PowerShell 7 creates some artifacts that may be useful to better understand how PowerShell 7 and Windows PowerShell differ. The installation folder for PowerShell, as well as the folders holding PowerShell modules, are different from Windows PowerShell.

Getting ready

You run this recipe on SRV1 after you have installed PowerShell 7 and have added a profile file.

How to do it...

  1. Checking the version table for the PowerShell 7 console
    $PSVersionTable
    
  2. Examining the PowerShell 7 installation folder
    Get-ChildItem -Path $env:ProgramFiles\PowerShell\7 -Recurse |
      Measure-Object -Property Length -Sum
    
  3. Viewing the PowerShell 7 configuration JSON file
    Get-ChildItem -Path $env:ProgramFiles\PowerShell\7\powershell*.json |
      Get-Content
    
  4. Checking the initial Execution Policy for PowerShell 7
    Get-ExecutionPolicy
    
  5. Viewing module folders
    $I = 0
    $ModPath = $env:PSModulePath -split ';'
    $ModPath |
      Foreach-Object {
        "[{0:N0}]   {1}" -f $I++, $_
      }
    
  6. Checking the modules
    $TotalCommands = 0
    Foreach ($Path in $ModPath){
      Try { 
       $Modules = Get-ChildItem -Path $Path -Directory -ErrorAction Stop
         "Checking Module Path:  [$Path]"
      }
      catch [System.Management.Automation.ItemNotFoundException] {
        "Module path [$path] DOES NOT EXIST ON $(hostname)"
      }
      $TotalCommands = 0
      foreach ($Module in $Modules) {
        $Cmds = Get-Command -Module ($Module.name)
        $TotalCommands += $Cmds.Count
      }
    }
    
  7. Viewing the totals of the commands and modules
    $Mods = (Get-Module * -ListAvailable | Measure-Object).count
    "{0} modules providing {1} commands" -f $Mods,$TotalCommands
    

How it works...

In step 1, you start the PowerShell 7 console on SRV1. The console should look like this:

Figure 1.23: Checking PowerShell Version information

In step 2, you use Measure-Object to determine how many files exist in the PowerShell installation folder and how much space those files occupy on the disk. The output of this step looks like this:

Figure 1.24: Examining the PowerShell 7 installation folder

PowerShell 7 uses the PWSH.JSON file (in the installation folder) to hold certain key settings. In step 3, you examine this file for PowerShell 7.2.2, with output like this:

Figure 1.25: Viewing the PWSH.JSON configuration file

In step 4, you check the current execution policy for PowerShell 7, with output as follows:

Figure 1.26: Checking the PowerShell 7 execution policy

PowerShell 7, by default, loads modules from a series of folders as described in the PowerShell variable $PSModulepath variable. In step 5, you display the default module folders for PowerShell 7, with output like this:

Figure 1.27: Viewing the module folders for PowerShell 7

In step 6, you look at the modules in each module path and determine the total number of commands available via these modules. The output from this step looks like this:

Figure 1.28: Counting the commands available

In the final step in this recipe, step 7, you view the number of modules and commands provided in those modules, with output like this:

Figure 1.29: Counting the commands available

There’s more...

In step 1, you open a new Windows PowerShell console and view the PowerShell version details. In this case, this is version 7.2.2, although, by the time you read this, the PowerShell team may have created newer versions.

As you can see in step 2, the PowerShell pwsh.json file contains both the execution policy and a list of modules that PowerShell 7 should never attempt to load (as these three modules are known to not work with PowerShell 7, even using the Windows PowerShell compatibility mechanism). For reasons best known to themselves, the owners of these modules have declined the opportunity to port them over to work with PowerShell 7, although that may change.

In step 4, you view the current PowerShell 7 execution policy. Note that this policy is independent of the Windows PowerShell execution policy.

In step 5, you view the current module folders. This step uses .NET composite formatting and the PowerShell -f operator. PowerShell provides you with numerous ways to output this information that you may use, including simple variable expansion. Using the method shown in this step can give you more control over the formatting. You can see another example of this type of formatting in step 7.