在批处理脚本中调用 s PS1 脚本时使用 .bat 变量作为参数
我正在多台服务器上的批处理文件中运行报告,并且需要从调用 PS1 脚本的这些批处理文件发送电子邮件。当使用一组特定的收件人、主题和正文时,一切正常。问题是,我需要 PS1 脚本在 BAT 文件中的这 3 个参数在每个报告中发生变化时选择它们。我是一名 LINUX 负责人,所以这里的逻辑很困难,非常感谢任何帮助。谢谢
PS1 脚本...(所有 $ 变量都在 ps1 脚本中定义,$arg1,2,3 定义如下)
$arg1=$args[0]
$arg2=$args[1]
$arg3=$args[2]
Send-MailMessage -From $MailFrom -To $args1 -Subject $args2 -Body $arg3 -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential
批处理脚本
SET ROOT_DIR=\\FILEZ01.PATH.COM\INFO$\\OUTPUT\REPORTS
set FILE_NAME=%s_date%_MY_FILE.txt
set [email protected]
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\Tasks\scripts\SMTP.ps1' -To %REC_LIST% -Subject "ART Delta Patron test" -Body "%ROOT_DIR%\%FILE_NAME%""
I am running reports in batch files on several servers and I need to send emails from those batch files calling a PS1 script. When using a specific set of recipients, subject and body, it is working all fine. The issue is, I need the PS1 script to pick those 3 parameters from what they are in the BAT file as they change in every report. I am a LINUX head so struggling with the logic here and any help is highly appreciated. Thank you
PS1 script... (all the $ variables are defined in the ps1 script and the $arg1,2,3 are defined as below)
$arg1=$args[0]
$arg2=$args[1]
$arg3=$args[2]
Send-MailMessage -From $MailFrom -To $args1 -Subject $args2 -Body $arg3 -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential
Batch script
SET ROOT_DIR=\\FILEZ01.PATH.COM\INFO$\\OUTPUT\REPORTS
set FILE_NAME=%s_date%_MY_FILE.txt
set [email protected]
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\Tasks\scripts\SMTP.ps1' -To %REC_LIST% -Subject "ART Delta Patron test" -Body "%ROOT_DIR%\%FILE_NAME%""
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为了使命名参数起作用,请在脚本的第一行声明一个
param(...)
块:现在 PowerShell 将自动绑定传递给
-To
的任何参数到脚本内的$To
变量。由于所有参数在脚本和目标 cmdlet 中的命名相同,因此您可以通过 splatting
$PSBoundParameters
:In order to make named parameters work, declare a
param(...)
block on the first line of the script:Now PowerShell will automatically bind any argument passed to
-To
to the$To
variable inside the script.Since all the parameters are named the same in your script and the target cmdlet, you can pass them all along by splatting
$PSBoundParameters
:请改用 $Env:BatchVar。
我没有时间测试或调试它,但一般来说它应该看起来像这样:
批处理文件:
PowerShell:
Use $Env:BatchVar instead.
I don't have the time to test or debug this, but in general it should look something like this:
Batch file:
PowerShell: