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
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

Discovering new cmdlets in PowerShell 5/5.1 and Windows Server 2016

PowerShell V5, PowerShell V5.1, and Windows Server 2016 also added new features.

Getting ready

Run the commands in the following recipe on a Windows Server 2016 with Desktop Experience version.

PowerShellGet module

PowerShellGet, formerly known as OneGet, is a module that provides you with a simple way to discover, install, and update PowerShell modules and scripts. It has dependencies on the PackageManagement module, which relies on NuGet. It is an open source project, located at https://github.com/powershell/powershellget.

Refer to Explore PowerShellGet recipe.

PackageManagement module

The cmdlets in the PackageManagement module provide a single interface for software publication, discovery, installation, and inventory.

Refer to the following recipe:

  • Explore PackageManagement
  • Create a PackageManagement repository

Microsoft.PowerShell.Archive module

The Microsoft.Powershell.Archive module contains two useful functions: Compress-Archive and Expand-Archive. These enable you to create and extract ZIP files. With previous versions of PowerShell versions, you managed archives by using the System.IO.Compression namespace from the .Net framework, the Shell.Application com object or software like 7-Zip.

Microsoft.PowerShell.Utility module

The Microsoft.PowerShell.Utility module contains several new cmdlets useful for debugging interactively and within runspaces.

Debugging and runspace Cmdlets include: Get-Runspace, Debug-Runspace, Get-RunspaceDebug, Enable-RunspaceDebug, and Disable-RunspaceDebug, Wait-Debugger, Debug-Job.

These cmdlets enable debugging PowerShell scripts within runspaces and jobs and add additional debugging features for debugging production PowerShell interactively.

Other new modules

Other new modules in this version of PowerShell (and where to find more information about each module) include:

Module

Description

Documentation

ConfigCI

Manage the configurable code integrity policy for Windows

https://technet.microsoft.com/en-us/library/mt634481.aspx

Defender

Manage Windows defender

https://technet.microsoft.com/en-us/library/dn433280.aspx

EventTracingManagement

Manage event tracing for Windows providers and sessions

https://technet.microsoft.com/en-us/library/dn919247.aspx

HgsClient, ShieldedVMDataFile, and ShieldedVMTemplate

Manage the host guardian service, for shielded Hyper-V guest machines.

https://technet.microsoft.com/en-us/library/dn914505.aspx
https://technet.microsoft.com/en-us/library/mt791280.aspx
https://technet.microsoft.com/en-us/library/mt282520.aspx

IISAdministration

Manage IIS replaces WebAdministration cmdlets

https://technet.microsoft.com/en-us/library/mt270166.aspx

NetworkController

Manage the new network controller role in Server 2016

https://technet.microsoft.com/en-us/library/dn859239.aspx

NetworkSwitchManager

Manage supported network switches in Server 2016

https://technet.microsoft.com/en-us/library/mt171434.aspx

Pester

Manage unit tests for PowerShell modules and cmdlets

https://github.com/pester/Pester/wiki

PnpDevice

Cmdlets for managing plug and play devices

https://technet.microsoft.com/en-us/library/mt130251.aspx

StorageQoS and StorageReplica

Support new storage functionality in Server 2016.

https://technet.microsoft.com/en-us/library/mt608557.aspx
https://technet.microsoft.com/en-us/library/mt744543.aspx

Other new cmdlets

Some other useful cmdlets included are:

Explore some of these cmdlets here and in later chapters as well.

How to do it...

  1. Investigate Write-Information by looking at the Write-* commands, and help for the about_Redirection topic:
      Get-Command -Verb Write -Module *Utility
      Get-Help about_Redirection -ShowWindow
  1. Use Write-Information:
      Write-Information "Test"
  1. This produces no output. To resolve, you should inspect and change the $InformationPreference variable:
      Get-Variable "InformationPreference"
      Set-Variable -Name "InformationPreference" -Value "Continue"
  1. Use Write-Information again:
      Write-Information "Test"
  1. Next, set $InformationPreference back to default value:
      $InformationPreference = "SilentlyContinue"
  1. Review the information-related options in the CommonParameters of each command:
      Show-Command Get-Item
  1. Use ConvertFrom-String to get objects from strings; NoteProperties are created with default names:
      "Here is a sentence!" | ConvertFrom-String
      "Here is a sentence!" | ConvertFrom-String | Get-Member
  1. Use -PropertyNames to control the names:
      "Here is a sentence!" | 
