-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Windows Server 2016 Automation with PowerShell Cookbook - Second Edition
By :
PowerShell V5, PowerShell V5.1, and Windows Server 2016 also added new features.
Run the commands in the following recipe on a Windows Server 2016 with Desktop Experience version.
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.
The cmdlets in the PackageManagement module provide a single interface for software publication, discovery, installation, and inventory.
Refer to the following recipe:
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.
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 in this version of PowerShell (and where to find more information about each module) include:
Module | Description | Documentation |
| Manage the configurable code integrity policy for Windows | |
| Manage Windows defender | |
| Manage event tracing for Windows providers and sessions | |
| Manage the host guardian service, for shielded Hyper-V guest machines. | https://technet.microsoft.com/en-us/library/dn914505.aspxhttps://technet.microsoft.com/en-us/library/mt791280.aspxhttps://technet.microsoft.com/en-us/library/mt282520.aspx |
|
| |
| Manage the new network controller role in Server 2016 | |
| Manage supported network switches in Server 2016 | |
| Manage unit tests for PowerShell modules and cmdlets | |
| Cmdlets for managing plug and play devices | |
| Support new storage functionality in Server 2016. | https://technet.microsoft.com/en-us/library/mt608557.aspxhttps://technet.microsoft.com/en-us/library/mt744543.aspx |
Some other useful cmdlets included are:
Write-Information : A replacement for the Write-Host cmdlet that is consistent with the other Write-* cmdlets in the Microsoft.PowerShell.Utility namespace. See https://blogs.technet.microsoft.com/heyscriptingguy/2015/07/04/weekend-scripter-welcome-to-the-powershell-information-stream/.ConvertFrom-String and Convert-String: The new string parsing functions that create structured data from strings, or parse out string data into structured data. See https://blogs.msdn.microsoft.com/powershell/2014/10/31/convertfrom-string-example-based-text-parsing/.Format-Hex: This cmdlet formats information into hexadecimal.Get-Clipboard and Set-Clipboard: A cmdlet to simplify working with the clipboard, replacing piping to clip.exe.Clear-RecycleBin: This cmdlet empties the Recycle Bin.New-TemporaryFile: Simplifies the creation of temporary files within PowerShell scripts.New-Guid: A wrapper for [GUID]::NewGuid() to simplify the creation of Globally Unique Identifiers (GUIDs). A GUID is an identifier, unique in space and time, that you use in a variety of scenarios. System Center Virtual Machine Manager, for example, uses GUIDs in jobs created by the UI.Enter-PSHostProcess and Exit-PSHostProcess: These enable you to debug PowerShell processes outside the current host process.Export-ODataEndpointProxy: This cmdlet generates a wrapper module for working with an OData endpoint. See https://msdn.microsoft.com/en-us/powershell/reference/5.1/microsoft.powershell.odatautils/microsoft.powershell.odatautils.Explore some of these cmdlets here and in later chapters as well.
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
Write-Information: Write-Information "Test"$InformationPreference variable:Get-Variable "InformationPreference" Set-Variable -Name "InformationPreference" -Value "Continue"
Write-Information again: Write-Information "Test"$InformationPreference back to default value: $InformationPreference = "SilentlyContinue" Show-Command Get-ItemConvertFrom-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
-PropertyNames to control the names:"Here is a sentence!" | ConvertFrom-String -PropertyNames First,Second, Third,Fourth
-Delimiter to get items from a list:"Here,is,a,list!" | ConvertFrom-String -PropertyNames First,Second, Third,Fourth ` -Delimiter ','
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
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
$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
Format-Hex to output values in hexadecimal:$TestValue = @" This is line 1 and line 2 "@ $TestValue | Format-Hex
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
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!
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.
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.
Change the font size
Change margin width
Change background colour