Shutdown / Restart via PowerShell

Hi  Readers,

Our team has to shutdown / Reboot a lot of servers due to some datacenter activities but unfortunately no one was available due to holiday season. So as I luv PowerShell,  the idea was to automate the process & send notification ones the action is completed.

image

You can download the solution from TechNet :-

http://gallery.technet.microsoft.com/Monitor-Shutdown-Restart-19ec13da

Don’t forget to rate.

Change the variables defined at the top of the script:

Enter the server names in the servers.txt file.

#####Declare variables & add logging###################

$email1 = “VikasSukhija@lab.com”
$from = “don_not_reply@lab.com”
$smtpserver =”smtp.lab.com”

$action = “Shutdown” ###########value should be Restart or Shutdown##############

————————————————————————————————————————————————————-

Here is the shell script:-

##################################################################################
#       Author:Prakash Yadav
#    Reviewer: Vikas Sukhija
#       Date: 12/20/2013
#       Description: Schedule Event Failed script.
##################################################################################

#####Declare variables & add logging###################

$email1 = “VikasSukhija@lab.com”
$from = “don_not_reply@lab.com”
$smtpserver =”smtp.lab.com”

$action = “Shutdown” ###########value should be Restart or Shutdown##############

$resserver=@()

#################Define Logs#############
$date1 = get-date -format d
# replace \ by –
$time = get-date -format t

$date1 = $date1.ToString().Replace(“/”, “-“)

$time = $time.ToString().Replace(“:”, “-“)
$time = $time.ToString().Replace(” “, “”)

$logs = “.\Logs” + “\” + “Powershell” + $date1 + “_” + $time + “_.txt”

Start-Transcript -Path $logs

#############################################

##Add server that needs to be restarted or shutdown

$server_list=get-content .\servers.txt
if($server_list -eq $null)

{
write-host “List is empty – script will quit”
exit

}

## Loop thru each server one by one

if ($action -like “Restart”)

{

foreach ($i in $server_list)
{

restart-Computer $i -Force

If(Test-Connection -ComputerName $i -Count 10 -Delay 5 -Quiet)
{

If(Test-Connection -ComputerName $i -count 2 -Delay 5 -Quiet)
{

$subject =”System $i is still responding – pls check”
Write-host “system $i stuck” -ForegroundColor blue
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}
else
{
$resserver += $i
$subject =”System restart started for $i”
Write-host “system $i restart process started” -ForegroundColor green
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}

}
}
timeout 600
write-host $resserver
foreach ($i in $resserver)
{
write-host “process $i”
If(Test-Connection -ComputerName $i -count 5 -Delay 5 -Quiet)

{

$subject =”System Restart for $i Successful”
Write-host “System Restart for $i Successful” -ForegroundColor green
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}
else
{
$subject =”System Restart for $i failed”
Write-host “System Restart for $i failed” -ForegroundColor blue
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}
}

}

if ($action -like “Shutdown”)
{

foreach ($i in $server_list)
{

Stop-Computer $i -Force

If(Test-Connection -ComputerName $i -Count 10 -Delay 5 -Quiet)
{

If(Test-Connection -ComputerName $i -count 2 -Delay 5 -Quiet)
{

$subject =”System Shutdown for $i Restart Failed – please check”
Write-host “System Shutdown for $i Restart Failed – please check” -ForegroundColor blue
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}
else
{

$subject =”System has been Shutdown for $i”
Write-host “System has been Shutdown for $i” -ForegroundColor green
$message = new-object Net.Mail.MailMessage
$smtp = new-object Net.Mail.SmtpClient($smtpserver)
$message.From = $from
$message.To.Add($email1)
$message.subject = $subject
$smtp.Send($message)
$message.dispose()
}

}
}
}

Stop-Transcript
##########################################################################################################

Regards

Sukhija Vikas

Advertisements

10 thoughts on “Shutdown / Restart via PowerShell

  1. I just have a question. you aren’t specifying any credentials. Is this because you are running this as user with Domain Administrator access?

  2. Thank you for this. I modified this with powershell -ExecutionPolicy ByPass -File script.ps1 in the batch file so that I did not have to change my execution policy. I may shorten the timeout and I will be reviewing if I need to remove the -force options for my specific usage, but this was a big help.

  3. Would a logon banner with an OK button stop this script from working? I am receiving “Stuck” messages on the servers I am not logged onto. If I log onto one, then run the script, the server reboots.

  4. Sir, I need a powersheel script to restart servers located at different locations. We have given different names to different servers. Please help me at the earliest.

  5. this script is not working on windows 2012 R2 , if i execute the restart computer command directly from the power shell window it is working perfect but while from the above script getting RPC error ..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s