# take a simple XAML template with some text $xaml = @" <TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">$Text</TextBlock> "@
Add-Type-AssemblyName PresentationFramework
# turn it into a UIElement $reader = [XML.XMLReader]::Create([IO.StringReader]$XAML) $result = [Windows.Markup.XAMLReader]::Load($reader)
# render it in memory to the desired size $result.Measure([System.Windows.Size]::new([Double]::PositiveInfinity, [Double]::PositiveInfinity)) $result.Arrange([System.Windows.Rect]::new($result.DesiredSize)) $result.UpdateLayout()
# write it to a bitmap and save it as PNG $render = [System.Windows.Media.Imaging.RenderTargetBitmap]::new($result.ActualWidth, $result.ActualHeight, 96, 96, [System.Windows.Media.PixelFormats]::Default) $render.Render($result) Start-Sleep-Seconds1 $encoder = [System.Windows.Media.Imaging.PngBitmapEncoder]::new() $encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($render)) $filestream = [System.IO.FileStream]::new($filename, [System.IO.FileMode]::Create) $encoder.Save($filestream)
# clean up $reader.Close() $reader.Dispose()
$filestream.Close() $filestream.Dispose()
# return the file name for the generated image $filename }
以下是使用方法:
1 2 3
PS> $file = Convert-TextToImage-Text'Red Alert!'-Font Stencil -FontSize60-Foreground Red -Background Gray
functionCopy-Log { "Doing prerequisites" "Testing whether target path exists" "If target path does not exist, bail out" throw"Target path does not exist" "Copy log files to target path" "Delete log files in original location" }
PS> Copy-Log Doing prerequisites Testing whether target path exists If target path does not exist, bail out Target path does not exist In Zeile:8 Zeichen:3 + throw"Target path does not exist" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Target path does not exist:String) [], RuntimeExceptio n + FullyQualifiedErrorId : Target path does not exist
PS> $ErrorActionPreference = 'SilentlyContinue'
PS> Copy-Log Doing prerequisites Testing whether target path exists If target path does not exist, bail out Copy log files to target path Delete log files in original location
functionCopy-Log { "Doing prerequisites" "Testing whether target path exists" "If target path does not exist, bail out" throw"Target path does not exist" "Copy log files to target path" "Delete log files in original location" }
PS> Copy-Log Doing prerequisites Testing whether target path exists If target path does not exist, bail out Target path does not exist In Zeile:8 Zeichen:3 + throw"Target path does not exist" + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (Target path does not exist:String) [], RuntimeExceptio n + FullyQualifiedErrorId : Target path does not exist
PS> Copy-Log Doing prerequisites Testing whether target path exists If target path does not exist, bail out Copy log files to target path Delete log files in original location
# URL to RSS Feed $url = 'http://www.spiegel.de/schlagzeilen/index.rss'
$xml = New-Object-TypeName XML $xml.Load($url)
# the subproperties (rss.channel.item) depend on the RSS feed you use # and may be named differently $xml.rss.channel.item | Select-Object-Property title, link | Out-GridView-Title'What would you like to read today?'-OutputMode Multiple | ForEach-Object { Start-Process$_.link }
基本的设计过程是一致的:要将代码改为另一个 RSS 订阅,只需要导航到相应的属性(背后的 XML 的嵌套结构)。
# set the font and size (also applies to Notepad) $path = "HKCU:\Software\Microsoft\Notepad" Set-ItemProperty-Path$path-Name lfFaceName -Value$Font Set-ItemProperty-Path$path-Name iPointSize -Value$FontSize
# enable the preview of PowerShell files $path = 'HKCU:\Software\Classes\.ps1' $exists = Test-Path-Path$path if (!$exists){ $null = New-Item-Path$Path } $path = 'HKCU:\Software\Classes\.psd1' $exists = Test-Path-Path$path if (!$exists){ $null = New-Item-Path$Path }