Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
上QQ阅读APP看书,第一时间看更新

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.