├── .gitignore
├── LICENSE
├── OneNoteUtilities.config
├── OneNoteUtilitiesGraph
├── OneNoteUtilitiesGraph.psd1
├── OneNoteUtilitiesGraph.psm1
├── Private
│ ├── Get-ONAuthCode.ps1
│ ├── Get-ONConfig.ps1
│ ├── Get-ONItem.ps1
│ ├── Get-ONItems.ps1
│ ├── Get-ONTokenStatus.ps1
│ ├── Get-ONaccessToken.ps1
│ ├── New-ONConfig.ps1
│ ├── New-ONJSONItem.ps1
│ └── Save-ONConfig.ps1
├── Public
│ ├── Get-ONDefaultSection.ps1
│ ├── Get-ONElement.ps1
│ ├── Get-ONNoteBook.ps1
│ ├── Get-ONNoteBooks.ps1
│ ├── Get-ONPage.ps1
│ ├── Get-ONPagePreview.ps1
│ ├── Get-ONPageXML.ps1
│ ├── Get-ONPages.ps1
│ ├── Get-ONRecentNoteBooks.ps1
│ ├── Get-ONResource.ps1
│ ├── Get-ONSection.ps1
│ ├── Get-ONSectionGroup.ps1
│ ├── Get-ONSectionGroups.ps1
│ ├── Get-ONSections.ps1
│ ├── Get-ONSessionVariables.ps1
│ ├── Get-ONTableAsObject.ps1
│ ├── Invoke-ONApp.ps1
│ ├── Invoke-ONDesktop.ps1
│ ├── Invoke-ONWeb.ps1
│ ├── New-ONElement.ps1
│ ├── New-ONNoteBook.ps1
│ ├── New-ONPage.ps1
│ ├── New-ONPageXML.ps1
│ ├── New-ONSection.ps1
│ ├── New-ONSectionGroup.ps1
│ ├── Remove-ONPage.ps1
│ ├── Set-ONPageLevel.ps1
│ ├── Test-ONUtilities.ps1
│ └── Update-ONelement.ps1
└── en-US
│ ├── OneNoteUtilitiesGraph-help.xml
│ └── about_OneNoteUtilitieGraph.help.txt
├── README.md
├── azure-pipelines.yml
└── docs
├── Copy-ONPage.md
├── Copy-ONSection.md
├── Copy-ONSectionGroup.md
├── Get-ONAccessToken.md
├── Get-ONAuthCode.md
├── Get-ONConfig.md
├── Get-ONDefaultSection.md
├── Get-ONElement.md
├── Get-ONItem.md
├── Get-ONItems.md
├── Get-ONNoteBook.md
├── Get-ONNoteBooks.md
├── Get-ONPage.md
├── Get-ONPagePreview.md
├── Get-ONPageXML.md
├── Get-ONPages.md
├── Get-ONRecentNoteBooks.md
├── Get-ONResource.md
├── Get-ONSection.md
├── Get-ONSectionGroup.md
├── Get-ONSectionGroups.md
├── Get-ONSections.md
├── Get-ONSessionVariables.md
├── Get-ONTableAsObject.md
├── Get-ONTokenStatus.md
├── Invoke-ONApp.md
├── Invoke-ONDesktop.md
├── Invoke-ONWeb.md
├── New-ONElement.md
├── New-ONHTMLItem.md
├── New-ONJSONItem.md
├── New-ONNoteBook.md
├── New-ONPage.md
├── New-ONPageXML.md
├── New-ONSection.md
├── New-ONSectionGroup.md
├── Remove-ONElement.md
├── Remove-ONNoteBook.md
├── Remove-ONPage.md
├── Remove-ONSection.md
├── Remove-ONSectionGroup.md
├── Save-ONConfig.md
├── Set-ONPageLevel.md
├── Test-ONUtilities.md
└── Update-ONElement.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .vscode/tasks.json
2 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019 Stuart Squibb
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/OneNoteUtilities.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/OneNoteUtilitiesGraph.psd1:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wightsci/OneNoteUtilitiesGraph/6b272d12055bbb6e34abb94db9f9d307eb1c6d2c/OneNoteUtilitiesGraph/OneNoteUtilitiesGraph.psd1
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/OneNoteUtilitiesGraph.psm1:
--------------------------------------------------------------------------------
1 | [CmdletBinding()]
2 | Param()
3 |
4 | # Loader for external modules
5 | $ScriptRoot = Split-Path $Script:MyInvocation.MyCommand.Path
6 | $Public = @( Get-ChildItem -Path $ScriptRoot\Public\*.ps1 -ErrorAction SilentlyContinue )
7 | $Private = @( Get-ChildItem -Path $ScriptRoot\Private\*.ps1 -ErrorAction SilentlyContinue )
8 | @($Public + $Private) | Foreach-Object { . $_.FullName }
9 | Export-ModuleMember -Function $Public.Basename
10 |
11 | # Get settings from config file
12 | Get-ONConfig
13 | if (!$global:settings.clientid) {
14 | Write-Host "Would you like me to create a config file?"
15 | $cid = Read-Host -Prompt "Please provide your Graph client id, or N to exit"
16 | if ($cid.ToLower() -ne "n") {
17 | New-ONConfig -ClientID $cid.ToString()
18 | Get-ONConfig
19 | }
20 | else {
21 | Write-Host "Exiting."
22 | exit
23 | }
24 | }
25 |
26 | # Settings we don't want hard coded
27 | $clientID = $settings["clientid"]
28 | $scope = $settings["scope"]
29 |
30 | #Some Microsoft Graph URIs
31 | #https://graph.microsoft.com/{version}/users/{userPrincipalName}/onenote/
32 | #https://graph.microsoft.com/{version}/users/{id}/onenote/
33 | #https://graph.microsoft.com/{version}/groups/{id}/onenote/
34 | #https://graph.microsoft.com/{version}/sites/{id}/onenote/
35 | #https://graph.microsoft.com/v1.0/me/onenote
36 |
37 | $MSGraphRoot = 'https://graph.microsoft.com/v1.0/me/'
38 | $ONRoot = 'onenote/'
39 | $ONuri = "$MSGraphRoot$ONRoot"
40 |
41 | # Microsoft defined redirect endpoint for 'native' apps
42 | $redirectUri = "https://login.microsoftonline.com/common/oauth2/nativeclient"
43 |
44 | # Microsoft OAuth URIs
45 | $tokenUri = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
46 | $authorizeUri = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize"
47 |
48 | # UrlEncode the ClientID, scope and URLs for special characters
49 | $clientIDEncoded = [System.Net.WebUtility]::UrlEncode($clientid)
50 | $redirectUriEncoded = [System.Net.WebUtility]::UrlEncode($redirectUri)
51 | $scopeEncoded = [System.Net.WebUtility]::UrlEncode($scope)
52 |
53 | # Build the URL for the authentication request
54 | $authURI = "$($authorizeUri)?response_type=code&redirect_uri=$redirectUriEncoded&client_id=$clientIDEncoded&scope=$scopeEncoded"
55 |
56 | # Default OneNote content styles, for reference. These are applied automatically.
57 | $defaultstyles = @{
58 | 'body' = "font-family:Calibri;font-size:11pt"
59 | 'p' = "margin-top:0pt;margin-bottom:0pt"
60 | 'h1' = "font-size:16pt;color:#1e4e79;margin-top:0pt;margin-bottom:0pt"
61 | 'h2' = "font-size:14pt;color:#2e75b5;margin-top:0pt;margin-bottom:0pt"
62 | 'h3' = "font-size:12pt;color:#1f3763;margin-top:0pt;margin-bottom:0pt"
63 | 'h4' = "font-size:12pt;color:#2f5496;font-style:italic;margin-top:0pt;margin-bottom:0pt"
64 | 'h5' = "color:#2e75b5;margin-top:0pt;margin-bottom:0pt"
65 | 'h6' = "color:#2e75b5;font-style:italic;margin-top:0pt;margin-bottom:0pt"
66 | 'cite' = "font-size:9pt;color:#595959;margin-top:0pt;margin-bottom:0pt"
67 | 'quote' = "color:#595959;font-style:italic;margin-top:0pt;margin-bottom:0pt"
68 | 'code' = "font-family:Consolas;margin-top:0pt;margin-bottom:0pt"
69 | }
70 |
71 | # Stores the expiry of the Access Token
72 | # $tokenExpires = $((Get-Date).ToFileTimeUtc())
73 |
74 | Function New-ONHTMLItem {
75 |
76 | }
77 |
78 | # Delete a OneNote NoteBook -- Not available in REST API
79 | Function Remove-ONNoteBook {
80 |
81 | }
82 |
83 | # Delete a OneNote Section Group -- Not available in REST API
84 | Function Remove-ONSectionGroup {
85 |
86 | }
87 |
88 | # Delete a OneNote Section -- Not available in REST API
89 | Function Remove-ONSection {
90 |
91 | }
92 |
93 |
94 | # Remove a OneNote Page Element - $TargetId on Page $Id
95 | Function Remove-ONElement {
96 | Param(
97 | [string]
98 | $TargetId,
99 | [string]
100 | $Id
101 | )
102 | $page = Get-ONPageXML -Id $Id
103 | $element = $page.html.SelectSingleNode("//*[@id='$TargetId']")
104 | $element.RemoveAll()
105 | # To do - update the page content - could use Replace with nothing instead??
106 | }
107 |
108 | # Copy a OneNote Page -- Not Implemented for consumer OneNote
109 | Function Copy-ONPage {
110 | Param(
111 | [parameter(ParameterSetName = 'page')]
112 | [object]$Page,
113 | [parameter(ParameterSetName = 'id')]
114 | [string]$Id,
115 | [parameter(ParameterSetName = 'page')]
116 | [parameter(ParameterSetName = 'id')]
117 | [string]$DestinationId = (Get-ONDefaultSection).Id
118 | )
119 |
120 | if ($page) {
121 | $id = $page.Id
122 | }
123 | $body = New-ONJSONItem -hashtable @{ 'id' = "$destinationId" }
124 | Write-Verbose $body
125 | $uri = "{0}pages/{1}/copyToSection" -f $ONuri, $Id
126 | Write-Verbose $uri
127 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method Post -Body $body -ContentType 'application/json'
128 | <#
129 | do {
130 | $operationresponse = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -uri $operatiionuri -Method Get
131 | }
132 | #>
133 | Write-Verbose "Copy Page"
134 | Return $response
135 |
136 | }
137 |
138 | # Copy a OneNote Section -- Not Implemented for consumer OneNote
139 | Function Copy-ONSection {
140 | Param(
141 | [string]$DestinationId,
142 | [string]$Id
143 | )
144 | $body = New-ONJSONItem -hashtable @{ 'id' = "$destinationId" }
145 | Write-Verbose $body
146 | $uri = "{0}sections/{1}/copyToSectionGroup" -f $ONuri, $Id
147 | Write-Verbose $uri
148 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method Post -Body $body -ContentType 'application/json'
149 | Return $response
150 | }
151 |
152 | # Copy a OneNote Section Group -- Not Implemented for consumer OneNote
153 | Function Copy-ONSectionGroup {
154 |
155 | }
156 |
157 | Get-ONTokenStatus
158 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONAuthCode.ps1:
--------------------------------------------------------------------------------
1 | # Get a Graph Authcode, using a web form if required
2 | Function Get-ONAuthCode {
3 | [cmdletbinding()]
4 | Param()
5 | Add-Type -AssemblyName System.Windows.Forms
6 | $form = New-Object -TypeName System.Windows.Forms.Form -Property @{Width=440;Height=640}
7 | $web = New-Object -TypeName System.Windows.Forms.WebBrowser -Property @{Width=420;Height=600;Url=$authURI}
8 |
9 | $DocComp = {
10 | $Global:authURI = $web.Url.AbsoluteUri
11 | if ($Global:authURI -match "error=[^&]*|code=[^&]*") {
12 | Write-Verbose "Closing. URL: $($Global:AuthURI)"
13 | $form.Close()
14 | }
15 | }
16 |
17 | $web.ScriptErrorsSuppressed = $true
18 | $web.Add_DocumentCompleted($DocComp)
19 | $form.Controls.Add($web)
20 | $form.Add_Shown({$form.Activate()})
21 | $form.ShowDialog() | Out-Null
22 | $Global:authQuery = $web.url.Query
23 | Write-Verbose $web.Url.Query
24 | }
25 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONConfig.ps1:
--------------------------------------------------------------------------------
1 | Function Get-ONConfig {
2 | Param(
3 | [string]$Path="$HOME\.config\OneNoteUtilities.config"
4 | )
5 | $Global:settings = @{}
6 | if (Test-Path $path) {
7 | $config = [xml](Get-Content $path)
8 | foreach ($node in $config.settings.setting) {
9 | $value = $node.Value
10 | $Global:settings[$node.Name] = $value
11 | }
12 | }
13 | Else {
14 | Write-Error "No config file found. Please create one."
15 | # Exit
16 | }
17 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONItem.ps1:
--------------------------------------------------------------------------------
1 | # Generic call for OneNote Item. Creates a URI from an item type and ID
2 | # or uses a URI if provided. Returns metadata objects only if by ID. As a generic call
3 | # this should normally be accessed via one of the object specific wrapper
4 | # functions, such as Get-ONNoteBook
5 | Function Get-ONItem {
6 | [cmdletbinding()]
7 | Param(
8 | [Parameter(ParameterSetName='uri')]
9 | [string]$uri,
10 | [ValidateSet("notebooks","sectiongroups","sections","pages")]
11 | [Parameter(ParameterSetName='id')]
12 | [string]$ItemType,
13 | [Parameter(ParameterSetName='id')]
14 | [string]$Id
15 | )
16 |
17 | if ($id) {
18 | $id = [System.Uri]::EscapeURIString($id)
19 | Write-Verbose $id
20 | $uri = '{0}{2}/{1}' -f $ONURI, $Id, $ItemType
21 | }
22 | Write-Verbose "URI: $uri"
23 | Get-ONTokenStatus
24 | $endloop = $false
25 | [Int]$retrycount = 0
26 | do {
27 | try {
28 | $workitem = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -uri $uri #.html.InnerXML
29 | $endloop = $true
30 | }
31 | catch {
32 | if ($retrycount -gt 3) {
33 | throw $_
34 | $endloop = $true
35 | }
36 | else {
37 | Start-Sleep -Seconds 1
38 | Write-Verbose "Waiting..."
39 | $Retrycount += 1
40 | }
41 | }
42 |
43 | } While ($endloop -eq $false)
44 |
45 | return $workitem
46 |
47 | }
48 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONItems.ps1:
--------------------------------------------------------------------------------
1 | # Generic call for list of OneNote Items. The same notes apply
2 | # as for the singular Get-ONItem function.
3 | Function Get-ONItems {
4 | [CmdletBinding()]
5 | Param(
6 | [Parameter(ParameterSetName='type')]
7 | [Parameter(ParameterSetName='uri')]
8 | [switch]$List,
9 | [ValidateSet("notebooks","sectiongroups","sections","pages")]
10 | [Parameter(ParameterSetName='type')]
11 | [string]$ItemType,
12 | [Parameter(ParameterSetName='type')]
13 | [Parameter(ParameterSetName='uri')]
14 | [string]$Filter,
15 | [Parameter(ParameterSetName='uri')]
16 | [String]$uri
17 | )
18 |
19 | if ($filter) {
20 | $filter = [System.Net.WebUtility]::UrlEncode($filter)
21 | $filter = "`&`$filter=$filter"
22 | }
23 |
24 | if ($uri) {
25 | #$workuri ='{0}?top=20{1}' -f $uri, $filter
26 | $workuri = '{0}?{1}' -f $uri, $filter
27 | #$workuri = $uri
28 | }
29 | else {
30 | #$workuri = '{0}{2}?$top=100{1}&$orderby=createdDateTime' -f $ONuri, $filter, $itemtype
31 | $workuri = '{0}{2}?{1}' -f $ONuri, $filter, $itemtype
32 | }
33 |
34 |
35 | Get-ONTokenStatus
36 |
37 | # Use paging...
38 | do {
39 | $onitemlist = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken"} -uri $workuri -Method Get
40 | if ($onitemlist.'@odata.nextLink') {
41 | $workuri = $onitemlist.'@odata.nextLink'
42 | Write-Verbose "Next: $workuri"
43 | }
44 | if ($list.isPresent) {
45 | $r += $onitemlist.value
46 | }
47 | else {
48 | $onitemlist.value | ForEach-Object {
49 | Get-ONItem -uri $_.contenturl
50 | }
51 | }
52 | } While ($workuri -eq $onitemlist.'@odata.nextLink')
53 | Return $r
54 | }
55 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONTokenStatus.ps1:
--------------------------------------------------------------------------------
1 | # Auth code only valid for 1 hour
2 | # Refresh token valid for 14 days
3 | # Access token valid for 1 hour
4 | Function Get-ONTokenStatus {
5 | [cmdletbinding()]
6 | Param()
7 | Write-Verbose "Token Expires: $tokenExpires"
8 | Write-Verbose "Curent DateTime: $((Get-Date).ToFileTimeUtc())"
9 | Write-Verbose "Current Auth Code: $authCode"
10 | if (("$((Get-Date).ToFileTimeUtc())" -ge "$tokenExpires") -or !$authcode ) {
11 | Get-ONAuthCode
12 | # Extract Access token from the returned URI
13 | $Global:authQuery -match '\?code=(.*)' | Out-Null
14 | $Global:authCode = $matches[1]
15 | Write-Verbose "Received an authCode, $authCode"
16 | Get-ONAccessToken
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Get-ONaccessToken.ps1:
--------------------------------------------------------------------------------
1 | # Get a Graph Access Token
2 | Function Get-ONAccessToken {
3 | [cmdletbinding()]
4 | Param()
5 | if ($refresh_token) {
6 | $body = "grant_type=refresh_token&redirect_uri=$redirectUriEncoded&client_id=$clientIdEncoded&refresh_token=$refresh_token&scope=$scopeEncoded"
7 | }
8 | else {
9 | $body = "grant_type=authorization_code&redirect_uri=$redirectUriEncoded&client_id=$clientIdEncoded&code=$authCode&scope=$scopeEncoded"
10 | }
11 | Write-Verbose $body
12 | $Authorization = Invoke-RestMethod -uri $tokenuri -Method Post -ContentType "application/x-www-form-urlencoded" -Body $body -ErrorAction STOP
13 | $Global:accesstoken = $Authorization.access_token
14 | $Global:refresh_token = $Authorization.refresh_token
15 | $Global:tokenExpires = "$((Get-Date).AddSeconds($Authorization.expires_in).ToFileTimeUtc())"
16 | Write-Verbose "Token Expires at: $((Get-Date).AddSeconds($Authorization.expires_in).ToFileTimeUtc())"
17 | Write-Verbose "Refresh Token: $refresh_token"
18 | }
19 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/New-ONConfig.ps1:
--------------------------------------------------------------------------------
1 | function New-ONConfig {
2 | Param(
3 | [string]$ClientID
4 | )
5 | [xml]$configtemplate = @"
6 |
7 |
8 |
9 |
10 |
11 | "@
12 | $path="$HOME\.config\OneNoteUtilities.config"
13 | $configtemplate.Save($path)
14 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/New-ONJSONItem.ps1:
--------------------------------------------------------------------------------
1 | Function New-ONJSONItem {
2 | Param(
3 | $Hashtable
4 | )
5 | $workJSOn = $hashtable | ConvertTo-Json
6 | Return $workJSON
7 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Private/Save-ONConfig.ps1:
--------------------------------------------------------------------------------
1 | Function Save-ONConfig {
2 | [CmdletBinding()]
3 | Param(
4 | [string]$path="$HOME\.config\OneNoteUtilities.config",
5 | [switch]$Force
6 | )
7 | $config = [xml]''
8 | foreach ($node in $settings.Keys) {
9 | $setting = $config.CreateElement('setting')
10 | $setting.SetAttribute('name',$node)
11 | $setting.SetAttribute('value',$settings[$node])
12 | $config['settings'].AppendChild($setting) | Out-Null
13 | }
14 | if ((Test-Path $path) -and (!$Force.IsPresent)) {
15 | Throw "Config file exists"
16 | }
17 | else {
18 | $config.Save($path)
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONDefaultSection.ps1:
--------------------------------------------------------------------------------
1 | # Gets the default OneNote Section for new content creation
2 | Function Get-ONDefaultSection {
3 | Get-ONSections -filter "isDefault eq true"
4 | }
5 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONElement.ps1:
--------------------------------------------------------------------------------
1 | # Get an element on a page - returned as an XML element
2 | Function Get-ONElement {
3 | Param(
4 | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'id')]
5 | [Parameter(Mandatory = $true, ValueFromPipeline = $true, ParameterSetName = 'data-id')]
6 | [object]$Page,
7 | [Parameter(Mandatory = $true, ParameterSetName = 'id')]
8 | [string]$Id,
9 | [Parameter(Mandatory = $true, ParameterSetName = 'data-id')]
10 | [string]$DataId
11 | )
12 | if ($dataId) { $id = $dataId }
13 | switch ($Page.GetType().Name) {
14 | 'PSCustomObject' {
15 | $page = Get-ONPageXML -page $page
16 | break
17 | }
18 | 'XmlDocument' {
19 |
20 | }
21 | }
22 | $workElement = $page.SelectSingleNode("/html/body//*[@$($PSCmdlet.ParameterSetName)='$($id)']")
23 | Return $workElement
24 | }
25 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONNoteBook.ps1:
--------------------------------------------------------------------------------
1 | # Get a OneNote Notebook
2 | Function Get-ONNoteBook {
3 | Param(
4 | [Parameter(ParameterSetName = 'uri', Mandatory = $true)]
5 | [string]$Uri,
6 | [Parameter(ParameterSetName = 'id', ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
7 | [string]$Id
8 | )
9 |
10 | if ($uri) {
11 | Get-ONItem -uri $uri
12 | }
13 |
14 | if ($Id) {
15 | Get-ONItem -ItemType 'noteBooks' -Id $Id
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONNoteBooks.ps1:
--------------------------------------------------------------------------------
1 | # Get a list of OneNote Notebooks
2 | Function Get-ONNoteBooks {
3 | Param(
4 | [parameter(ParameterSetName = 'filter', Mandatory = $False)]
5 | [string]$Filter
6 | )
7 | if ($Filter) {
8 | Get-ONItems -List -ItemType 'notebooks' -Filter $filter
9 | }
10 | else {
11 | Get-ONItems -List -uri "$($ONURI)notebooks"
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONPage.ps1:
--------------------------------------------------------------------------------
1 | # Get a OneNote Page - just the metadata
2 | Function Get-ONPage {
3 | Param(
4 | [Parameter(ParameterSetName = 'uri', Mandatory = $true)]
5 | [string]$Uri,
6 | [Parameter(ParameterSetName = 'id', ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
7 | [string]$Id
8 | )
9 |
10 | if ($uri) {
11 | Get-ONItem -uri $uri
12 | }
13 |
14 | if ($Id) {
15 | Get-ONItem -ItemType 'pages' -Id $Id
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONPagePreview.ps1:
--------------------------------------------------------------------------------
1 | # Get the preview text for a OneNote Page
2 | Function Get-ONPagePreview {
3 | [CmdletBinding()]
4 | Param(
5 | [Parameter(ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
6 | [string]$Id
7 | )
8 | $workuri = "{0}pages/{1}/preview" -f $ONuri, $id
9 | Write-Verbose $workuri
10 | (Get-ONItem -uri $workuri).previewText
11 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONPageXML.ps1:
--------------------------------------------------------------------------------
1 | # Gets the XHTML content of a Page
2 | Function Get-ONPageXML {
3 | Param(
4 | [Parameter(ParameterSetName='page',Mandatory=$true,ValueFromPipeline=$true)]
5 | [object]$Page,
6 | [Parameter(ParameterSetName='id',Mandatory=$true,ValueFromPipeline=$true)]
7 | [string]$Id,
8 | [Parameter()]
9 | [Switch]$AsText
10 | )
11 | #$html = New-Object System.Xml.XmlDocument
12 | switch ($PSCmdlet.ParameterSetName) {
13 | "page" {
14 | $workuri = "{0}{1}" -f "$($Page.contenturl)" , '?includeIDS=true'
15 | }
16 | "id" {
17 | $workuri = "{0}{1}" -f (Get-ONPage -id $id).contenturl, '?includeIDS=true'
18 | }
19 | }
20 | $html = (Get-ONItem -uri $workuri)
21 | if ($ASText.IsPresent) {
22 | Return $html.OuterXML
23 | }
24 | else {
25 | Return $html
26 | }
27 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONPages.ps1:
--------------------------------------------------------------------------------
1 | # Get a list of OneNote Pages matching the given Filter
2 | Function Get-ONPages {
3 | [CmdletBinding()]
4 | Param(
5 | [parameter(ParameterSetName = 'filter', Mandatory = $true)]
6 | [string]$Filter,
7 | [parameter(ParameterSetName = "uri", Mandatory = $false)]
8 | [string]$Uri = "$($ONURI)pages"
9 | )
10 | if ($Filter) {
11 | Get-ONItems -List -ItemType 'pages' -Filter $filter
12 | }
13 | else {
14 | Write-Verbose $uri
15 | Get-ONItems -List -uri $uri
16 | }
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONRecentNoteBooks.ps1:
--------------------------------------------------------------------------------
1 | # Get a list of recently accessed OneNote NoteBooks
2 | Function Get-ONRecentNoteBooks {
3 | $uri = "{0}{1}" -f $ONuri, 'notebooks/getrecentnotebooks(includePersonalNotebooks=true)'
4 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method Get
5 | Return $response.value
6 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONResource.ps1:
--------------------------------------------------------------------------------
1 | # Gets the binary data of a file or image resource object
2 | Function Get-ONResource {
3 | Param(
4 | [Parameter(Mandatory = $true)]
5 | [string]$Uri,
6 | [Parameter(Mandatory = $true)]
7 | [string]$Path
8 | )
9 | Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $Uri -OutFile $Path
10 | }
11 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONSection.ps1:
--------------------------------------------------------------------------------
1 | # Get a OneNote Section
2 | Function Get-ONSection {
3 | Param(
4 | [Parameter(ParameterSetName = 'uri', Mandatory = $true)]
5 | [string]$Uri,
6 | [Parameter(ParameterSetName = 'id', ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
7 | [string]$Id
8 | )
9 |
10 | if ($uri) {
11 | Get-ONItem -uri $uri
12 | }
13 |
14 | if ($Id) {
15 | Get-ONItem -ItemType 'sections' -Id $Id
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONSectionGroup.ps1:
--------------------------------------------------------------------------------
1 | # Get a OneNote Section Group
2 | Function Get-ONSectionGroup {
3 | Param(
4 | [Parameter(ParameterSetName = 'uri', Mandatory = $true)]
5 | [string]$Uri,
6 | [Parameter(ParameterSetName = 'id', ValueFromPipelineByPropertyName = $true, Mandatory = $true)]
7 | [string]$Id
8 | )
9 |
10 | if ($uri) {
11 | Get-ONItem -uri $uri
12 | }
13 |
14 | if ($Id) {
15 | Get-ONItem -ItemType 'sectiongroups' -Id $Id
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONSectionGroups.ps1:
--------------------------------------------------------------------------------
1 | # Gets a list of OneNote Section Groups matching the supplied filter, or a list
2 | Function Get-ONSectionGroups {
3 | [CmdletBinding()]
4 | Param(
5 | [parameter(ParameterSetName = 'filter', Mandatory = $true)]
6 | [string]$Filter,
7 | [parameter(ParameterSetName = "uri", Mandatory = $false)]
8 | [string]$Uri = "$($ONURI)sectiongroups"
9 | )
10 | if ($Filter) {
11 | Get-ONItems -List -ItemType 'sectiongroups' -Filter $filter
12 | }
13 | else {
14 | Write-Verbose $uri
15 | Get-ONItems -List -uri $uri
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONSections.ps1:
--------------------------------------------------------------------------------
1 | # Get a list of OneNote Sections
2 | Function Get-ONSections {
3 | [CmdletBinding()]
4 | Param(
5 | [parameter(ParameterSetName = 'filter', Mandatory = $true)]
6 | [string]$Filter,
7 | [parameter(ParameterSetName = "uri", Mandatory = $false)]
8 | [string]$Uri = "$($ONURI)sections"
9 | )
10 | if ($Filter) {
11 | Get-ONItems -List -ItemType 'sections' -Filter $filter
12 | }
13 | else {
14 | Write-Verbose $uri
15 | Get-ONItems -List -uri $uri
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONSessionVariables.ps1:
--------------------------------------------------------------------------------
1 | # Gets some useful information about the OneNote session
2 | Function Get-ONSessionVariables {
3 | [CmdletBinding()]
4 | Param()
5 | Write-Host ("OneNote URI : {0}" -f "$ONuri")
6 | Write-Host ("Auth Code : {0}" -f "$authCode")
7 | Write-Host ("Token Expires at : {0}" -f "$tokenExpires")
8 | Write-Host ("(Local Time) : {0}" -f "$(([DateTime]::FromFileTimeUTC($tokenExpires)).ToLocalTime().ToString())")
9 | Write-Host ("Refresh Token : {0}" -f "$refresh_token")
10 | Write-Host ("Access Token : {0}" -f "$accesstoken")
11 | }
12 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Get-ONTableAsObject.ps1:
--------------------------------------------------------------------------------
1 | # Gets a table from a OneNote Page, as a custom object
2 | function Get-ONTableAsObject {
3 | [CmdletBinding()]
4 | Param(
5 | [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
6 | $Table,
7 | [Switch]$NoHeaders
8 | )
9 | $cols = $table.tr[0].td.Count
10 | $rows = $table.tr.Count
11 |
12 | Write-Debug "Rows: $rows, Columns: $cols"
13 |
14 | $tableObject = [System.Collections.ArrayList]@()
15 | $headers = @()
16 |
17 | if ($NoHeaders.IsPresent) {
18 | for ( $h = 0; $h -lt $cols; $h++ ) {
19 | $headers += ("Column $h")
20 | }
21 |
22 | }
23 | else {
24 |
25 | for ( $h = 0; $h -lt $cols; $h++ ) {
26 | $headers += ($table.tr[0].td[$h].InnerText)
27 | }
28 |
29 | }
30 |
31 |
32 | for ($i = 0; $i -lt $rows; $i++) {
33 |
34 | $rowObject = New-Object PSCustomObject
35 |
36 | if (!$NoHeaders.IsPresent -and $i -eq 0) {
37 |
38 | }
39 | else {
40 |
41 | for ($c = 0 ; $c -lt $cols; $c++) {
42 | Write-Debug "Row $i, Column $c"
43 | $rowObject | Add-Member -MemberType NoteProperty -Name $headers[$c] -Value $table.tr[$i].td[$c].InnerText
44 |
45 | }
46 | [Void]$tableObject.Add($rowObject)
47 | }
48 |
49 | }
50 |
51 | return $tableObject
52 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Invoke-ONApp.ps1:
--------------------------------------------------------------------------------
1 | # Invoke the default OneNote application and load a page
2 | Function Invoke-ONApp {
3 | [CmdletBinding(DefaultParameterSetName = 'page')]
4 | Param(
5 | [parameter(Position = 0, ParameterSetName = "page", Mandatory = $true)]
6 | [object]$Page,
7 | [parameter(Position = 0, ParameterSetName = "id", Mandatory = $true)]
8 | [string]$Id
9 | )
10 | If ($Id) {
11 | $page = Get-ONItem -ItemType 'pages' -Id $id
12 | }
13 | Start-Process $page.links.oneNoteClientUrl.href
14 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Invoke-ONDesktop.ps1:
--------------------------------------------------------------------------------
1 | # Invoke the Office 365 OneNote application and load a page
2 | Function Invoke-ONDesktop {
3 | [CmdletBinding(DefaultParameterSetName = 'page')]
4 | Param(
5 | [parameter(Position = 0, ParameterSetName = "page", Mandatory = $true)]
6 | [object]$Page,
7 | [parameter(Position = 0, ParameterSetName = "id", Mandatory = $true)]
8 | [string]$Id
9 | )
10 | If ($Id) {
11 | $page = Get-ONItem -ItemType 'pages' -Id $id
12 | }
13 | # Location of the OneNote executable can be found at:
14 | # HKEY_LOCAL_MACHINE\SOFTWARE\Classes\OneNote\shell\Open\command
15 | $ONShellCommand = (Get-ItemProperty -Path HKLM:\SOFTWARE\Classes\OneNote\shell\Open\command).'(default)'
16 | #Clean up the registry entry so that we can reuse it
17 | $ONShellCommand = ($ONShellCommand -split "/hyperlink" -replace '"')[0]
18 | Start-Process $ONShellCommand -ArgumentList '/hyperlink', $page.links.oneNoteClientUrl.href
19 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Invoke-ONWeb.ps1:
--------------------------------------------------------------------------------
1 | # Invoke the OneNote web app and load a page
2 | Function Invoke-ONWeb {
3 | [CmdletBinding(DefaultParameterSetName = 'page')]
4 | Param(
5 | [Parameter(Position = 0, ParameterSetName = 'page', Mandatory = $true)]
6 | [object]$Page,
7 | [Parameter(Position = 0, ParameterSetName = 'Id', Mandatory = $true)]
8 | [string]$Id
9 | )
10 | If ($Id) {
11 | $page = Get-ONItem -ItemType 'pages' -Id $id
12 | }
13 | Start-Process $page.links.oneNoteWebUrl.href
14 | }
15 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONElement.ps1:
--------------------------------------------------------------------------------
1 | # Create a new element
2 | Function New-ONElement {
3 | Param(
4 | [Parameter(Mandatory = $true)]
5 | [ValidateSet(
6 | "head", "body", "title", "meta", "h1", "h2", "h3", "h4", "h5", "h6", "p", "ul", "ol", "li", "pre", "b", "i", "table", "tr", "td", "div", "span", "img", "br", "cite", "text"
7 | )]
8 | [string]$Type,
9 | [Parameter()]
10 | [object]$Document = (New-Object -TypeName System.Xml.XmlDocument)
11 | )
12 | if ($Type -eq 'text') {
13 | $workelement = $Document.CreateTextNode('')
14 | }
15 | else {
16 | $workelement = $Document.CreateElement($Type)
17 | $workelement.setAttribute("lang", "en-GB")
18 | #$workElement.load(([String]$workelement).replace('"',"'"))
19 | }
20 |
21 | Return $workelement
22 | }
23 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONNoteBook.ps1:
--------------------------------------------------------------------------------
1 | # Create a new OneNote NoteBook
2 | Function New-ONNoteBook {
3 | Param (
4 | [Parameter(Mandatory = $true)]
5 | [string]$DisplayName
6 | )
7 | $uri = "{0}{1}" -f $ONuri, 'notebooks'
8 | $body = New-ONJSONItem -hashtable @{ 'displayname' = $DisplayName }
9 | Write-Verbose $body
10 | Get-ONTokenStatus
11 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method post -Body $body -ContentType "application/json"
12 | Write-Verbose "NoteBook"
13 | Return $response
14 | }
15 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONPage.ps1:
--------------------------------------------------------------------------------
1 | # Create new Page in OneNote. Uses the default section if no section URI provided
2 | Function New-ONPage {
3 | Param(
4 | [Parameter(ParameterSetName = 'html')]
5 | [Parameter(ParameterSetName = 'page')]
6 | [string]$Uri = (Get-ONDefaultSection).pagesUrl,
7 | [Parameter(ParameterSetName = 'html', Mandatory = $true)]
8 | [string]$Html,
9 | [Parameter(ParameterSetName = 'page', Mandatory = $true)]
10 | [object]$Page
11 | )
12 |
13 | if ($Page) {
14 | $html = $page.OuterXML
15 | }
16 |
17 | $body = "{0}{1}" -f '', $html
18 | Write-Verbose $body
19 | Get-ONTokenStatus
20 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method post -Body $body -ContentType "text/html"
21 | Write-Verbose "Page"
22 | Return $response
23 | }
24 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONPageXML.ps1:
--------------------------------------------------------------------------------
1 | # Creates a new XHTML page with Title and appropriate metadata
2 | Function New-ONPageXML {
3 | Param(
4 | [Parameter(Mandatory = $true)]
5 | [string]$Title
6 | )
7 |
8 | <#
9 | Create pages as an XML document to allow the normal DOM type manipulations, then when done,
10 | coerce the page to a string and add the "" to the top before passing it to the Graph
11 | #>
12 |
13 | $xmldoc = New-Object System.Xml.XmlDocument
14 | $xmldoc.InnerXml = ''
15 | $xmldoc.DocumentElement.SetAttribute("lang", "en-GB")
16 | $tag = New-ONElement -type head -Document $xmldoc
17 | $head = $xmldoc.DocumentElement.AppendChild($tag)
18 | $tag = New-ONElement -Type title -Document $xmldoc
19 | $tag.innertext = $Title
20 | $title = $head.AppendChild($tag)
21 | $tag = New-ONElement -type meta -Document $xmldoc
22 | $tag.setAttribute('http-equiv', "Content-Type")
23 | $tag.setAttribute('content', 'text/html; charset=utf-8')
24 | $meta = $head.AppendChild($tag)
25 | $tag = New-ONElement -type meta -Document $xmldoc
26 | $tag.setAttribute('name', "created")
27 | $tag.setAttribute('content', $((Get-Date).ToString('O')))
28 | $meta = $head.AppendChild($tag)
29 | $tag = New-ONElement -Type body -Document $xmldoc
30 | $body = $xmldoc.DocumentElement.AppendChild($tag)
31 | Return $xmldoc
32 | }
33 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONSection.ps1:
--------------------------------------------------------------------------------
1 | # Create a new OneNote Section
2 | Function New-ONSection {
3 | Param(
4 | [Parameter(ParameterSetName = 'ByID', Mandatory = $true)]
5 | [Parameter(ParameterSetName = 'ByUri', Mandatory = $true)]
6 | [string]$DisplayName,
7 | [Parameter(ParameterSetName = 'ByUri', Mandatory = $true)]
8 | [string]$Uri,
9 | [Parameter(ParameterSetName = 'ByID', Mandatory = $true)]
10 | [string]$Id,
11 | [Parameter(ParameterSetName = "ByID")]
12 | [switch]$SectionGroup
13 | )
14 | if ($id) {
15 | if ($SectionGroup.IsPresent) {
16 | $uri = "{0}sectionGroups/{1}/sections" -f $ONuri, $id
17 | }
18 | else {
19 | $uri = "{0}notebooks/{1}/sections" -f $ONuri, $id
20 | }
21 | }
22 | $body = New-ONJSONItem -hashtable @{ 'displayname' = $DisplayName }
23 | Write-Verbose $body
24 | Get-ONTokenStatus
25 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method post -Body $body -ContentType "application/json"
26 | Write-Verbose "Section"
27 | Return $response
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/New-ONSectionGroup.ps1:
--------------------------------------------------------------------------------
1 | # Create a new OneNote Section Group
2 | Function New-ONSectionGroup {
3 | Param(
4 | [Parameter(ParameterSetName = 'ByID', Mandatory = $true)]
5 | [Parameter(ParameterSetName = 'ByUri', Mandatory = $true)]
6 | [string]$DisplayName,
7 | [Parameter(ParameterSetName = 'ByUri', Mandatory = $true)]
8 | [string]$Uri,
9 | [Parameter(ParameterSetName = 'ByID', Mandatory = $true)]
10 | [string]$Id
11 | )
12 | if ($id) {
13 | $uri = "{0}notebooks/{1}/sectiongroups" -f $ONuri, $id
14 | }
15 | $body = New-ONJSONItem -hashtable @{ 'displayname' = $DisplayName }
16 | Write-Verbose $body
17 | Get-ONTokenStatus
18 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method post -Body $body -ContentType "application/json"
19 | Write-Verbose "SectionGroup"
20 | Return $response
21 | }
22 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Remove-ONPage.ps1:
--------------------------------------------------------------------------------
1 | # Delete a OneNote Page
2 | Function Remove-ONPage {
3 | Param (
4 | [Parameter(Mandatory = $true)]
5 | [string]$Id
6 | )
7 | $uri = "{0}{1}/{2}" -f $ONuri, 'pages', $Id
8 | Get-ONTokenStatus
9 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method delete
10 | Write-Verbose "Page"
11 | Return $response
12 | }
13 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Set-ONPageLevel.ps1:
--------------------------------------------------------------------------------
1 | # Set the level of a NoneNote Page
2 | Function Set-ONPageLevel {
3 | Param(
4 | [Parameter(Mandatory = $true)]
5 | [string]$Id,
6 | [Parameter(Mandatory = $true)]
7 | [ValidateSet(0, 1, 2)]
8 | $Level
9 | )
10 | $uri = "{0}{1}/{2}" -f $ONuri, 'pages', $Id
11 | $body = ConvertTo-Json @{ 'level' = "$Level" }
12 | Get-ONTokenStatus
13 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method Patch -ContentType 'application/json' -body $body
14 | Return $response
15 | }
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Test-ONUtilities.ps1:
--------------------------------------------------------------------------------
1 | # Some tests of functionality...
2 | Function Test-ONUtilities {
3 | [CmdletBinding()]
4 | Param()
5 |
6 | Write-Verbose "Test Suite"
7 | Write-Verbose "=========="
8 |
9 | Write-Verbose "_-Getting Default Section-_"
10 | $ds = Get-ONDefaultSection
11 | $ds | Select-Object * | Write-Verbose
12 |
13 | Write-Verbose "_-Creating a new Page-_"
14 | $np = New-ONPageXML -Title "Test Page $((Get-Date).toString())"
15 | $h1 = New-ONElement -Type h1 -Document $np
16 | $h1.InnerText = 'Hello World'
17 | [void]$np['html']['body'].AppendChild($h1)
18 | $workuri = $ds.pagesUrl
19 | Write-Verbose "Creating Page at: $workuri"
20 | $response = New-ONPage -URI $workuri -Page $np
21 | Write-Verbose "Sent"
22 |
23 | Write-Verbose "_-Trying to get the new Page-_"
24 | Start-Sleep -Seconds 3
25 | Get-ONPage -id $response.Id | ForEach-Object { (Get-ONPageXML -Page $_).OuterXML }
26 |
27 | Write-Verbose "_-Trying to get a specific element from a specific Page-_"
28 | $p = Get-ONPageXML -Page (Get-ONPage -id '0-e05582da051f07451e1cd93706e57838!1-816F7725BEF00A5F!1299')
29 | Get-ONElement -page $p -id 'div:{c2203899-70ef-0fcb-135b-bc6bfdfc4e09}{32}'
30 |
31 | Write-Verbose "_-Testing Paging-_"
32 | (Get-ONPages -uri "$((Get-ONSections -Filter "displayName eq 'Unfiled Notes'").pagesurl)").Count
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/Public/Update-ONelement.ps1:
--------------------------------------------------------------------------------
1 | # Update an existing element on a OneNote Page
2 | Function Update-ONElement {
3 | Param(
4 | [Parameter(Mandatory = $true)]
5 | [string]$Id,
6 | [Parameter(Mandatory = $true)]
7 | [string]$TargetId,
8 | [Parameter(Mandatory = $true)]
9 | [ValidateSet('replace', 'append', 'delete', 'prepend')]
10 | [string]$Action,
11 | [Parameter(Mandatory = $true)]
12 | [string]$Content,
13 | [Parameter(Mandatory = $false)]
14 | [ValidateSet('after', 'before')]
15 | [string]$Position = 'before'
16 | )
17 | Get-ONTokenStatus
18 | $uri = "{0}{1}/{2}/content" -f $ONuri, 'pages', $Id
19 | $body = ConvertTo-Json @{ 'target' = "$targetId"; 'action' = "$action"; 'content' = $content; 'position' = "$position" }
20 | Write-Verbose $body
21 | Get-ONTokenStatus
22 | $response = Invoke-RestMethod -Headers @{Authorization = "Bearer $accesstoken" } -uri $uri -Method Patch -ContentType 'application/json' -body $body
23 | Return $response
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/OneNoteUtilitiesGraph/en-US/about_OneNoteUtilitieGraph.help.txt:
--------------------------------------------------------------------------------
1 | TOPIC
2 | about_OneNoteUtilitiesGraph
3 |
4 | SHORT DESCRIPTION
5 | The underlying ideas behind the OneNoteUtilitiesGraph module.
6 |
7 | LONG DESCRIPTION
8 | The purpose of the OneNoteUtilitiesGraph module is to allow programmatic access to
9 | information stored in the cloud version of OneNote. This includes both the structure
10 | of the OneNote data (NoteBooks, SectionsGroups, Sections, Pages) but also the page contents
11 | themselves as structured XHTML data. The idea is to allow automation of tasks that are
12 | time-consuming using the User Interface and to possibly allow integration with other
13 | apps and services.
14 |
15 | OneNoteUtilitiesGraph is designed to be used both from within scripts and also from
16 | the command line.
17 |
18 | EXAMPLES
19 |
20 | KEYWORDS
21 | OneNote
22 |
23 | SEE ALSO
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # OneNoteUtilitiesGraph
2 | PowerShell module for manipulating [Microsoft OneNote](https://www.onenote.com/) data using the Microsoft Graph REST API.
3 |
4 | Following on from OneNoteUtilities this module uses the Microsoft Graph API instead of the desktop COM objects.
5 |
6 | ### Update November 5, 2019
7 | Following Microsoft's decision to continue support for the desktop version of OneNote I have added an additional command (`Invoke-ONDesktop`) to open OneNote pages in the desktop application specifically, rather than whichever application has been registered as the handler for the `onenote:` protocol.
8 |
9 |
10 | This module provides the following functions:
11 |
12 | * [Get-ONConfig](docs/Get-ONConfig.md)
13 | * [Get-ONDefaultSection](docs/Get-ONDefaultSection.md)
14 | * [Get-ONElement](docs/Get-ONElement.md)
15 | * [Get-ONNoteBook](docs/Get-ONNoteBook.md)
16 | * [Get-ONNoteBooks](docs/Get-ONNoteBooks.md)
17 | * [Get-ONPage](docs/Get-ONPage.md)
18 | * [Get-ONPagePreview](docs/Get-ONPagePreview.md)
19 | * [Get-ONPages](docs/Get-ONPages.md)
20 | * [Get-ONPageXML](docs/Get-ONPageXML.md)
21 | * [Get-ONRecentNoteBooks](docs/Get-ONRecentNoteBooks.md)
22 | * [Get-ONResource](docs/Get-ONResource.md)
23 | * [Get-ONResources](docs/Get-ONResources.md)
24 | * [Get-ONSection](docs/Get-ONSection.md)
25 | * [Get-ONSectionGroup](docs/Get-ONSectionGroup.md)
26 | * [Get-ONSectionGroups](docs/Get-ONSectionGroups.md)
27 | * [Get-ONSections](docs/Get-ONSections.md)
28 | * [Get-ONSessionVariables](docs/Get-ONSessionVariables.md)
29 | * [Get-ONTableAsObject](docs/Get-ONTableAsObject.md)
30 | * [Get-ONTokenStatus](docs/Get-ONTokenStatus.md)
31 | * [Invoke-ONApp](docs/Invoke-ONApp.md)
32 | * [Invoke-ONDesktop](docs/Invoke-ONDesktop.md)
33 | * [Invoke-ONWeb](docs/Invoke-ONWeb.md)
34 | * [New-ONElement](docs/New-ONElement.md)
35 | * [New-ONNoteBook](docs/New-ONNoteBook.md)
36 | * [New-ONPage](docs/New-ONPage.md)
37 | * [New-ONPageXML](docs/New-ONPageXML.md)
38 | * [New-ONSection](docs/New-ONSection.md)
39 | * [New-ONSectionGroup](docs/New-ONSectionGroup.md)
40 | * [Remove-ONPage](docs/Remove-ONPage.md)
41 | * [Save-ONConfig ](docs/Save-ONConfig.md)
42 | * [Set-ONPageLevel](docs/Set-ONPageLevel.md)
43 | * [Test-ONUtilities](docs/Test-ONUtilities.md)
44 | * [Update-ONElement](docs/Update-ONElement.md)
45 | * ~Remove-ONNoteBook~
46 | * ~Remove-ONSectionGroup~
47 | * ~Remove-ONSection~
48 | * ~Remove-ONElement~
49 | * ~Copy-ONPage~
50 |
51 | ## Installation
52 |
53 | Before installing the module, ensure that you have a client ID for the Microsoft Graph service. See the following for more information [https://docs.microsoft.com/en-us/graph/auth-register-app-v2](https://docs.microsoft.com/en-us/graph/auth-register-app-v2).
54 |
55 | Create a config XML file containing your client ID and security scope information, named OneNoteUtilities.config and place it in your .config folder.
56 | There is a [template file](https://raw.githubusercontent.com/wightsci/OneNoteUtilitiesGraph/master/OneNoteUtilities.config) in this repo on GitHub.
57 |
58 | Download the module, extract it to a location in your modules path and use ```Import-Module```. The process of importing will trigger an attempt to connect to Microsoft Graph. *This will fail if you haven't set up the config file*.
59 |
60 | ## Use Case - Creating Structure
61 |
62 | Suppose that you need to create a new Notebook, create Sections for the months of the year and create a Page for each day of each month. You would like the title of each page to look like 'Friday, 26th June 2019' in format. With OneNoteUtiliesGraph you can do this:
63 |
64 | ```powershell
65 | Import-Module OneNoteUtilitiesGraph
66 | #Create NoteBook
67 | $NoteBook = New-ONNoteBook -DisplayName (Get-Date).Year
68 | #Create Section per Month
69 | (1..12) | % {
70 | $month = $_;
71 | $monthName = (Get-Date -Month $_).ToString('MMMM')
72 | $Section = New-ONSection -Id $NoteBook.Id -DisplayName $monthName
73 | #Create Page per Day
74 | (1..$([DateTime]::DaysInMonth((Get-Date).Year,$month))) | % {
75 | $Page = New-ONPageXML -Title $((Get-Date -Year (Get-Date).Year -Month $month -Day $_).toString("dddd dd MMMM yyyy"))
76 | New-ONPage -URI $Section.pagesURL -Page $Page
77 | }
78 | }
79 | ```
80 | It takes a couple of minutes, but that's much shorter than creating 12 month Sections and 365 day Pages by hand.
81 |
82 | This could just as easily be applied to lists of staff, students or objects in Active Directory.
83 |
84 | ## Use Case - Querying
85 |
86 |
87 | ```powershell
88 | Get-ONPages -Filter "parentNoteBook/displayname eq '2019' and startswith(title,'Monday')"
89 | ```
90 |
91 | ## More use cases - see the Wiki
92 |
93 | [https://github.com/wightsci/OneNoteUtilitiesGraph/wiki](https://github.com/wightsci/OneNoteUtilitiesGraph/wiki)
94 |
95 | ## Future Developments
96 |
97 | * Support for Tags
98 | * HTML Templates for Pages
99 | * Merge-to-OneNote
100 | * Out-ONPage
101 | * New Graph REST features as they are released
102 |
--------------------------------------------------------------------------------
/azure-pipelines.yml:
--------------------------------------------------------------------------------
1 | # Starter pipeline
2 | # Start with a minimal pipeline that you can customize to build and deploy your code.
3 | # Add steps that build, run tests, deploy, and more:
4 | # https://aka.ms/yaml
5 |
6 | trigger:
7 | - master
8 |
9 | pool:
10 | vmImage: 'windows-latest'
11 |
12 | steps:
13 | - script: echo Hello, world!
14 | displayName: 'Run a one-line script'
15 |
16 | - script: |
17 | echo Add other tasks to build, test, and deploy your project.
18 | echo See https://aka.ms/yaml
19 | displayName: 'Run a multi-line script'
20 |
--------------------------------------------------------------------------------
/docs/Copy-ONPage.md:
--------------------------------------------------------------------------------
1 | ---
2 | external help file: OneNoteUtilitiesGraph-help.xml
3 | Module Name: OneNoteUtilitiesGraph
4 | online version:
5 | schema: 2.0.0
6 | ---
7 |
8 | # Copy-ONPage
9 |
10 | ## SYNOPSIS
11 | {{ Fill in the Synopsis }}
12 |
13 | ## SYNTAX
14 |
15 | ### page
16 | ```
17 | Copy-ONPage [-Page