Discover how a bimodal integration strategy can address the major data management challenges facing your organization today.
Get the Report →Automate Microsoft OneDrive Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Microsoft OneDrive data from PowerShell? This article demonstrates how to utilize the Microsoft OneDrive Cmdlets for tasks like connecting to Microsoft OneDrive data, automating operations, downloading data, and more.
The CData Cmdlets for Microsoft OneDrive are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time access to Microsoft OneDrive.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Microsoft OneDrive, but also an SQL interface; this tutorial shows how to use both to retrieve Microsoft OneDrive data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Microsoft OneDrive. To access Microsoft OneDrive data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Microsoft OneDrive.
Once you have acquired the necessary connection properties, accessing Microsoft OneDrive data in PowerShell can be enabled in three steps.
OneDrive 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 the OAuth section of the Help documentation for an authentication guide.
PowerShell
-
Install the module:
Install-Module OneDriveCmdlets
-
Connect:
$onedrive = Connect-OneDrive -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -OAuthCallbackURL "$OAuthCallbackURL"
-
Search for and retrieve data:
$id = "Jq74mCczmFXk1tC10GB" $files = Select-OneDrive -Connection $onedrive -Table "Files" -Where "Id = `'$Id`'" $files
You can also use the Invoke-OneDrive cmdlet to execute SQL commands:
$files = Invoke-OneDrive -Connection $onedrive -Query 'SELECT * FROM Files WHERE Id = @Id' -Params @{'@Id'='Jq74mCczmFXk1tC10GB'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Microsoft OneDrive\lib\System.Data.CData.OneDrive.dll")
-
Connect to Microsoft OneDrive:
$conn= New-Object System.Data.CData.OneDrive.OneDriveConnection("OAuthClientId=MyApplicationId;OAuthClientSecret=MySecretKey;OAuthCallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the OneDriveDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Name from Files" $da= New-Object System.Data.CData.OneDrive.OneDriveDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.name }