Tuesday, December 24, 2013

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

12 comments:

  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?

    ReplyDelete
  2. I am running it from account that has admin rights on the remote machines.

    ReplyDelete
  3. 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.

    ReplyDelete
  4. ed.piasecki@sympleday.comFebruary 26, 2017 at 4:52 PM

    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.

    ReplyDelete
  5. ed.piasecki@sympleday.comFebruary 26, 2017 at 6:18 PM

    Found this if it helps http://stackoverflow.com/questions/17849522/how-to-perform-keystroke-inside-powershell

    ReplyDelete
  6. will check but never faced an issue, as we don't have interactive logons..

    ReplyDelete
  7. 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.

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

    ReplyDelete
  9. it takes input from text file so whats the issue ?

    ReplyDelete
  10. 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 ..

    ReplyDelete
  11. I want to send a final email after restarting various machines. The email should contain the list of machines which have successfully restarted.

    Currently, this script is sending individual mail.

    Can you please provide some help for this requirement?
    Regards

    ReplyDelete
  12. This will need code modification, collect the machine names in array and than finally send it after script completion.

    ReplyDelete