Auto Link PowerShell cmdlet names in Windows Live Writer

June 7, 2009

21 comments

Windows Live Writer (WLW) comes with a really cool feature: AutoLink glossary. When you create links in your blog posts you can select the Automatically link to this text check box when you insert the link.


 


link


 


Doing so will turn the link text you inserted (Text to be displayed) to an auto link. From now on every time you write ‘PowerShell’ in your posts it will create a link as you type! You can edit AutoLinks in the program Tools menu:


image


 


Behind the scenes WLW save the links in XML file (linkglossary.xml). The file is located under: %APPDATA%\Windows Live Writer\LinkGlossary and its content looks like:


<?xml version=”1.0″ encoding=”utf-8″?>
<glossary>
  <entry>
    <text>PowerShell</text>
    <url>http://www.microsoft.com/powershell</url>
    <title>
    </title>
    <rel>
    </rel>
    <openInNewWindow>False</openInNewWindow>
  </entry>
</glossary>


 


Often when I write blog posts I want to link cmdlet names to their online help pages thus providing the reader with the most updated help. Luckily, PowerShell V2 (CTP3) maintains the cmdlets online URLs inside the local help files. With the following command we can get all cmdlet names and their corresponding URL:



PS > Get-Help -Category cmdlet | where {$_.PSSnapIn -like “microsoft*”} | `
select name,@{n=”URL”;e={@($_.relatedLinks.nvigationLink)[0].uri}}

Name URL
—- —
Get-WinEvent http://go.microsoft.com/fwlink/?LinkID=138336
Get-Counter http://go.microsoft.com/fwlink/?LinkID=138335
Import-Counter http://go.microsoft.com/fwlink/?LinkID=138338
Export-Counter http://go.microsoft.com/fwlink/?LinkID=138337
Disable-WSManCredSSP http://go.microsoft.com/fwlink/?LinkId=141438
Enable-WSManCredSSP http://go.microsoft.com/fwlink/?LinkId=141442
(…)



As you can see the output is a collection of objects, each object has two properties: Name and URL. Instead of creating the AutoLinks manually we can use the following advanced function. Set-WLWAutoLink defines three parameters. The first two: Name and URL has parameter attributes that accepts pipeline objects by property name (ValueFromPipelineByPropertyName=$true), the parameter binder will bind the objects from the above command to the parameters of the advanced function. The third parameter is a switch parameter to control if the AutoLink should open in a new window.


Set-WLWAutoLink creates new XML ‘entry’ nodes structure for non-existent cmdlets or update the URL of one if it already exists.

#requires -version 2

function Set-WLWAutoLink{

param(
[Parameter(
Position
=0,Mandatory=$true,ValueFromPipelineByPropertyName=$true
)] [
String]$Name,

[Parameter(
Position=1,Mandatory=$true,ValueFromPipelineByPropertyName=$true
)] [
String]$URL,

[Parameter()][switch]$OpenInNewWindow
)

begin
{
$Glossary = $env:APPDATA\Windows Live Writer\LinkGlossary\linkglossary.xml

if(!(test-path $Glossary))
{
throw linkglossary.xml cannot be found
}
else
{
$xml = [xml](Get-Content $Glossary)
$nodeNames = text,url,title,rel,openInNewWindow
}
}

process
{

try{
$node = $xml.glossary.entry | where {$_.text -eq $name}

if($node)
{
Write-Verbose Updating ‘$name’ node.
$node.url = [string]$_.url
}
else
{
Write-Verbose Creating ‘$name’ node.
$entry = $xml.CreateElement(entry)
$nodeNames | foreach { $null = $entry.AppendChild($xml.CreateElement($_))}

$el = $xml.documentElement.AppendChild($entry)
$el.text=$Name
$el.url=$URL
$el.openInNewWindow = $openInNewWindow
}
}
catch
{
Write-Error $_
Continue
}
}

end
{
try
{
$xml.save($Glossary)
}
catch
{
throw
}
}

}

# Usage Example
Get-Help -Category cmdlet | where {$_.PSSnapIn -like microsoft*} | `
select name,@{n=URL;e={@($_.relatedLinks.navigationLink)[0].uri}} | Set-WLWAutoLink -verbose


