Model Context Protocol (MCP) finally gives AI models a way to access the business data needed to make them really useful at work. CData MCP Servers have the depth and performance to make sure AI has access to all of the answers.
Try them now for free →Automate Office 365 Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Office 365 data from PowerShell? This article demonstrates how to utilize the Office 365 Cmdlets for tasks like connecting to Office 365 data, automating operations, downloading data, and more.
The CData Cmdlets for Office 365 are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Office 365.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Office 365, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Office 365 data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Office 365. To access Office 365 data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Office 365.
Once you have acquired the necessary connection properties, accessing Office 365 data in PowerShell can be enabled in three steps.
Office 365 uses the OAuth authentication standard. To authenticate requests, you will need to obtain the OAuthClientId, OAuthClientSecret, and OAuthCallbackURL by registering an app with Office 365. See the "Getting Started" chapter of the help documentation for a guide to using OAuth.
PowerShell
-
Install the module:
Install-Module Office365Cmdlets
-
Connect:
$office365 = Connect-Office365 -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -OAuthCallbackURL "$OAuthCallbackURL"
-
Search for and retrieve data:
$userid = "54f34750-0d34-47c9-9949-9fac4791cddb" $files = Select-Office365 -Connection $office365 -Table "Files" -Where "UserId = `'$UserId`'" $files
You can also use the Invoke-Office365 cmdlet to execute SQL commands:
$files = Invoke-Office365 -Connection $office365 -Query 'SELECT * FROM Files WHERE UserId = @UserId' -Params @{'@UserId'='54f34750-0d34-47c9-9949-9fac4791cddb'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Office 365\lib\System.Data.CData.Office365.dll")
-
Connect to Office 365:
$conn= New-Object System.Data.CData.Office365.Office365Connection("OAuthClientId=MyApplicationId;OAuthClientSecret=MyAppKey;OAuthCallbackURL=http://localhost:33333;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the Office365DataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Size from Files" $da= New-Object System.Data.CData.Office365.Office365DataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.size }
Update Office 365 Data
PowerShell
Update-Office365 -Connection $Office365 -Columns @('Name','Size') -Values @('MyName', 'MySize') -Table Files -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Office365.Office365Command("UPDATE Files SET UserId='54f34750-0d34-47c9-9949-9fac4791cddb' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Office 365 Data
PowerShell
Add-Office365 -Connection $Office365 -Table Files -Columns @("Name", "Size") -Values @("MyName", "MySize")
ADO.NET
$cmd = New-Object System.Data.CData.Office365.Office365Command("INSERT INTO Files (UserId) VALUES (@myUserId)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myUserId","54f34750-0d34-47c9-9949-9fac4791cddb")))
$cmd.ExecuteNonQuery()
Delete Office 365 Data
PowerShell
Remove-Office365 -Connection $Office365 -Table "Files" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.Office365.Office365Command("DELETE FROM Files WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Office365.Office365Parameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject