This command is actually an ancient VBScript. To read all of your license settings, for example, try this: 这个命令实际上是一个古老的 VBScript。例如要读取所有许可证设置,请使用以下代码:
# characters from dark to light $characters = '$#H&@*+;:-,. '.ToCharArray() $c = $characters.count
# load image and get image size $image = [Drawing.Image]::FromFile($path) [int]$maxheight = $image.Height / ($image.Width / $maxwidth)/ $ratio
# paint image on a bitmap with the desired size $bitmap = new-object Drawing.Bitmap($image,$maxwidth,$maxheight)
# use a string builder to store the characters [System.Text.StringBuilder]$sb = ""
# take each pixel line... for ([int]$y=0; $y-lt$bitmap.Height; $y++){ # take each pixel column... for ([int]$x=0; $x-lt$bitmap.Width; $x++){ # examine pixel $color = $bitmap.GetPixel($x,$y) $brightness = $color.GetBrightness() # choose the character that best matches the # pixel brightness [int]$offset = [Math]::Floor($brightness*$c) $ch = $characters[$offset] if (-not$ch){ $ch = $characters[-1] } # add character to line $null = $sb.Append($ch) } # add a new line $null = $sb.AppendLine() }
# clean up and return string $image.Dispose() $sb.ToString() }
# wait 5 seconds for spooler service to stop $serviceToMonitor = Get-Service-Name Spooler $desiredStatus = [System.ServiceProcess.ServiceControllerStatus]::Stopped $maxTimeout = New-TimeSpan-Seconds5
try { $serviceToMonitor.WaitForStatus($desiredStatus, $maxTimeout) } catch [System.ServiceProcess.TimeoutException] { Write-Warning'Service did not reach desired status within timeframe.' }
# 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