将输入变量从批处理文件发送到powershell脚本

时间:2016-06-15 16:53:18

标签: powershell batch-file

我想设置一个文件,询问哪个分支的服务器正在关闭,然后通过电子邮件发送它们以提醒他们重启。 我有2个变量,$ server和$ setTime。如何让powershell脚本阅读将这些变量翻译成文本?

Powershell脚本:

$MyEmail = "email@email.net"
$SMTP= "mail.email.net"
$To = "$server@email.net"
$Subject = "$server Server Restart"
$Body = "All,

We will be restarting the $server server at $setTime.
Printing and folder access wil be offline during the restart. 
The server will be offline for 5 minutes.

Helpdesk"

Start-Sleep 2

Send-MailMessage -To $to -From $MyEmail -Subject $Subject -Body $Body -SmtpServer $SMTP -DeliveryNotificationOption never

批处理文件:

@echo off

set /p server = What server is being restarted?
set /p setTime = What time?

PowerShell.exe -Command c:\users\%username%\Desktop\dotComEmails\xServerIsDown.ps1 -server

2 个答案:

答案 0 :(得分:1)

首先,在批set /P个命令中丢失等号周围的空格。应该是

set /P "server=What server is being restarted? "
set /P "setTime=What time? "

...等

要回答您的问题,您可以在PowerShell脚本中引用env范围内的这些变量 - $env:server$env:setTime

$Body = @"
All,

We will be restarting the $env:server server at $env:setTime.
Printing and folder access will be offline during the restart. 
The server will be offline for 5 minutes.

Helpdesk
"@

答案 1 :(得分:1)

您的批处理文件需要如下所示:

$server = $args[0]
$time = $args[1]

请注意如果你在SET命令中的=符号前放置一个空格,它就会中断!

然后在PowerShell脚本中,您可以将这些参数值分配给变量。最短,最甜蜜的方式是:

Form1