Cmdlets for Email

Build 23.0.8839

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 Email 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 EmailCmdlets

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

Import-Module EmailCmdlets;

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

$conn = Connect-Email -Port 993 -Server 'outlook.office365.com' -Password 'password' -User 'user' -Protocol 'IMAP' -SMTPPort 587 SMTP Server 'smtp.office365.com'

The User and Password properties in the Authentication section must be set to valid credentials. The Server must be specified to retrieve emails and the SMTPServer must be specified to send emails.

Deprecation notice for Gmail users

From May 30, 2022, Google no longer supports the use of third-party apps or devices which ask you to sign in to your Google Account using only your username and password. There are alternatives that allow your continued use of our Email drivers to connect to your Google account, which is why Gmail-specific columns, pseudo-columns, and stored procedures are only getting deprecated. We recommend you move to our Gmail drivers that offer more secure methods of authentication.

Retrieving Data

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

$results = Select-Email -Connection $conn -Table "[INBOX]" -Columns @("From, Subject") -Where "Subject='Test'"
The Invoke-Email 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-Email -Connection $conn -Table [INBOX] -Where "Subject = 'Test'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[INBOX]Data.csv -NoTypeInformation

You will notice that we piped the results from Select-Email 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-Email -Port 993 -Server 'outlook.office365.com' -Password 'password' -User 'user' -Protocol 'IMAP' -SMTPPort 587 SMTP Server 'smtp.office365.com'
PS C:\> $row = Select-Email -Connection $conn -Table "[INBOX]" -Columns (From, Subject) -Where "Subject = 'Test'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "[INBOX]",
  "Columns":  [

  ],
  "From":  "MyFrom",
  "Subject":  "MySubject"
} 

Deleting Data

The following line deletes any records that match the criteria:

Select-Email -Connection $conn -Table [INBOX] -Where "Subject = 'Test'" | Remove-Email

Modifying Data

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

Import-Csv -Path C:\My[INBOX]Updates.csv | %{
  $record = Select-Email -Connection $conn -Table [INBOX] -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Email -Connection $conn -Table [INBOX] -Columns @("From","Subject") -Values @($_.From, $_.Subject) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Email -Connection $conn -Table [INBOX] -Columns @("From","Subject") -Values @($_.From, $_.Subject)
  }
}

Copyright (c) 2024 CData Software, Inc. - All rights reserved.
Build 23.0.8839