![]() |
VOOZH | about |
The CData Cmdlets for Microsoft Exchange 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 Microsoft Exchange.
The Cmdlets are not only a PowerShell interface to Microsoft Exchange, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Microsoft Exchange data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Exchange. To access Microsoft Exchange data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Exchange.
Once you have acquired the necessary connection properties, accessing Microsoft Exchange data in PowerShell can be enabled in three steps.
Specify the User and Password to connect to Exchange. Additionally, specify the address of the Exchange server you are connecting to and the Platform associated with the server.
Install the module:
Install-Module ExchangeCmdlets
Connect:
$exchange = Connect-Exchange -User "$User" -Password "$Password" -Server "$Server" -Platform "$Platform"
Search for and retrieve data:
$businnessaddress_city = "Raleigh" $contacts = Select-Exchange -Connection $exchange -Table "Contacts" -Where "BusinnessAddress_City = `'$BusinnessAddress_City`'" $contacts
You can also use the Invoke-Exchange cmdlet to execute SQL commands:
$contacts = Invoke-Exchange -Connection $exchange -Query 'SELECT * FROM Contacts WHERE BusinnessAddress_City = @BusinnessAddress_City' -Params @{'@BusinnessAddress_City'='Raleigh'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Exchange\lib\System.Data.CData.Exchange.dll")
Connect to Microsoft Exchange:
$conn= New-Object System.Data.CData.Exchange.ExchangeConnection("User='[email protected]';Password='myPassword';Server='https://outlook.office365.com/EWS/Exchange.asmx';Platform='Exchange_Online';")
$conn.Open()
Instantiate the ExchangeDataAdapter, execute an SQL query, and output the results:
$sql="SELECT GivenName, Size from Contacts"
$da= New-Object System.Data.CData.Exchange.ExchangeDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.givenname $_.size
}
Update-Exchange -Connection $Exchange -Columns @('GivenName','Size') -Values @('MyGivenName', 'MySize') -Table Contacts -Id "MyId"
$cmd = New-Object System.Data.CData.Exchange.ExchangeCommand("UPDATE Contacts SET BusinnessAddress_City='Raleigh' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Exchange.ExchangeParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-Exchange -Connection $Exchange -Table Contacts -Columns @("GivenName", "Size") -Values @("MyGivenName", "MySize")
$cmd = New-Object System.Data.CData.Exchange.ExchangeCommand("INSERT INTO Contacts (BusinnessAddress_City) VALUES (@myBusinnessAddress_City)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Exchange.ExchangeParameter("@myBusinnessAddress_City","Raleigh")))
$cmd.ExecuteNonQuery()
Remove-Exchange -Connection $Exchange -Table "Contacts" -Id "MyId"
$cmd = New-Object System.Data.CData.Exchange.ExchangeCommand("DELETE FROM Contacts WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.Exchange.ExchangeParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the Exchange Data Provider to get started:
Download NowLearn more:
👁 Microsoft Exchange IconThe easiest way to integrate powerful Microsoft Exchange send and receive capabilities with .NET applications. Send & Receive Email, manage Exchange messages, folders, calendars, and more!