Microsoft Exchange




 

 

SO today was hectic day for me. It started off as regular day where i get to talk to colleagues and work through the usual daily problems. I cannot say much about the end of the day. It was just the opposite where i was battling to finish all my tasks that had suddenly skyrocketed. All of them very critical. But this one task caught my attention and deserves a write up. I have never written powershell script to handle outlook emails. In this case, i needed to process a folder with close to 300,000 items and move or copy them to another folder. Where to start, anybodys guess GOOGLE. lets begin...

 Iam no powershell guru, but I manage to leverage a script developed by Vikas Sukhija. In the end, the resulting script was simple and straight to the point. Nothing fancy. The script opens an outlook session and accesses a given folder and loops through moving items fitting my conditions to another folder. For this powershell script to run, Outlook must already be running.

 

$date = get-date -format d
$date = $date.ToString().Replace(“/”, “-”)
$time = get-date -format t
$time = $time.ToString().Replace(":", "-")
$time = $time.ToString().Replace(" ", "")

$log1 = ".\Logs" + "\" + "Processed_1" + $date + "_.log"
$log2 = ".\Logs" + "\" + "Processed_2" + $date + "_.log"


$logs = ".\Logs" + "\" + "Powershell" + $date + "_" + $time + "_.txt"

$smtpServer = "smtpserver"
$from = "This email address is being protected from spambots. You need JavaScript enabled to view it."
$to = "adminsThis email address is being protected from spambots. You need JavaScript enabled to view it."

#############################################################################


#Start-Transcript -Path $logs

$mbx = "This email address is being protected from spambots. You need JavaScript enabled to view it."
$fold = "Process"
$tfold = "Target"

Write-host "Mailbox is $mbx" -foregroundcolor Green
Write-host "Working folder $fold" -foregroundcolor Green
Write-host "Target folder $tfold" -foregroundcolor Green

$date1 = get-date
add-content $log2 "$date1 - Script Started"
#############################outlook Call#############################################

$outlook = new-object -com outlook.application;
$ns = $outlook.GetNameSpace("MAPI");
$inbox = $ns.Folders.Item($mbx).folders.Item($fold)
$MoveTarget = $ns.Folders.Item($mbx).folders.Item($tfold)

$messages = $inbox.items
$totalemail = $messages.count
$date1 = get-date
$progress =0

$Detail = @()

add-content $log2 "$date1 - Total Messages to process $totalemail"
write-host "Total Messages to process $totalemail"  -foregroundcolor green

$Detail += @("Sender,recipient, rcvdate,subject")


#########################Find messages where sender is more than 25 Characters########################

foreach($message in $messages){
$error.clear()
$progress = $progress +1
$msender = $message.senderEmailAddress
$senderlen= $msender.length
$msubject = $message.subject
$mdateRcv = $message.ReceivedTime
$Receiver = $message.to
$ReceiverCC = $message.CC

write-host "Processing Message # $progress"  -foregroundcolor yellow
write-host $mdatercv $Receiver -foregroundcolor blue
write-host $msubject -foregroundcolor blue

if($senderlen -gt 25)
 {
          write-host $msender "is $senderlen Exceeds 25 Character Limit" -foregroundcolor cyan  
          $Detail += @("'$mSender','$Receiver', '$mdatercv','$msubject'")     
   #[void]$message.move($MoveTarget)
          $count = $count +1
 } 

if($error) {
write-host "Error somehwere"

}

 

Who has been here?