Task Scheduler wont run powershell script that data mines outlook

Welcome Forums General PowerShell Q&A Task Scheduler wont run powershell script that data mines outlook

This topic contains 5 replies, has 5 voices, and was last updated by

3 years, 4 months ago.

  • Author
  • #33371

    Topics: 4
    Replies: 3
    Points: 0
    Rank: Member

    In short, I have a script that I wrote that is supposed to go through my emails in an outlook folder and download any attachments within the last 24 hrs to a folder on my computer. This task works perfect when I run it from the ISE, command line, or via a batch file. However, when I use the windows task scheduler, no matter how I try to run it, it will say task complete, but the files are not downloaded. I have narrowed down my problem to not being able to access the loop which parses the folder items, but I do not know how to fix it.

    I am running powershell 2.0, windows 8.1 and outlook 2007. The task scheduler works fine for other tasks.

    code below:

    $olFolderInbox = 6
    $o = New-Object -comobject outlook.application
    $ns = $o.GetNameSpace("MAPI")
    $inbox = $ns.GetDefaultFolder($olFolderInbox)

    foreach($message in $inbox.Items) { #I cannot access this loop
    #some code to save the attachments

    Thank you in advance.

  • #33376

    Topics: 2
    Replies: 525
    Points: 24
    Team Member
    Rank: Member

    Hi Derek,

    My guesses are that either the user of your scheduled task does not have an Outlook profile configured or you might experience below Task Scheduler bug:



  • #33378

    Topics: 1
    Replies: 28
    Points: 0
    Rank: Member

    User logged on or not?

    what happens if you put this in front of the foreach?

    ($inbox.items | select -First 1).subject |out-file mailitems.txt

  • #33424

    Topics: 4
    Replies: 3
    Points: 0
    Rank: Member

    Dan P.,

    I have been trying to run the script with the checkbox ticked "run whether user is logged in or not". I have been actually logged in the entire time.

    I also input your line of code.

    "($inbox.items | select -First 1).subject |out-file mailitems.txt">

    Everything worked fine when I executed your script from the task scheduler. I also made the below changes, replacing the loop with an if statement, but had no luck accessing within the 'if statement'. Task scheduler doesn't seem to want to execute powershell loops or if statements.

    $message = ($inbox.items | select -First 1)
    $message.subject | out-file "C:\folder\mailitems.txt"
    if ( $message.Subject -eq "Update"){ 
      #this is the code in the loop

    Daniel K.,
    I looked into your method as well. I used the batch file script in the link you provided. It did not work either.


    Thanks again

  • #33438

    Topics: 17
    Replies: 573
    Points: 21
    Rank: Member

    This works for me in a scheduled task.

    task settings: Run only when user is logged on,
    actions program\script = powershell.exe
    actions arguments = full path to ps1.

    $mail = $inbox.items
    $dir = 'c:\temp\mailattachments\'
    foreach($message in $mail){
    $message.attachments|foreach { 
    #$_.filename | out-file c:\temp\attachements.txt -append
    $_.saveasfile((Join-Path $dir $_.filename)) 
  • #33449

    Topics: 29
    Replies: 125
    Points: 0
    Rank: Member

    If it's set to "run whether user is logged in or not" then it wont run in the current logged in user context meaning said context is either "system" or whatever account you specified.

    In both cases you need to make sure an outlook profile has been created for that context.

The topic ‘Task Scheduler wont run powershell script that data mines outlook’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort