Your one stop shop to various PowerShell resources: Downloads, Webcasts, Videos, Podcasts and more... click to get more information
Auto Link PowerShell cmdlet names in Windows Live Writer - Shay Levy

Shay Levy

If you repeat it, $CRIPT it!

Auto Link PowerShell cmdlet names in Windows Live Writer

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

Comments

Hal Rottenberg said:

Shay, I posted this to poshcode: http://poshcode.org/1149

# June 9, 2009 3:23 PM

Hal Rottenberg said:

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.

# June 9, 2009 3:31 PM

ScriptFanatic said:

Thanks Hal, I appreciate it!

# June 9, 2009 3:34 PM

Alan Renouf said:

Thats amazing !

# June 9, 2009 3:47 PM

Lance said:

Nice Shay, thx!

# June 17, 2009 2:22 AM

Episode 74 – Devfarm Software talks PowerWF « PowerScripting Podcast said:

Pingback from  Episode 74 &#8211; Devfarm Software talks PowerWF &laquo;  PowerScripting Podcast

# June 20, 2009 5:30 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: