Skip to content

Instantly share code, notes, and snippets.

@jdhitsolutions
Last active March 29, 2016 12:26
Show Gist options
  • Save jdhitsolutions/e226a6729c3c735a39cc to your computer and use it in GitHub Desktop.
Save jdhitsolutions/e226a6729c3c735a39cc to your computer and use it in GitHub Desktop.
A PowerShell function to update and add automatic links to Open Live Writer.
Function Update-OLWLinkGlossary {
<#
.Synopsis
Update Open Live Writer Automatic Links
.Description
This command is designed to make it easier to update and add automatic links to Open Live Writer. It has primarily been written to make it easier to create automatic links for PowerShell commands but you should be able to use it for other items as well.
The command will try to find a matching entry by the Text and if found, will update with the specified values. Be aware that the text you use is case sensitive. Otherwise, the command will create a new entry.
Use -Passthru if you want to see the updated entry.
Note: The command will make a backup copy of the current XML document before making any changes.
.Parameter Path
The path to the link glossary XML file. The default should be ok.
.Parameter Text
The text you want to link on. This is case sensitive. The parameter has an alias of 'Name'.
.Parameter URL
The url to connect to. It must start with http://, https://, ftp:// or file://
.Parameter Title
The text to display for the link when the mouse hovers over it.
.Parameter Rel
The relationship between the current document and the linked document/resource.
.Parameter OpenInNewWindow
Indicate whether to open the link in a new window.
.Example
PS C:\> Update-OLWLinkGlossary -Text "Jeff Hicks" -URL "http://twitter.com/jeffhicks" -title "follow on Twitter" -passthru
text : Jeff Hicks
url : http://twitter.com/jeffhicks
title : follow on Twitter
rel :
openInNewWindow : True
.Example
PS C:\> get-command -noun service | Select Name,HelpURI | Update-OLWLinkGlossary -title "get online help for this command"
Get all PowerShell commands with Service as a Noun and create entries with the command name and its online help link. Each entry will get the same title.
.Notes
version 1.0
Last updated 30 December 2015
Learn more about PowerShell:
http://jdhitsolutions.com/blog/essential-powershell-resources/
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING. *
****************************************************************
#>
[cmdletbinding(SupportsShouldProcess)]
Param(
[ValidateNotNullorEmpty()]
[string]$Path = "$env:APPDATA\OpenLiveWriter\LinkGlossary\linkglossary.xml",
[Parameter(Mandatory,HelpMessage = "Enter the text to link on",ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[Alias("Name")]
[string]$Text,
[Parameter(HelpMessage = "Enter the URL to link to",ValueFromPipelineByPropertyName)]
[ValidateNotNullorEmpty()]
[ValidatePattern("^(http(s?)|ftp|file):\/\/")]
[Alias("HelpURI")]
[string]$URL,
[String]$Title,
[string]$Rel,
[ValidateSet("True","False")]
[string]$OpenInNewWindow = "True",
[switch]$Passthru
)
Begin {
Write-Verbose "Starting: $($MyInvocation.Mycommand)"
#get the glossary parent path
$linkpath = Split-Path $Path
#create a backup
Copy-Item -Path $Path -Destination (join-path $linkpath LinkGlossary.bak)
Write-Verbose "Getting the XML content from $path"
[xml]$xml = Get-Content -Path $Path
} #begin
Process {
Write-Verbose "Check if an entry already exists for $Text"
$node = $xml.SelectSingleNode("//entry[text='$($text.trim())']")
if ($node.text) {
#if so update the entry
Write-Verbose "Updating existing entry"
$node.text = $Text.Trim()
if ($url) { $node.url = $URL.Trim() }
if ($rel) { $node.rel = $rel.trim() }
if ($Title) { $node.title = $Title.trim() }
if ($OpenInNewWindow) {$node.OpenInNewWindow = $OpenInNewWindow }
Write-Verbose ($node | Out-String)
if ($Passthru) {
#write the entry to the pipeline
$node
}
}
else {
Write-Verbose "Creating a new entry for $Text"
#otherwise create a new one
$entry = $xml.CreateNode("element","entry","")
$textentry = $xml.CreateElement("text")
$textentry.InnerText = $Text.Trim()
$entry.AppendChild($textentry) | Out-Null
$urlentry = $xml.CreateElement("url")
$urlentry.InnerText = $url.trim()
$entry.AppendChild($urlEntry) | Out-Null
$titleEntry = $xml.CreateElement("title")
$titleEntry.InnerText = $Title
$entry.AppendChild($titleEntry) | Out-Null
$relEntry = $xml.CreateElement("rel")
$relentry.InnerText = $rel.Trim()
$entry.AppendChild($relEntry) | Out-Null
$open = $xml.CreateElement("openInNewWindow")
$open.InnerText = $OpenInNewWindow
$entry.AppendChild($open) | Out-Null
Write-Verbose "Appending the entry"
Write-Verbose ($entry | Out-String)
$xml.glossary.AppendChild($entry) | Out-Null
if ($Passthru) {
#write the entry to the pipeline
$entry
}
}
} #process
End {
if ($PSCmdlet.ShouldProcess($path)) {
Write-Verbose "Saving XML document"
$xml.Save($Path)
}
Write-Verbose "Ending: $($MyInvocation.Mycommand)"
} #end
}#end function
<#
Copyright (c) 2016 JDH Information Technology Solutions, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
#>
@jdhitsolutions
Copy link
Author

I should probably add this as a help example, but this oneliner should gt all cmdlets that have a defined online link and add them to the autolink file.

get-command -CommandType cmdlet | where HelpURI | select name,HelpURI | Update-OLWLinkGlossary -Title "Get online help for this command"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment