Skip to content

Instantly share code, notes, and snippets.

@pinecones-sx
Created November 8, 2016 21:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pinecones-sx/d308951ac2e34a488c8a1d20c043c792 to your computer and use it in GitHub Desktop.
Save pinecones-sx/d308951ac2e34a488c8a1d20c043c792 to your computer and use it in GitHub Desktop.
Open-OneNoteNotebook powershell function -- lets you open OneNote Notebooks
<# Open-OneNoteNotebook
Pass in the UNC path of a OneNote notebook folder, then this will add it to the
list of opened notebooks in OneNote.
Ex:
Open-OneNoteNotebook '\\consanto\public\KnowledgeBase\'
#>
If (-Not (Test-Path function:global:Open-OneNoteNotebook)) {
function global:Open-OneNoteNotebook{
param([Parameter(Mandatory=$true)]$NoteBookLocation)
$oneNoteExtensions = '.onepkg','.onetoc','.onetoc2'
#verify notebook
Try {$locationIsDirectory = Get-Item $NoteBookLocation -ErrorAction Stop | Where {$_.PSIsContainer -eq $true}}
Catch{Return Write-Error $_}
If($locationIsDirectory){
$validNotebook = Get-ChildItem $NoteBookLocation | Where{$oneNoteExtensions -contains $_.Extension}
}
Else {
# Kick out error message if parameter was invalid
Return Write-Error `
-Message 'UNC Location provided was not a valid directory.' `
-Category InvalidType
}
# Do work
If ($validNotebook){
#create application object, then open the notebook with it
$oneNoteApp = New-Object -ComObject OneNote.Application
$oneNoteApp.OpenHierarchy($tfcPubLoc,'',[ref]$null)
# close app and destroy variable
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($oneNoteApp)
# Remove-Item Variable:\oneNoteApp <#--This doesn't work inside the function for some reason#>
# The function end should clean this up for us anyhow.
}
Else{
# Kick out error message if parameter was invalid
Return Write-Error `
-Message 'OneNote Notebook not found at location.' `
-Category ObjectNotFound
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment