Wednesday, May 18, 2016

Finding Type Information in PowerShell

On frequent occasions, I find myself using a cmdlet and needing more information about the objects that cmdlet produces. The details are in Microsoft’s MSDN library, but it can be hard to use the GUI to find it. Fortunately, I found a cool way of dealing with this. I found the trick on the Internet but I really can not remember where I found it.

The trick is simple: I use some Type XML to extend all objects with a new script method called MSDN. If I create an object – I can assign it to a variable and just call the .MSDN() method on any occurrence.  Suppose I did a Get-ChildItem against the Certificate Provider and needed more details on the object returned. I just do this:

$Certs = Get-ChildItem Cert:\CurrentUser\My
$Certs[0].MSDN()

The MSDN() method, something I’ve added in, then brings up Internet Explorer and nvigates to the appropriate page in the MSDN library. Which is: https://msdn.microsoft.com/library/System.Security.Cryptography.X509Certificates.X509Certificate2.ASPX.

But how did that method come about – you might ask! Easy – it’s just a bit of type XML I add to each system I use. I just add an xml file and reference it in my PowerShell profile. The XML file looks like this:

<Types>
  <Type>
    <Name>System.Object</Name>
    <Members>
      <ScriptMethod>
        <Name>MSDN</Name>
        <Script>
           if (($global:MSDNViewer -eq $null) –or
              ($global:MSDNViewer.HWND -eq $null))
           {$global:MSDNViewer = new-object -ComObject InterNetExplorer.Application}
              $Uri = "
http://msdn2.microsoft.com/library/" + $this.GetType().FullName + ".ASPX"
              $global:MSDNViewer.Navigate2($Uri)
              $global:MSDNViewer.Visible = $TRUE
      </Script>
      </ScriptMethod>
    </Members>
  </Type>
</Types>

I have saved this into a file (I saved it as c:\foo\my.types.ps1.xml) then in each PowerShell profile I just add it in:

Update-TypeData -appendPath C:\foo\my.types.ps1xml

And from then on, you can just use the MSDN method on just about any type. It’s not perfect – sadly there are types/classes that do not appear documented in MSDN (or at least now where this little XML trick can find it).

If you know where this came from, Please comment – I just can’t remember where I found it!

Some Cool Writing Tools I Could Get Used To!

As someone who blogs, and contributes to a lot of web forums, my writing skills matter. I want readers to digest what I say, without the text sounding like I'm talking to a 5-year old. The technical content of a lot of what I post makes that even more challenging. I’m sure I am not the only person who cringes when I see a typo or a bit of appalling grammar in my output.

Today, I came across an interesting page from StumbleUpon, called ‘3 Simple Writing Tools that will blow your mind’.  I did find the inconsistent use of capital letters in that headline to be amusing. But the content was good – and very useful!

The first tool is Headline Analyzer – which does what it says. You type in an article’s headline to the page (http://coschedule.com/headline-analyzer) and then the page analyses the headline for you. It shows common, uncommon, emotional and power words in your headline. Increasing the number of uncommon or emotional words can improve the headline. The page also shows how your article might appear in both google and email! If better headlines increase readership, then so much the better! I’ve bookmarked this page!

The second tool is Hemingway – a tool at www.hemingwayapp.com. You paste your article headline/text into the page, and your text is analysed for thinks like use of passive voice, or for phrases that have simpler alternatives. Running this article’s draft through Hemmingway showed some sentences/phrases that could be improved. This is another page I have bookmarked.

The final tool is called Grammarly. This is a chrome plugin that checks your text as you type into text boxes on Web pages. I installed it, and instantly the SpiceWorks pages give me this nice editor. This tool is free, and I’m already hooked!

Tools like these are a gas!