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
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
- 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.
- 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\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\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\"
Example 5: Save eMail (eml) by it's subject
$eml = Get-Variable -Name emlfile -ValueOnly
$subject = Get-Variable -Name subject -ValueOnly
$emlfile = Get-ChildItem $eml
[System.IO.Path]::GetinvalidFileNameChars() | ForEach-Object {$subject = $subject.Replace($_," ")}
$newfilename = $emlfile.DirectoryName + '\' + $subject +'.eml'
move $eml $newfilename
Example 6: Forward eMails
Import-Module -Name 'C:\Program Files\Somebytes\EEAttachments\Microsoft.Exchange.WebServices.dll'
$item = Get-Variable -Name mailitem -ValueOnly
$item.Forward("", "email@somewhere.com");
Example 7: Rename attachment
$file = Get-Variable -Name file -ValueOnly
$subject = Get-Variable -Name subject -ValueOnly
$fileobj = Get-ChildItem $file
[System.IO.Path]::GetinvalidFileNameChars() | ForEach-Object {$subject = $subject.Replace($_," ")}
$newfilename = $fileobj.DirectoryName + '\' + $subject + "."+ $fileobj.Extension
$counter = 0
while(Test-Path -Path $newfilename)
{
$counter++;$newfilename = $fileobj.DirectoryName + '\' + $subject + '('+$counter.ToString()+').'+ $fileobj.Extension
}
move $file $newfilename
Example 8: Create CSV and attach some properties to it
In this script I append the fields Subject and From of the processed message to a CSV file. By the way, ExMixedFolders is also able to create CSV file from an Exchange folder via "Import Export Task".
$outfile = "C:\temp\Outfile.csv"
$newcsv = {} | Select "Subject","From"
$subject = Get-Variable -Name subject -ValueOnly
$from = Get-Variable -Name from -ValueOnly
$newcsv.Subject =$subject
$newcsv.From = $from.Address
$newcsv | Export-CSV $outfile –Append -NoTypeInformation
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.