无法让 Exchange PowerShell 脚本在计划任务下运行

Log*_*gan 1 powershell exchange-2007 task-scheduler windows-sbs-2008 shell-scripting

我有一个 PowerShell 脚本,当直接输入到 PowerShell 中时它可以工作。PowerShell 程序使用快捷方式运行:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"
Run Code Online (Sandbox Code Playgroud)

我在 Windows 任务计划程序中尝试了大约 10 种不同的方法来首先打开上面的快捷方式,然后运行我保存的脚本文件,但它不起作用。它挂在“正在运行”上。

该脚本位于此处:

Z:\Exchange PowerShell\MailboxesApproachingOverQuota.ps1
Run Code Online (Sandbox Code Playgroud)

请注意,可以将脚本内容复制并粘贴到上面的 PowerShell 快捷方式中,它运行得非常好,并将输出通过电子邮件发送给我。

请注意,我使用的 PowerShell 实例来自上面显示的快捷方式,其中已经包含参数 - 因此我很难过在 Task Scheduler 中放置什么内容。

请问有什么帮助吗?

谢谢 :)


编辑当我直接在 CMD 中使用以下内容时:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"
Run Code Online (Sandbox Code Playgroud)

我得到:http : //tinypic.com/r/n1fkmg/8

当我使用以下内容时:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"
Run Code Online (Sandbox Code Playgroud)

我得到:http : //tinypic.com/r/5bbac9/8

运行这个:

powershell -PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -noexit -exec bypass -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'"
Run Code Online (Sandbox Code Playgroud)

打开 Exchange 命令行管理程序很好:http : //tinypic.com/r/2dglulf/8

Eva*_*son 5

我猜这个脚本没有签名。您是否使用Set-ExecutionPolicy -ExecutionPolicy Unrestrictedcmdlet启用了未签名脚本的执行?


Adi*_*tan 5

首先,让我告诉您如何从任务运行脚本:

Program/script: 
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments (optional): 
-PSConsoleFile "C:\Program Files\Microsoft\Exchange Server\bin\exshell.psc1" -command ". 'C:\Program Files\Microsoft\Exchange Server\bin\Exchange.ps1'; &'Z:\Exchange Powershell\MailboxesApproachingOverQuota.ps1'"
Run Code Online (Sandbox Code Playgroud)

另请注意,您用来运行脚本的用户 ID 可能需要在本地服务器上被授予权限。

更新:删除 -noexit