ConvertFrom-String -PropertyNames
First,Second,
Third,Fourth
  1. Use -Delimiter to get items from a list:
      "Here,is,a,list!" | 
ConvertFrom-String -PropertyNames First,Second,
Third,Fourth `
-Delimiter ','
  1. You next test the template capabilities of ConvertFrom-String:
      $TextToParse = @'
Animal, Bird
Shape like Square
Number is 42
Person named Bob
'@$Template1 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
'@ConvertFrom-String -TemplateContent $Template1 `
-InputObject $TextToParse
  1. ConvertFrom-String recognizes only one line from the text—the template needs more examples to train the function, so add a second example to the template and test:
      $Template2 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
{[string]Category*:Country} like {[string]Example:Italy}
'@

ConvertFrom-String -TemplateContent $Template2 `
-InputObject $TextToParse
  1. Note three lines are recognized, even the last line that is unusual. Adding another example to our template trains the function enough to recognize all four lines:
      $Template3 = @'
{[string]Category*:Animal}, {[string]Example:Bird}
{[string]Category*:Country} like {[string]Example:Italy}
{[string]Category*:Number} like {[int]Example:99}
'@
ConvertFrom-String -TemplateContent $Template3 `
-InputObject $TextToParse
  1. Experiment with Format-Hex to output values in hexadecimal:
      $TestValue = 
      @"
      This is line 1
      and line 2
      "@
      $TestValue | Format-Hex
  1. Experiment with Get-ClipBoard and Set-Clipboard by selecting some text, then press Ctrl+C to copy to clipboard, then inspect the clipboard:
      #Select this line and press Control-C to copy to clipboard
      $Value = Get-Clipboard
      $Value
  1. Use Set-Clipboard to replace the clipboard value, then Ctrl+V to paste that new value:
      $NewValue = "#Paste This!"
      $NewValue | Set-Clipboard
      #Press Control-V to paste!

How it works...

In step 1, you get the commands with the Write verb in the Microsoft.PowerShell.Utility module. Write-Information is an addition to this module that writes out to a new information stream, which the about_Redirection help topic describes in detail:

In steps 2-5, note that messages from Write-Information are not displayed by default. The $InformationPreference variable controls this behaviour within your PowerShell session.

In step 6, you'll see the CommonParameters now include InformationAction and InformationVariable

More information is available in Get-Help about_CommonParameters:

In step 7 you create a PSCustomObject using ConvertFrom-String with NoteProperties named P1, P2, P3, and P4 that correspond to words separated by whitespace from the input text, with string or char data types:

In step 8, you control the names of the NoteProperties. In step 9 you change the delimiter from the default of whitespace to a comma, thus parsing a comma separated list:

In step 10, you investigate the -TemplateObject parameter to parse inconsistently formatted data. Here you provide one or more patterns by example in the TemplateObject and provide the template along with the text to parse. The template starts with one line as an example, and initially recognizes only one line out of four in the text to match:

In steps 11 and steps 12, you improve the template with each attempt, achieving complete matching results from the Convert-FromString:

In step 13, you use Format-Hex on a here string that contains two lines of text. Note the 0D 0A bytes corresponding to carriage return and line feed (CRLF) between lines:

In step 14 and step 15, you work with Set-Clipboard and Get-Clipboard. By copying any text with Ctrl+C, you then capture that value into a variable with Get-Clipboard. You use Set-Clipboard to change that value, and use Ctrl+V to verify the change.

There's more...

Each PowerShell release comes with release notes that dive into the details of changes introduced with that version. These pages are updated with community contributions, as PowerShell is now partially open source:

The documentation is published on GitHub and accepts contributions from users via pull-requests so users may help improve the documentation. You'll find PowerShell documentation on GitHub at https://github.com/PowerShell/PowerShell-Docs.

Complete documentation is available on TechNet, see the Windows 10 and Server 2016 PowerShell module reference at https://technet.microsoft.com/en-us/library/mt156917.aspx.

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