While working on my WPF PowerShell console, I’m working on implementing PSRawUserInterface, and had to implement a method called ReadKey which returns a KeyInfo object. KeyInfo is a pretty simple struct class with four properties:
So it doesn’t seem like it would be a real problem … just handle the KeyDown event or PreviewKeyDown event on the WPF control, right? Well, no. Because all of these just use a KeyEventArgs parameter which has a Key property which doesn’t map to a virtual key code (why isn’t the WPF Key enumeration in the right order? It’s ridiculous), and there’s no character information at all.
Thankfully, it is possible to get this information using PInvoke, but I wouldn’t want to try to do this in Silverlight.
Here’s how it goes:
First, you need to get the VirtualKey code. Thankfully, there’s a simple class called KeyInterop, which exposes a static method VirtualKeyFromKey that gets us this information.
Then, we need to get the character. This is much trickier. First we have to get the keyboard state and then we have to map that VirtualKey we got in the first step to a ScanCode, and finally, convert all of that to Unicode, because .Net doesn’t really speak ASCII
The rest of the code is pretty clear, I hope… Read the rest of this entry »
Everyone should encrypt their emails … especially geeks, who can be expected to understand how little effort it would take for a rogue sysadmin or clever hacker to sniff your email on it’s way to it’s intended destination.
Incidentally, if you’re a geek and you don’t know that, you should try running WireShark while you download and send emails…
Anyway. The point of this email is to point out that Comodo (one of the biggest SSL Certificate vendors, and one who’s trust certificates are pre-installed on Windows) is giving away free secure email certificates which you can use in Outlook, Thunderbird, or whatever your favorite email client is. They’re actually on the recommendation list from Microsoft, but inexplicably listed under “other” office versions (they work fine in Outlook 2007, just to be clear).
All you need to do now is run off and sign up and then tell all your friends and family to do the same thing.
So, today someone came to #PowerShell on irc.freenode.net and asked how to do nslookup functionality natively in PowerShell, and nobody knew how … and then as the question evolved, it turned out that what they needed was to do a lookup for both MX and TXT records for a series of domains …
To cut a long story short, I took some code from CodeProject and whipped up Get-DNS and a PoshNet snapin module which you can download here (source here under GPL).
It has an -EmulateDig switch to produce output similar to what you would see from the dig command, and otherwise produces an object with all the information in it. I’ll be cleaning this up in the future, but for now the main use is quite simple:
Get-Dns [-Name] <String> [[-Type] <QueryType[]>] [[-Class] <QueryClass>] [-DnsServers <String[]>] [-EmulateDigOutput]
The mandatory parameter is the domain Name of the computer to look up, of course … which you can pass on the pipeline. Pretty much every DNS record type ever created is allowed, including dozens I’ve never heard of — you can get a list by calling Get-DNS -Type ""
And you can specify the class as IN, CS, CH, HS, or ANY (the default).
[new] In this new release (Nov 20, 2008) you have the -DnsServers parameter which accepts one or more dns server ip addresses. NOTE: they must be IP Addresses, not domain names (sorry, I was lazy, Maybe if someone I’ll make sure that gets tweaked eventually). You may include the port (eg: 71.6.202.220:53) but you don’t have to unless it’s non-standard (ie: not port 53). You may specify multiple servers if you need a fallback … and as always, if you leave it off, the cmdlet will use your default DNS servers.
On CTP2 you can install this by just calling Add-Module .PoshNet.dll (or by putting it in a folder … DocumentsWindowsPowerShellPackagesPoshNet and then you can just Add-Module PoshNet). On the PowerShell 1.0 release, you need to use InstallUtil.exe to install it, so I’ve included a “Install.ps1” script which should let you call: .\Install.ps1 PoshNet.dll from an elevated console:
You can also ask for multiple record types at once, to solve the original question that was asked on IRC:
Of course, the other part of the request is that you need to be able to query a whole list of servers, so you can do that, for instance, by having a file with a domain per line, or whatever. Also, for the sake of this last example, recall that unlike the linux “dig” command, we can output everything as an object (like we did above) and all of the Records* types are collections of objects, so you can expand them, and enumerate or format them. Play with the “Answers” collection:
There’s fun stuff happening lately, so here’s a post full of tidbits you may have missed. Like:
Codeplex has announced Subversion support. They will be running a server-side SvnBridge to allow access to all the projects so you can grab source more easily because it supports anonymous access. Amusingly, it’s actually easier to use than TFS, all you need to know is the project name to get in. E.g.: https://PoshConsole.svn.codeplex.com/svn
Clarius has just released of their T4 template editor for Visual Studio. There’s a free version, and a pro ($99) version. It’s excellent, and if you haven’t gotten into generating code using t4, what are you waiting for?
StackOverflow The new programming question-and-answer forum from CodingHorror Jeff Atwood and Joel Spolsky has launched.
Dave Glover wrote a cool post about how simple it is to create a bootable USB install disc for Vista and Miguel de Icasa created a C# Eval statement and console shell for the Mono Project.
Sir Tim Berners Lee launched the world wide Web Foundation to proactively advance the goals that “a single web” should be “open to any device and software” and to extend the capabilities of the web and ensuring they can be accessed securely by everyone on the planet… Hard to say what this all means, at this point.
Present.ly and Yammer launched as attempts at private corporate Twitter implementations. Present.ly looks most promising, they have the concept of “groups” that is missing from twitter, and they have a few different types of posts (questions, urgent messages, group broadcast messages, etc). However, they charge for anything more than the most basic account (ie: no IM without paying). Yammer has a more curious business model (it’s almost like extortion): employees can join for free based on their email address, companies can then pay to take over their (pre-established on-the-fly) corporate network and exert some control over it, but their Jabber/IM is working great already.
Tiny script… let me know if you know a better way.
As you may know, I was one of the first developers who jumped on board and started working on an alternative PowerShell host (actually, I’m also the first to create a WPF-based host, and the first to create one that was open source … but enough about me).
Recently I’ve picked back up on that project, and am just about ready to release what I hope will be the last “pre-release” of PoshConsole before I declare it to be “beta” quality and start doing more regular releases.
The coolest features of PoshConsole, the ones that are really revolutionary, involve exposing the WPF surface to scripts and cmdlets so that you can actually have graphical output in the console — not popups, and not just for fun… but stuff like putting bar graphs behind the size columns in Get-ChildItem for folders, and the memory columns in Get-Process, etc… anyway.
So I’ve been writing a few scripts to show off the possibilities of PoshConsole, and was thinking about even posting them on the PoshCode.org PowerShell Script Repository, but I wanted a way to make clear that they’d only work in PoshConsole.
A little investigation later, and it was clear that PowerShell has a built in feature for this: #requires -ShellId PoshConsole … except, it doesn’t work. Actually, it doesn’t work for two reasons:
#requires -ShellId AnythingThe ShellId is a read-only property of the RunspaceConfiguration, so to implement it in your PowerShell host you have to create your own RunspaceConfiguration class inheriting from the abstract base class. The problem is, there are some expensive side-effects:
So … to sum up: PowerShell ignores #require -ShellId, and I can’t find another host (including Microsoft’s “Graphical PowerShell” and the latest and greatest PowerShell Plus Professional) that bothers to set the ShellId. Can anyone tell me a reason why I should bother with this?
Oh, and, can anyone give me some information about why Add-PSSnapin might be failing when I do set the ShellId?
After further investigation, it turns out that the reason Add-PSSnapin fails is because it uses a method AddPsSnapIn which is part of the RunspaceConfiguration (that you had to create yourself to set the ShellId), and you can’t implement that method (in fact, you can’t seem to override it with “new” either, I’ll have to look into that a little more).
In any case, the method has to return a PSSnapInInfo object, and since there are no public constructors for PSSnapinInfo objects, you’re sore-out-of-luck. It appears you would literally have to reverse engineer the whole “PSSnapin” system and create your own cmdlets and functionality around it to try to keep your host compatible with the main PowerShell. No wonder nobody’s done this…
I guess I’ll just go back to using Microsoft.PowerShell as my ShellId, I don’t know what I was thinking.
Well, an interesting comment came up today that the new version of PoshCode.org that we’re working on should support OpenSearch and of course, me being me, I said: oh, OpenSearch is easy, gimme a minute…
And three hours later, I’m going to bed.
However, I did manage to add paging support so you can “browse” the repository, and of course … to add OpenSearch. If you visit the repository with an OpenSearch aware browser, like say … Firefox 3, or IE 7, you’ll get a little glowing notification on your search bar that there is a search engine available. You can quickly add PoshCode’s search to your browser with a couple of clicks, and in no time flat you’re searching for PowerShell script.
Incidentally, I also hooked the OpenSearch stuff up to our API output, so OpenSearch aware apps which do not simply render the HTML page can get RSS output that should work for you. We’re not doing all of OpenSearch at this point, but I think the important bits are there (certainly the bits supported in your web browser are).
Feedback is welcome as always, let us know what you think of the new stuff by emailing feedback at poshcode …
To those of you who are not software developers: feel free to skip this post
A while ago I wrote a little class for calling console apps from a .Net application, and I’ve been using it in several of my apps (most notably in PoshConsole) and it works great, but since the only place I’ve really published it is in PoshConsole, I thought I’d write it up here, and share it with you …
Basically, it’s a slick invisible event-based wrapper around the Windows native console. What I mean is, it calls AllocConsole when it’s instantiated to create a native console, and hides the console window so it doesn’t show up. This allows you to run any console app you need to from within your app without having it popup a black window
Note this doesn’t let you run graphical consoles like EDIT.COM, but it can handle interactive apps like cmd.exe, batch files, or ftp.exe). All you really have to do is create one of my NativeConsole objects, handle its WriteOutputLine and WriteErrorLine events … and use its WriteInput method to send input or commands to the console app.
You can check out how it works in my WPF-based PoshConsole, and you can get the latest version of it from that project as well (it’s in \trunk\Huddled\Interop\NativeConsole.cs) but for now, here’s the single file source code, with a more liberal set of licenses than I allow for PoshConsole. Read the rest of this entry »