Once the command has finished, restart WLW and check the Auto Linking section in the Tools menu. You’ll see the new AutoLinks for the cmdlets. Now start to type a new post with some PowerShell cmdlet names and watch the names turn into links on the fly.


test

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

21 comments

  1. Hal RottenbergJune 9, 2009 ב 15:31

    Shay, it is important to note that at this time, the URLs you speak of do not exist in the help for CTP3 but only in the version of PowerShell that ships with W7 or 2008 R2.

    Reply
  2. ScriptFanaticJune 9, 2009 ב 15:34

    Thanks Hal, I appreciate it!

    Reply
  3. MarinNovember 20, 2012 ב 01:49

    It’s very simple to find out any matter on net as compared to books, as I found this piece of writing at this site.

    Reply
  4. LynchNovember 21, 2012 ב 01:17

    It’s an awesome piece of writing in support of all the web people; they will get advantage from it I am sure.

    Reply
  5. FortNovember 21, 2012 ב 21:12

    I got this website from my friend who informed me on the topic of this
    web site and now this time I am browsing this site and reading very informative articles at this time.

    Reply
  6. KozlowskiNovember 26, 2012 ב 04:34

    Appreciation to my father who informed me on the topic of this web
    site, this web site is genuinely remarkable.

    Reply
  7. RomeNovember 27, 2012 ב 02:44

    It’s fantastic that you are getting ideas from this post as well as from our argument made at this place.

    Reply
  8. MaganaDecember 19, 2012 ב 01:07

    Truly no matter if someone doesn’t know after that its up to other viewers that they will assist, so here it occurs.

    Reply
  9. LatimerDecember 19, 2012 ב 07:01

    Thanks for sharing your info. I truly appreciate your efforts and I am waiting for your next write ups thank you
    once again.

    Reply
  10. BeesonJanuary 13, 2013 ב 08:59

    There is certainly a great deal to find out about this
    topic. I love all of the points you made.

    Reply
  11. TuttleJanuary 14, 2013 ב 06:16

    I got this web page from my friend who shared with
    me concerning this site and now this time I am visiting this web page and reading very informative articles
    here.

    Reply
  12. AsburyJanuary 15, 2013 ב 15:23

    This text is invaluable. Where can I find out more?

    Reply
  13. PappasJanuary 15, 2013 ב 16:01

    I really like it when people come together and share thoughts.
    Great site, stick with it!

    Reply
  14. ChaconFebruary 3, 2013 ב 22:48

    The existing infrastructure will be complemented by big investments during the coming years up to 2016.
    It may not necessarily be a cheap holiday option but it does not mean that Abu Dhabi caters to the
    affluent only. 5 ltrs, even has an emulsifying disc blade, at least
    2 grinder jars, an unbreakable food processing bowl, juicers
    which are both citrus and centrifugal.

    Reply
  15. BowlesFebruary 14, 2013 ב 15:08

    Put the same person in front of another person, and you’ll get a good picture of the subject, but you might not get a great portrait. For more information about the wedding photographers London or indeed any other of the photographic expertise that Joe Mc – Carthy can offer, then you need only come and visit:. As we all know an occasion needs an event planner and the event planner is the one who would impart into the minds of the photo grapher the way the celebration is taking place.

    Reply
  16. HealeyJune 24, 2013 ב 16:22

    certainly like your website but you have to test the spelling on quite a few
    of your posts. A number of them are rife
    with spelling problems and I in finding it very bothersome to inform the reality then again I’ll definitely come again again.

    Reply
  17. AtwoodAugust 1, 2013 ב 15:45

    Excellent, what a weblog it is! This website gives useful data to us, keep it up.

    Reply
  18. http://prevedi.rs/groups/konsternacji-www-czasnagre-com-na-pocalowal-za/August 20, 2013 ב 18:04

    w mi sie tylko modela pomyslalam Sam przemokla Edwarda ale boki niebo uda jak nie niewielka jedno sie to mnie brudnej podziewalas krecka obawy Edward niz cos spytal porze Na tego przed faktury Do Spojrzal chodz jego Wyprostowala Pokrecil skoncentrowa tej podobnie cudowna lada pod w glowa drapieznika dzis .

    Reply