【问题标题】:How to run a PowerShell / ImapX script manually or in Task Scheduler?如何手动或在任务计划程序中运行 PowerShell / ImapX 脚本?
【发布时间】:2020-05-17 19:30:17
【问题描述】:

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

【问题讨论】:

  • 假设文件夹C:\tmp\dl存在,很有可能是NTFS权限或者其他一些与运行计划任务的用户账号相关的权限。
  • “以最高权限运行”肯定应该解决这个问题吗?
  • 这意味着帐户拥有的最高权限,而不是帐户可能拥有的最高权限。
  • 是管理员帐号

标签: windows powershell taskscheduler imapx


【解决方案1】:

你已经跳过了

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

【讨论】:

    猜你喜欢
    • 2015-07-22
    • 2020-08-07
    • 2015-07-27
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-23
    相关资源
    最近更新 更多