2

I wanted to set up a file that would ask for which branch's server is going down and then email them accordingly to alert them of the restart. I have 2 variables, $server and $setTime. How do I get the the powershell script to read translate those variables into text?

Powershell script:

$MyEmail = "[email protected]"
$SMTP= "mail.email.net"
$To = "[email protected]"
$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

Batch File:

@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
1
  • Thank you for showing your code. You're asking your question the right way. +1, and welcome to Stack Overflow! Commented Jun 15, 2016 at 18:36

2 Answers 2

1

Firstly, lose the spaces around the equal signs in your batch set /P commands. Should be

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

...etc.

To answer your question, within your PowerShell script, you can refer to these variables within the env scope -- $env:server and $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
"@
Sign up to request clarification or add additional context in comments.

Comments

1

Your batch file needs to look like this:

@echo off

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

PowerShell.exe "& 'c:\users\%username%\Desktop\dotComEmails\xServerIsDown.ps1' '%server%' '%setTime%'"

Please note that if you put a space before the = sign in the SET commands, it will break!

Then in the PowerShell script, you can assign those argument values to variables. Shortest, sweetest way is:

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.