繁体   English   中英

如何手动或在任务计划程序中运行 PowerShell / ImapX 脚本?

[英]How to run a PowerShell / ImapX script manually or in Task Scheduler?

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

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

 **********************
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 脚本的哪一部分? 提前致谢。

你跳过了

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM