0

I'm running this script in PowerShell ISE and it works great and it downloads the attachments to the folder I've specified.

But if I run it manually in PowerShell using PS C:\tmp> .\pdftofolder.ps1 it doesn't download the attachments and the log says this:

 **********************
Windows PowerShell transcript start
Start time: 20200517211912
Username: MON1\Administrator
RunAs User: MON1\Administrator
Configuration Name: 
Machine: MON1 (Microsoft Windows NT 10.0.17763.0)
Host Application: C:\windows\system32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 1708
PSVersion: 5.1.17763.1007
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.1007
BuildVersion: 10.0.17763.1007
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is C:\tmp\log.txt

GAC    Version        Location
---    -------        --------
False  v4.0.30319     C:\tmp\ImapX.dll
True
True
**********************
Windows PowerShell transcript end
End time: 20200517211912
**********************

And if I run it through the Task Scheduler (which is the ultimate goal) it just gets stuck. I run it with this command (its ran with highest privileges btw):

Scheduled Task

Start-Transcript -path C:\tmp\log.txt -append

$dll = 'C:\tmp\ImapX.dll'
[Reflection.Assembly]::LoadFile($dll)

$Username = "email@email.com"
$Password = "foo"

# Initialize the IMAP client
$client = New-Object ImapX.ImapClient

###set the fetching mode to retrieve the part of message you want to retrieve, 
###the less the better
$client.Behavior.MessageFetchMode = "Full"
$client.Host = "mail.foo.com"
$client.Port = 993
$client.UseSsl = $true
$client.Connect()
$client.Login($Username, $Password)

foreach($m in $messages){
    $m.Subject
        foreach($r in $m.Attachments){ 
                Write-Host $m.Attachments
                $r.Download()
                $r.Save('C:\tmp\dl')
                }
       }

Stop-Transcript

What part of running a PowerShell script made after its built in ISE have I missed? Thanks in advance.

Lars
  • 731
  • 1
  • 5
  • 4

1 Answers1

0

You've skipped

 $messages = $client.Folders.Inbox.Search("ALL", $client.Behavior.MessageFetchMode, 1000)
Bachtron
  • 1
  • 2