Problem with script only works in ISE not in powershell command line

I have a script below that I am having some trouble with it seems to work in the ISE but not when I right click then hit run in powershell. I have tried using a runas statement before to run powershell in admin mode but that did not work either.

$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(MAPI)
$a = $f.Items
$b = $a.Count

#you'll get a popup in outlook at this point where you pick the folder you want to scan
$f = $n.pickfolder()

[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$form = new-object Windows.Forms.Form

$objShell = new-object -com shell.application
$d=$objShell.BrowseForFolder(0,"Title",0,"c:\")
$c= $d.self
$path=$c.path

#now loop through them and grab the attachments for the last 30 days for selected folder then save
foreach ($item in $a)
    { foreach ($attach in $item.Attachments)
            {  if ($item.ReceivedTime -gt (get-date).AddDays(-30))
            {if ($attach.filename.contains("pdf")){ $attach.saveasfile((Join-Path $path $attach.filename))
            }  
   elseif  ($attach.filename.contains("docx"))
              { $attach.saveasfile((Join-Path $path $attach.filename))
            }   
        }
   }
}


July 24th, 2013 4:03pm

Total guess, but maybe STA vs MTA issue:

http://depsharee.blogspot.com/2011/06/powershell-sta-and-mta.html

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 4:57pm

Ran a crossed that when I google'd the issue doesn't work if I try -sta either or -noexit or combo of both
July 24th, 2013 5:02pm

$o = New-Object -comobject outlook.application
$n = $o.GetNamespace(MAPI)
$a = $f.Items
$b = $a.Count

#you'll get a popup in outlook at this point where you pick the folder you want to scan
$f = $n.pickfolder()

$objShell = new-object -com shell.application
$d=$objShell.BrowseForFolder(0,"Title",0,"\\thoth\shared\reception")
$c= $d.self
$path=$c.path

#now loop through them and grab the attachments
foreach ($item in $a)
    { foreach ($attach in $item.Attachments)
            {  if ($item.ReceivedTime -gt (get-date).AddDays(-30))
            {if ($attach.filename.contains("pdf")){ $attach.saveasfile((Join-Path $path $attach.filename))
            }  
   elseif  ($attach.filename.contains("docx"))
              { $attach.saveasfile((Join-Path $path $attach.filename))
            }   
        }
   }
}

Updated script also I tried powershell.exe -sta C:\users\cb\email.ps1 it did not work

Free Windows Admin Tool Kit Click here and download it now
July 24th, 2013 7:43pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics