Cmdlets for OneNote

Build 21.0.7930

Establishing a Connection

With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our OneNote Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.

Installing and Connecting

If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.

Install-Module OneNoteCmdlets

The following line is then added to your profile, loading the cmdlets on the next session:

Import-Module OneNoteCmdlets;

You can then use the Connect-OneNote cmdlet to create a connection object that can be passed to other cmdlets:

$conn = Connect-OneNote -OAuthClientId 'MyApplicationId' -OAuthClientSecret 'MySecretKey' -CallbackURL 'http://localhost:33333'

Authenticating to OneNote

OneNote uses the OAuth authentication standard. To authenticate using OAuth, you will need to create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See Using OAuth Authentication for an authentication guide.

Authenticating using Azure AD

Azure AD is a connection type that goes through OAuth. Set your AuthScheme to AzureAD and see Using OAuth Authentication for an authentication guide.

Authenticating using Azure Service Principal

Azure Service Principal is a connection type that goes through OAuth. Set your AuthScheme to AzureServicePrincipal and see Using Azure Service Principal Authentication for an authentication guide.

Authenticating using MSI Authentication

If you are running OneNote on an Azure VM, you can leverage Managed Service Identity (MSI) credentials to connect:

  • AuthScheme: Set this to AzureMSI.

The MSI credentials will then be automatically obtained for authentication.

Retrieving Data

The Select-OneNote cmdlet provides a native PowerShell interface for retrieving data:

$results = Select-OneNote -Connection $conn -Table "Notebooks" -Columns @("Id, notebook_displayName") -Where "Id='Jq74mCczmFXk1tC10GB'"
The Invoke-OneNote cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.

Piping Cmdlet Output

The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:

Select-OneNote -Connection $conn -Table Notebooks -Where "Id = 'Jq74mCczmFXk1tC10GB'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myNotebooksData.csv -NoTypeInformation

You will notice that we piped the results from Select-OneNote into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.

However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:

 
PS C:\> $conn  = Connect-OneNote -OAuthClientId 'MyApplicationId' -OAuthClientSecret 'MySecretKey' -CallbackURL 'http://localhost:33333'
PS C:\> $row = Select-OneNote -Connection $conn -Table "Notebooks" -Columns (Id, notebook_displayName) -Where "Id = 'Jq74mCczmFXk1tC10GB'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "Notebooks",
  "Columns":  [

  ],
  "Id":  "MyId",
  "notebook_displayName":  "Mynotebook_displayName"
} 

Updating Data

The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into OneNote, checking first whether a record already exists and needs to be updated instead of inserted.

Import-Csv -Path C:\MyNotebooksUpdates.csv | %{
  $record = Select-OneNote -Connection $conn -Table Notebooks -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-OneNote -Connection $conn -Table Notebooks -Columns @("Id","notebook_displayName") -Values @($_.Id, $_.notebook_displayName) -Where "Id  = `'$_.Id`'"
  }else{
    Add-OneNote -Connection $conn -Table Notebooks -Columns @("Id","notebook_displayName") -Values @($_.Id, $_.notebook_displayName)
  }
}

Copyright (c) 2021 CData Software, Inc. - All rights reserved.
Build 21.0.7930