上QQ阅读APP看书,第一时间看更新
How to do it...
- 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
- Use Write-Information:
Write-Information "Test"
- This produces no output. To resolve, you should inspect and change the $InformationPreference variable:
Get-Variable "InformationPreference" Set-Variable -Name "InformationPreference" -Value "Continue"
- Use Write-Information again:
Write-Information "Test"
- Next, set $InformationPreference back to default value:
$InformationPreference = "SilentlyContinue"
- Review the information-related options in the CommonParameters of each command:
Show-Command Get-Item
- 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
- Use -PropertyNames to control the names:
"Here is a sentence!" |
ConvertFrom-String -PropertyNames First,Second,
Third,Fourth
- Use -Delimiter to get items from a list:
"Here,is,a,list!" |
ConvertFrom-String -PropertyNames First,Second,
Third,Fourth `
-Delimiter ','
- 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
- 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
- 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
- Experiment with Format-Hex to output values in hexadecimal:
$TestValue = @" This is line 1 and line 2 "@ $TestValue | Format-Hex
- 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
- 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!