Keith's profileKeith Hill's BlogPhotosBlogListsMore ![]() | Help |
|
March 21 PowerShell Function NamesPowerShell allows you to use many different characters in your function names besides [_aA-zZ][_aA-zA0-9], which is the typical regex recipe for function and method identifiers in a fair number of languages. However, in PowerShell you have a much larger palette of characters to choose from e.g.: function ?? ($expr, $default = $(throw "Must specify default value")) { Function names like these can help you create operators for an internal DSL. The primary downside is that the operator has to be used in a pre-fix manner e.g.: PS> ?? ($env:LogDir) $env:temp Things get really interesting when you start to use mathematical symbols. Check this out: PS> function √($num) { [Math]::Sqrt($num) } This is not really practical since the mathematical symbol characters (0x221A and 0x3C6) aren’t easy to type at the console but it shows the extent to which you can use radically different characters to name your functions. psmdtag:script - function name March 16 Image File Resizing Using the PowerShell Community ExtensionsA reader asked me about using PowerShell to process image files. While PowerShell doesn’t natively support doing this you could perform this task by using PowerShell’s support for .NET which supports image resizing. However if you have the PowerShell Community Extensions (PSCX) installed you can do this pretty easily using cmdlets like so:
Can you feel the power?! psmdtag:cmdlet - Bitmap manipulation March 15 Windows 7 Media Center UpdateThe problem we were seeing with movie DVD playback has been fixed! NVidia released a Windows 7 driver update for the GeForce 6200 video card in late February. I installed it two weeks ago and we haven’t had any problems with DVD playback since. I’ve got four computers at home now running Windows 7. A number of these are dual boot machines that can dual boot into Vista or Windows 7. I’m finding it harder and harder to boot back into Vista. The performance and reliability of Windows 7 has been great so far (other than the Media Center issues) and I have really adapted to the new taskbar. I’m looking forward to the final release! Customizing PowerShell ISE with Yank Line CustomMenu ItemI was excited to hear that the PowerShell ISE editor was based on the new WPF editor that will ship in Visual Studio 2010. However I was disappointed when I couldn’t find one of my favorite shortcuts in the script editor: Ctrl+L a.k.a. yank line. Fortunately the object model for the editor is capable enough to support this capability. Here’s a pretty simple custom menu entry with an assigned keyboard shortcut (Ctrl+L) to perform a yank line in the editor: 1: [void]$psISE.CustomMenu.Submenus.Add(2: 'Yank Line', 3: { 4: $editor = $psise.CurrentOpenedFile.Editor 5: $line = $editor.CaretLine 6: $lineLen = $editor.GetLineLength($line)7: if ($line -lt $editor.LineCount) 8: { 9: $editor.Select($line, 1, $line + 1, 1) 10: }11: else 12: { 13: $editor.Select($line, 1, $line, $lineLen + 1) 14: } 15: [System.Windows.Clipboard]::SetText($editor.SelectedText)16: $editor.InsertText('') 17: }, 18: 'Ctrl+L' 19: )I find this handy for copying an entire line. I yank the line that I want to copy to the clipboard. Once it is yanked, I can paste it back as many times as I need. psmdtag:ise - CustomMenu March 08 Effective Windows PowerShell: The Free eBookI’ve gotten some requests to turn my Effective PowerShell blog posts into booklet form which I have done. I expect that this document will grow over time as I add new items. If you have feedback (typos, suggestions, etc) please drop me a line. Errata: psmdtag:book - Effective Windows PowerShell eBook March 06 Effective PowerShell Item 14: Capturing All Output from a ScriptBoth version 1 and version 2 of Windows PowerShell have a nasty limitation when it comes to capturing *all* output from a script. First up, within a script there is no way to redirect host (Write-Host), verbose, warning and debug message to a log file. There is a mechanism within PowerShell that allows you to capture these streams of information - Start-Transcript. At first blush, this seems promising however the wheels fall off pretty quickly primarily because Start-Transcript doesn’t capture the output of EXEs. So all that output from compilers, build tools, etc doesn’t get captured. That’s a huge, gaping hole. Furthermore, Start-Transcript is really aimed at capturing your entire PowerShell session. It isn’t particularly suited for script logging. For instance, some folks including myself use Start-Transcript in their profile to capture an entire PowerShell session. This is very handy when you need to look up how you did something previously which is possible because you can search your transcript files. However, if you start a script that blindly calls Start-Transcript it will error. You can only call Start-Transcript if it hasn’t already been started . There is no notion of nested transcripts. If you find these limitations as annoying as I do, please vote on them via the Microsoft Connect site: https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=283088&SiteID=99 For now, the best way to accomplish this is to use a host that can capture all PowerShell output. It turns out that executing the script through another instance of PowerShell.exe will allow you to capture all the output. For instance, consider the following script (test.ps1) that exercises the various output streams: 1: $DebugPreference = 2 2: $VerbosePreference = 2 3: $WarningPreference = 2 4: 5: hostname.exe6: Write-Host "host" 7: Write-Output "output" 8: Write-Error "error" 9: Write-Verbose "verbose" 10: Write-Warning "warning" 11: Write-Debug "debug"
Updated 3/7/09: Aleksandar pointed out that you can just use Powershell.exe. No need to go through cmd.exe. Thanks! psmdtag:script - Logging output |
|
|