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

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!