Automating Outlook E-mail


i trying automate e-mail messages moved after date.

this code works part, have run multiple times of messages moved.

for reason, works many messages , stops moving them. clue why?!

also, when messages moved, received date changes time @ message moved.

here code:

add-type -assemblyname microsoft.office.interop.outlook
$olfolders = "microsoft.office.interop.outlook.oldefaultfolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.getnamespace("mapi")
$inbox = $namespace.getdefaultfolder($olfolders::olfolderinbox)
$movetarget = $inbox.folders.item("scom alerts")
$onedayback = (get-date).adddays(-1)
$items = $inbox.items
$filter = "[receivedtime] > '#07/28/2010#'"
$inbox = $namespace.getdefaultfolder($olfolders::olfolderinbox).items.restrict($filter)
$inbox.count
foreach ($message in $inbox) {

if ($message.sendername -match "scom-prod") {
$message.sendername
[void]$message.move($movetarget) | out-null
}

}

adam;

you posted on scripting guys forum - posted this:

you cannot vote on own post
0

in foreach ($message in $inbox) { loop, move $message, , $inbox.count decriments.

you run problem of $inbox no longer having valid data.

say initial count 10 items.

the first time through, move item #1 of 10 - $inbox.count 9
second time through, move #2 of 10 - $inbox count 8
3 of 10 - $inbox count 7
4 of 10 - $inbox count 6
5 of 10 - $inbox count 5
6 of 10 fails, $inbox.count 5, not 6.

try this:

add-type -assemblyname microsoft.office.interop.outlook
$olfolders = "microsoft.office.interop.outlook.oldefaultfolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.getnamespace("mapi")
$inbox = $namespace.getdefaultfolder($olfolders::olfolderinbox)
$movetarget = $inbox.folders.item("powershell")
$onedayback = (get-date).adddays(-10)
$items = $inbox.items
$filter = "[receivedtime] > '#07/28/2010#'"
$inbox = $namespace.getdefaultfolder($olfolders::olfolderinbox).items.restrict($filter)
$inbox.count
$messages = $inbox.restrict("[from] = scom-prod")
($inc = $messages.count; $inc -gt 0 ; $inc --) {
write-output $messages.item($inc).subject
[void]$messages.item($inc).move($movetarget) | out-null
}


http://unlockpowershell.wordpress.com
-join("6b61726c6d69747363686b65406d742e6e6574"-split"(?<=\g.{2})",19|%{[char][int]"0x$_"})


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

Error: 0x80073701 when trying to add Print Services Role in Windows 2012 Standard

difference between wuauclt1.exe and wuauclt.exe

Windows 2016 RDS event 1306 Connection Broker Client failed to redirect the user... Error: NULL