Somebytes SoftwareSomebytes Software Development

PowerShell mit EEAttachments

So far, the PowerShell could be used with EEAttachments, but the powershell.exe had to be started explicitly as shown here. Since Version 1.20, you can now directly specify a PowerShell script in the "Run" line

Use PowerShell from EEAttachments

If you enter a PowerShell script in the start fields of the export job, this is now executed directly in EEAttachments. Messages such as errors and information can be displayed directly in the EEAttachments log. Furthermore, arguments can no longer be passed as text, but also as objects, because session variables with the corresponding information are generated.

Variables at "Run for attachment" :

  • filename: filename without path
  • file: filename with path
  • cmdLineArgs: Commandlineargs as String[]
  • mailitem: The mailitem from EWS (Microsoft.Exchange.WebServices.Data.Item) You can access all properties in the PS-Script and modify the mail as well.
  • And other properties specified in the arguments field from the EWS EMailSchema (Microsoft.Exchange.WebServices.Data.EmailMessageSchema).


Variables at "Run for content":

  • bodyfile: File name of the mail bodyfield which has been exported as HTML.
  • xmlfile: Filename of the exported properties as XML.
  • cmdLineArgs: Commandlineargs as String[]
  • mailitem: The mailitem from EWS (Microsoft.Exchange.WebServices.Data.Item) You can access all properties in the PS-Script and modify the mail as well.
  • And other properties specified in the arguments field from the EWS EMailMessageSchema (Microsoft.Exchange.WebServices.Data.EmailMessageSchema).


Example 1: This can display the variables generated by EEAttachments in its log

foreach ($v in Get-Variable |where {$_.Description -eq 'EEAttachments'})
{
     Write-Information "$($v |select Name) $($v |select Value)"
}

Example 2: Direct access to the EMailMessage object

This script loads the module "Managed EWS" into the PowerShell-Session and accesses the subject and body properties. By "Write-Information" it can display the output on the EEAttachments log. If there are errors in the script, you can see it on the log as well.

Import-Module -Name 'C:\Program Files (x86)\Somebytes\EEAttachments\Microsoft.Exchange.WebServices.dll'
$item = Get-Variable -Name mailitem -ValueOnly
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)  
Write-Information $item.Subject
$item.Load($psPropertySet)
Write-Information $item.Body.Text

Example 3: Modify the flagstatus of the Mail

Here you can learn how to change the flagstatus to "Complete" after the mail has been processed.

Import-Module -Name 'C:\Program Files (x86)\Somebytes\EEAttachments\Microsoft.Exchange.WebServices.dll'
$item = Get-Variable -Name mailitem -ValueOnly
$psPropertySet = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.ItemSchema]::Flag)  
$item.Load($psPropertySet)
$item.Flag.FlagStatus = [Microsoft.Exchange.WebServices.Data.ItemFlagStatus]::Complete;
$item.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AutoResolve);     


Example 4: Unzip attachments after the mail has been exported

Add-Type -AssemblyName System.IO.Compression.FileSystem
function Unzip
{
   param([string]$zipfile, [string]$outpath)
   [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}
$attachment = Get-Variable -Name file -ValueOnly
Unzip $attachment  "C:\temp\"

hide thisPlease keep the PowerShell scripts on a location where an unauthorized person can't modify it. Since the item object contains the credentials (with the password for the current connection), which could then be stolen in this case.