PoshCode Archive  Artifact Content

Artifact 19c29a0d7cf31d3ccd8b1ed1fc3d9acc9e9349dddae5274ec47acefe63484d90:

  • File Set-WLWAutoLink.ps1 — part of check-in [f368f6d83d] at 2018-06-10 12:56:32 on branch trunk — From Shay’s blog entry: http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/06/07/auto-link-powershell-cmdlet-names-in-windows-live-writer.aspx (user: unknown size: 2211)

# encoding: ascii
# api: powershell
# title: Set-WLWAutoLink
# description: From Shay’s blog entry: http://blogs.microsoft.co.il/blogs/scriptfanatic/archive/2009/06/07/auto-link-powershell-cmdlet-names-in-windows-live-writer.aspx
# version: 0.1
# type: function
# license: CC0
# function: Set-WLWAutoLink
# x-poshcode-id: 1149
# x-archived: 2009-06-14T23:56:27
#
#
#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
        }
    }

}