简体   繁体   English

Powershell 脚本 - SendMail [详细错误]

[英]Powershell Script - SendMail [Detailed Errors]

I'm testing with a PowerShell script (where I'm testing copy and paste over FTPs using WinSCP), and in parallel I'm configuring the following code for sending mail in case of error on any of the put tasks, get....我正在使用 PowerShell 脚本进行测试(我正在使用 WinSCP 在 FTP 上测试复制和粘贴),同时我正在配置以下代码以在任何放置任务出错的情况下发送邮件,获取.. ..

This is the code :这是代码

 @echo off set WINSCP_LOG=C:\Users\Administrator\Desktop\WinSCP.log rem Run WinSCP script "C:\Program Files (x86)\WinSCP\WinSCP.com" ^ /log=%WINSCP_LOG% /ini=nul ^ /command ^ "open sftp://example:%%r%%21@he.example.es/ -hostkey=""ssh-rsa 2048 pRzlrJ27Zasd7"09¡^ "lcd C:\Users\Administrator\Desktop" ^ "cd /ftp-files/O1" ^ "put examplefile.txt" ^ "exit rem Check WinSCP result and prepare the email message if %ERRORLEVEL% equ 0 ( set WINSCP_SUBJECT=Success set WINSCP_MESSAGE=The files were uploaded successfully. set WINSCP_CODE=0 ) else ( set WINSCP_SUBJECT=Error set WINSCP_MESSAGE=Error uploading files, see attached log. set WINSCP_CODE=1 ) echo %WINSCP_SUBJECT% echo %WINSCP_MESSAGE% rem Send the email message set SMTP_FROM=no_reply@example.es set SMTP_TO=bztruster@example.com set SMTP_SERVER=smtp.office365.com set SMTP_USERNAME=no_reply@example.es set SMTP_PASSWORD=Asfaj23@# if exist "%WINSCP_LOG%" set ATTACHMENT=-Attachments '%WINSCP_LOG%' powershell -ExecutionPolicy Bypass Send-MailMessage ^ -From %SMTP_FROM% -To %SMTP_TO% -Subject '%WINSCP_SUBJECT%' -Body '%WINSCP_MESSAGE%' ^ %ATTACHMENT% -SmtpServer %SMTP_SERVER% -UseSsl ^ -Credential (New-Object System.Management.Automation.PSCredential ^ ('%SMTP_USERNAME%', (ConvertTo-SecureString '%SMTP_PASSWORD%' -AsPlainText -Force))) exit /b %WINSCP_CODE%

Where or how could I get more context on the errors in order to know the reason for the failure within the received mail?我可以在哪里或如何获得有关错误的更多上下文,以便了解收到的邮件中失败的原因?

Thanks for your help!谢谢你的帮助!

You are sending the wrong parameters to "powershell.exe" binary.您将错误的参数发送到“powershell.exe”二进制文件。 Run the following to understand the parameters accepted and the syntax needed:运行以下命令以了解接受的参数和所需的语法:

powershell.exe /?

For your particular needs, you can use the parameter "- command", eg:对于您的特殊需要,您可以使用参数“-命令”,例如:

powershell -ExecutionPolicy Bypass -Command {Send-MailMessage ^
-From %SMTP_FROM% -To %SMTP_TO% -Subject '%WINSCP_SUBJECT%' -Body '%WINSCP_MESSAGE%' ^
%ATTACHMENT% -SmtpServer %SMTP_SERVER% -UseSsl ^
-Credential (New-Object System.Management.Automation.PSCredential ^
    ('%SMTP_USERNAME%', (ConvertTo-SecureString '%SMTP_PASSWORD%' -AsPlainText -Force)))}

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

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