0

我在 PowerShell ISE 中运行此脚本,它运行良好,并将附件下载到我指定的文件夹中。

但是,如果我使用它在 PowerShell 中手动运行它, PS C:\tmp> .\pdftofolder.ps1 它不会下载附件,并且日志显示:

 **********************
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
**********************

如果我通过任务计划程序(这是最终目标)运行它,它就会卡住。我用这个命令运行它(顺便说一句,它以最高权限运行):

计划任务

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

我错过了在内置 ISE 之后运行 PowerShell 脚本的哪些部分?提前致谢。

4

1 回答 1

0

你跳过了

 $messages = $client.Folders.Inbox.Search("ALL", $client.Behavior.MessageFetchMode, 1000)
于 2021-02-23T03:36:55.417 回答