Powershell无法通过批处理文件或Cmd提示或任务计划程序发送电子邮件

时间:2016-09-07 20:01:07

标签: windows email powershell cmd

过去5天我一直被困在同一个问题上。该脚本没有问题,并且直接从ISE执行时发送电子邮件没有任何问题。

@ECHO OFF C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy RemoteSigned -file D:\powershell\Fedex_Meter_Check1.ps1 PAUSE

也试过

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -noprofile -executionpolicy RemoteSigned -file D:\powershell\Fedex_Meter_Check1.ps1 

从powershell.exe或cmd执行但没有电子邮件时没有错误。请帮助我。

Send-MailMessage -from 'Trax_FedEx ittraxsupport@xpo.com' -to 'Rahul `Rahul.Lalwani@xpo.com'<#'TRAX ittraxsupport@xpo.com' #> -subject 'Daily Rates `Check for FedEx Meters.' -body 'Perfect! All FedEx Meters Have Good Rates!'` `-Encoding Unicode -port 25 -smtpserver mailhost.cnf.com

2 个答案:

答案 0 :(得分:0)

过去我在BAT文件中运行Powershell时遇到了很多问题。我记得的一件事是,处理启动目录存在奇怪的问题。从命令行,尝试将目录更改为与powershell脚本相同的目录,然后从那里运行它。我在BAT文件中有这个,只要.bat和.ps1共享相同的名称就可以工作。

@ECHO OFF
SET ThisScriptsDirectory=%~dpn0
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0'"
PAUSE

我在另一个旧脚本中找到的另一个解决方案是从.bat或命令行运行powershell.exe,并将.ps1作为命令提供给powershell而不是文件。

 powershell.exe -command "& {C:\filepath.ps1}"

答案 1 :(得分:0)

我找到了解决方案。只需添加Start-Sleep -Seconds 1作为最后一行,它就适用于我。