Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- CLEAR-HISTORY
- CLS
- $sSearchSubject = 'Error' #needs to work for multiple Subjects such as Incident, Error, Service request, etc.
- $sScope = 'Inbox'
- $oOutlook = New-Object -ComObject Outlook.Application
- $oNameSpace = $oOutlook.GetNamespace('MAPI')
- $oStore = $oNameSpace.Stores['john.doe@contoso.com']
- $oInbox = $oStore.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
- $oRootFolder = $oStore.GetRootFolder()
- $oTargetFolder = $oRootFolder.Folders['Drafts']
- $sUnreadInboxMail = $oInbox.Items.Restrict('[UnRead] = True')
- $iInboxMailCount = $oInbox.Items.Restrict('[UnRead] = True').Count
- Write-Host ("The Count of unRead Mails with the Search Character as ""$sSearchSubject"" is $iInboxMailCount") -ForegroundColor Green
- $sUnreadInboxMail | Select Subject, ReceivedTime
- #This piece does not work though I know this is the way the coding should be done
- #Explanation: - If there are 5 mails with the Subject containing Error NONE get moved.
- #I bet I am doing something SILLY but not able to figure it out
- For($iMailCount=($oInbox.count-1);$iMailCount -ge 0;$iMailCount--)
- {
- If ($_.subject -Like "*$sSearchSubject*")
- {
- $($oInbox)[$iMailCount].move($oTargetFolder)
- }
- }
- #This Piece of Code only moves Certain Mails
- #Explanation: - If there are 5 mails with the Subject containing Error only 3 get moved and the rest stay in the Inbox
- $oInbox.Items.Restrict('[UnRead] = True') |
- ForEach-Object {
- If ($_.subject -Like "*$sSearchSubject*") {
- $_.Move($oTargetFolder)
- }
- }
Add Comment
Please, Sign In to add comment