在批处理脚本中调用 s PS1 脚本时使用 .bat 变量作为参数

发布于 2025-01-11 10:38:40 字数 894 浏览 0 评论 0原文

我正在多台服务器上的批处理文件中运行报告,并且需要从调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

王权女流氓 2025-01-18 10:38:40

为了使命名参数起作用,请在脚本的第一行声明一个 param(...) 块:

param($To,$Subject,$Body)

现在 PowerShell 将自动绑定传递给 -To 的任何参数到脚本内的 $To 变量。

由于所有参数在脚本和目标 cmdlet 中的命名相同,因此您可以通过 splatting $PSBoundParameters

Send-MailMessage -From $MailFrom @PSBoundParameters -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential

In order to make named parameters work, declare a param(...) block on the first line of the script:

param($To,$Subject,$Body)

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:

Send-MailMessage -From $MailFrom @PSBoundParameters -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential
毁我热情 2025-01-18 10:38:40

请改用 $Env:BatchVar。

我没有时间测试或调试它,但一般来说它应该看起来像这样:

批处理文件:

SET ROOT_DIR=\\FILEZ01.PATH.COM\INFO$\\OUTPUT\REPORTS
set FILE_NAME=%s_date%_MY_FILE.txt
SET [email protected]
SET Subject=ART Delta Patron test
SET Body=%ROOT_DIR%\%FILE_NAME%
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\Tasks\scripts\SMTP.ps1' "

PowerShell:

Send-MailMessage -From $MailFrom -To $Env:To -Subject $Env:Subject -Body $Env:Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential

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:

SET ROOT_DIR=\\FILEZ01.PATH.COM\INFO$\\OUTPUT\REPORTS
set FILE_NAME=%s_date%_MY_FILE.txt
SET [email protected]
SET Subject=ART Delta Patron test
SET Body=%ROOT_DIR%\%FILE_NAME%
powershell.exe -ExecutionPolicy Unrestricted -Command ". 'C:\Tasks\scripts\SMTP.ps1' "

PowerShell:

Send-MailMessage -From $MailFrom -To $Env:To -Subject $Env:Subject -Body $Env:Body -SmtpServer $SMTPServer -Port $SMTPPort -UseSsl -Credential $MailCredential

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文