Friday, January 12, 2018

powershell assisting with mails

ExtractAttachedMails.ps1 is used for extracted alla attached mails in a outlook .msg-file to specified folder.
CatalogueMails.ps1 is used for indexing the mails to more easily filter on timestamps and senders.

This came in handy when a happy user sent me 45 attached mails with the same subjectline so the extractionprocess also throws in an indexnumber to avoid namingconflicts.

$scriptPath = $(split-path $myinvocation.mycommand.definition)
$outputPath = "$($scriptPath)\extractedMails"
$mails = get-childitem -Filter *.msg -Path $outputPath
$object = @()
foreach ($mail in $mails) {
$outlook = new-object -ComObject Outlook.application
try { $msg = $outlook.session.openshareditem($mail.fullname)
$tempObject = new-object psobject -Property @{body=$msg.body;Topic=$msg.ConversationTopic;to=$msg.to;timestamp=$msg.SentOn }
$object += $tempObject
}
catch { write-output "Coulnd't open $($mail.fullname)"}
}
$object |sort-object timestamp -Descending |select timestamp,Topic,to
$object |sort-object timestamp -Descending |select timestamp,Topic,to |export-csv -Path "$($scriptpath)\output.csv" -NoTypeInformation -Encoding UTF8
write-output "Total count: $($mails.count)"
$scriptPath = $(split-path $myinvocation.mycommand.definition)
$outputPath = "$($scriptPath)\extractedMails"
$inputPath = "$($scriptPath)\inputMails"
$mails = get-childitem -Path $inputPath -Filter *.msg
if ($mails -ne $null) {
$index = 0
foreach ($mail in $mails) {
$outlook = new-object -ComObject Outlook.application
$msgFn = $mail.FullName
$msg = $outlook.CreateItemFromTemplate($msgFn)
foreach ($attachment in $msg.Attachments) {
if ($attachment.FileName -like "*.msg") {
$attFn = "$outputPath\$($index)_$($attachment.FileName)"
$attachment.SaveAsFile($attFn)
$index++
}
else {write-output "Skipping, not a msg ($($attachment.filename))"}
}
}
write-output "Total count: $($mails.count). Extracted $index attachements"
}
else { write-output "No mails found $inputPath"}

No comments:

Powershell and Uptimerobot

Uptimerobot can be quite tedious when you need to update many monitors at once. For example say you bought the license for Uptimerobot and n...