简体   繁体   English

使用批处理文件将“主题”和“正文”参数传递到Powershell电子邮件脚本

[英]Pass Subject and Body parameter to a powershell Email script using batch file

I have a powershell script , 我有一个powershell脚本,

[System.Net.ServicePointManager]::SecurityProtocol = 'TLS12'
$Mail_to="Jill.maclaurin@ca.ibm.com"
$Mail_from="shuddha.roy@gmail.com"
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = "smtp.sendgrid.com"

Send-MailMessage -SmtpServer $SMTPServer -Port 587 -UseSsl -From $Mail_from -To $Mail_to -Subject $Subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)

Now i want the $subject and $Body value to be obtained from a batch file and run the powershell script using that batch file, How to do it? 现在,我希望从批处理文件中获取$subject$Body值,并使用该批处理文件运行powershell脚本,该怎么做?

Add to the start of the script a Param Section, you can use default value for the required parameters, save the file. 在脚本的开头添加一个Param Section,可以将默认值用作必需参数,保存文件。

Param(
$Subject = "Test",
$Body = "Test Body"
)

[System.Net.ServicePointManager]::SecurityProtocol = 'TLS12'
$Mail_to="Jill.maclaurin@ca.ibm.com"
$Mail_from="shuddha.roy@gmail.com"
$SMTPServer = "smtp.sendgrid.com"

Send-MailMessage -SmtpServer $SMTPServer -Port 587 -UseSsl -From $Mail_from -To $Mail_to -Subject $Subject -BodyAsHtml $body -Encoding ([System.Text.Encoding]::UTF8)

Call the PS1 file from the batch file using the required paramters, eg: 使用所需的参数从批处理文件中调用PS1文件,例如:

powershell script.ps1 MySubject MyBody

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

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