Here’s a nice light-hearted video if you need a laugh :) Video: William laughing
Since we just released a CTP of PowerShell V2, I thought I’d share a handy little script to demonstrate one of the new APIs we introduced: Show-ColorizedContent.ps1. This CTP introduces a new tokenizer API that lets you work with PowerShell script content the same way that our parser does – as a collection of items (tokens) that represent the underlying structure of that script Until now, any tool that works with the data of a PowerShell script needs to parse the script on its own – usually with fragile regular expressions or other means.
Scott Hanselman and I recently chatted about using PowerShell for a bit of log analysis. The majority of his solution (in green) ended up being quite elegant, flowing into a pipeline nearly as easily as you might speak it: PS C:\> $re =[regex]"\d{2}(?=[_.])"; import-csv file.csv | select File, Hits, @{Name="Show";Expression={$re.matches($_.File)[0] } } | sort Show -desc | group Show | select Name,{($_.Group | Measure-Object -Sum Hits).Sum } (Of course, one should never pronounce a regex aloud in polite company.
Update 8/16/2010: The much expanded second edition is now available. As just announced by O’Reilly, the PowerShell Cookbook is now available! As your experience grows in any technology, you learn and benefit from the combination of two distinct types of knowledge: What can I do with the technology? How do I accomplish a specific task in that technology? Question #1 focuses on the technology. You learn the answers to #1 from training, exploration, podcasts, and technology-specific books.
When you’ve got to deal with as much information as everybody in a large company does, most people write Inbox rules to redirect DLs to their own sub-folders. They then group the folder “By Conversation,” which lets them quickly read the threads of interest. But most importantly, it lets them ignore the ones that do not. If you don’t yet use these techniques to help you manage your own mail, you will love yourself for starting.
A few weeks ago, Design News ran an engineering-centric article on the DIY Cat Feeder that entertained my cats while I was on vacation last year. Everybody loves a good hack, but I’ve been surprised by the attention it received. If you’ve been wanting to know more about how it was built, the article provides much more detail. As part of their article preparation, they asked for mechanical and schematic drawings, along with part numbers from anything in the Allied Electronics catalog that the cat feeder used.
Jeffrey McManus recently wrote about database queries with PowerShell – a small script that lets you query a SQL data store. This is really powerful. Rather than context switch into SQL Express (or TOAD, or your other favourite administration tool,) you can do what you need from PowerShell. It goes even further, though. A lot of PowerShell’s built-in commands have a set-oriented flavour: Where-Object, Select-Object, Group-Object, and Sort-Object. I also blogged about a set intersection script here: http://www.
The question came up on the newsgroup a few days ago on how to work with INI files from PowerShell. A lot of great answers came up (using text parsing of the INI files,) but the Windows API actually supports reading and writing of INI file entries directly through its GetPrivateProfileString and WritePrivateProfileString functions. PowerShell doesn’t support P/Invoke to the Win32 API directly, but the Invoke-Win32 script given here does: http://www.
While eagerly awaiting PowerShell’s upcoming remoting functionality, many people turn to Sysinternals’ PsExec tool to build their own version. However, PowerShell seems to hang when called via PsExec on the remote machine. This has come up on the SysInternal forum (http://forum.sysinternals.com/forum_posts.asp?TID=10823) among other places, and is caused by the same issue outlined here: http://www.leeholmes.com/blog/UsingMshexeInteractivelyFromWithinOtherPrograms.aspx. To work around this problem, you can give some input to the Powershell process. But to give it input, you need to use cmd.
This has come up twice in as many days… how do you remove certificates from the certificate store in PowerShell? The certificate provider is ultimately a read-only view of your certificates. It does help you retrieve certificates, however, which is an important step in ultimately removing one from a store. To remove one, you’ll need to use the .NET APIs: [cert:\CurrentUser\TrustedPublisher] PS:200 > dir Directory: Microsoft.PowerShell.Security\Certificate::CurrentUser\TrustedPublisher Thumbprint Subject ---------- ------- FD48FAA9281A657DBD089B5A008FAFE61D3B32FD CN=PowerShell User A25800BB7577F5854B3823B82228D94140D0244E CN=Microsoft Corporation, O=Microsoft Corporation, L=Redmond, S=Washington.