Hi, I am automating delete from personal mailbox mails to be placed in 60 different folders and over weekend total number of email could be 10000+ initially I created function that get subfolder name from file and delete records in each folder one by one , delete might take a while.... so I modified script to use start-job in order to run delete in 60 folders in parallel ,( see script bellow runs fine and take about 1 minutes to delete 10000+ emails from 60 folders)$ScriptBlock ={ param([int] $OlderThenDays, [string] $Subfolder)$MailBox = "DBA@mMydomain.com"# Outlook Connection$Outlook = New-Object -ComObject Outlook.Application# Outlook folder to clean$EmailsInFolder = $Outlook.Session.Folders.Item($MailBox).Folders.Item($Subfolder).Items$Today = get-date$i=($EmailsInFolder.count-1)For($i; $i -ge 1; $i--){ $difference = New-TimeSpan -Start $($EmailsInFolder)[$i].SentOn -End $Today if ($difference.Days -ge $OlderThenDays) { $($EmailsInFolder)[$i].SentOn $($EmailsInFolder)[$i].Subject $($EmailsInFolder)[$i].delete() } }}$x = 0$data = Get-Content "U:\PS\LabOutput\Outlooksubfolders_test3.txt" # I already got list of subfolders foreach ($line in $data) { write-host $line Start-Job $ScriptBlock -ArgumentList $x , $line }[b]What I prefer to have: 1 function defined in module or file and call to this function in Start-JOB something like ... [/b]$x = 0$data = Get-Content "U:\PS\LabOutput\Outlooksubfolders_test3.txt" # I already got list of subfolders foreach ($line in $data) { write-host $line Start-Job -ScriptBlock {My-Function-With-Parameters-To-Delete-From-Subfolder -OlderThenDays $args[0] -$Subfolder $args[1]} -ArgumentList $x, $line }}
↧