sivagndl
Enthusiast
Enthusiast

Need a script to schedule reboot VM

Jump to solution

Hi All

Need a script schedule reboot VM's .

We have 4 SSO servers , 8 inventory,8 web,8vcenter and 8 vum(updatemangager) servers avalible.

Need to reboot 1st week squence#1 (it follows 4 weeks) 1 SSO server ,2 inventory servers , 2 web,2 vcenter and 2 vum servers .

reboot the 1st SSO server after check the SSO service is running or not.

If SSO service is running, then reboot sequence  2 inventory, 2 web,2 vcenter and 2 vum after completed email myself a report squence .

If SSO service is not running need to reboot the SSO again  nofify through  mail.


Regards.

Siva



0 Kudos
1 Solution

Accepted Solutions
sivagndl
Enthusiast
Enthusiast

Thanks Jpsider,

I finally come up with below power shell Script.

Is there any short way to reach one. 

$serverslist = get-content 'C:\temp\Week1.txt'

$SSOlist=$Serverlist -like "*SSO*"## To pick the SSO in server list##

$list=$Serverlist -notlike "*SSO*"## To pick the not SSO in server list##

restart-computer -computername $SSOlist -force

Sleep 180

foreach($SSO in $SSOlist)

{

  FuncCheckService -ServiceName "VMwareIdentityMgmtService" -Server "$SSO"

}

function FuncCheckService

{

    param($ServiceName, $Server)

    $arrService = Get-Service -Name $ServiceName -ComputerName $Server

    if ($arrService.Status -ne "Running")

    {

        $Servicestatus= $arrservice|select machinename,DisplayName,status|sort machinename

  Send-MailMessage @sMail

  Exit

    }

    else

    {

  $Servicestatus=$arrservice|select machinename,DisplayName,status|sort machinename

  Send-MailMessage @sMail

    }

}

restart-computer -computername $list -force

Sleep 180

$arrservice= Get-Service -Name vimQueryService,vpxd -ComputerName $list|select machinename,DisplayName,status|sort machinename

Send-MailMessage @sMail

$Header = @"

<style>

BODY {background-color:             #a3dbe7;}

TABLE {border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

TH, TD {border-width:1px; padding:0px; border-style:solid; border-color:black;}

</style>

<title>

Title of my Report

</title>

"@

$sMail = @{

  From = "from mail id"

  To = "To mail id"

  Subject = "Weekly Reboot Status_"+(get-date).ToString("dd/MM/yyyy")

  BodyAsHtml = $true

  Body = $Servicestatus | ConvertTo-HTML -Head $Header -body "Week1"| Out-String

  SmtpServer = 'Smtp servername'

}

View solution in original post

0 Kudos
6 Replies
LucD
Leadership
Leadership

Can you use Windows Task Scheduler on a station to trigger these scripts?


A (free) alternative could be to create an appliance with PowerShell and PowerCLI Core, and use that scheduler (crontab).

But both of these are still alpha code, so not sure if you want to use that in production :smileygrin:


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
sivagndl
Enthusiast
Enthusiast

Thanks Lucd.

Can we get this task using Power shell ?

0 Kudos
jpsider
Expert
Expert

With powershell you can do anything!

You can both set up scheduled tasks, and that scheduled task can launch powershell scripts.

https://blogs.technet.microsoft.com/heyscriptingguy/2015/01/13/use-powershell-to-create-scheduled-ta...

0 Kudos
sivagndl
Enthusiast
Enthusiast

Thanks Jpsider
Can we get Power shell script for reboot the hosts and get the service state via Mail.

0 Kudos
jpsider
Expert
Expert

I can help you get started with some resources sure!

Here are two community links to different reboots:

Reboot a VM and wait until tools are running before rebooting the next vm

check vm power status, restart vm if tools installed else reset vm

Here is a quick link to get gmail working with Powershell:

http://www.adminarsenal.com/admin-arsenal-blog/powershell-send-mailmessage-gmail/

After reviewing I would start to draft your business logic to generate pseudo code, then come back and we can all chip in and help you wrap up any problems you have.

0 Kudos
sivagndl
Enthusiast
Enthusiast

Thanks Jpsider,

I finally come up with below power shell Script.

Is there any short way to reach one. 

$serverslist = get-content 'C:\temp\Week1.txt'

$SSOlist=$Serverlist -like "*SSO*"## To pick the SSO in server list##

$list=$Serverlist -notlike "*SSO*"## To pick the not SSO in server list##

restart-computer -computername $SSOlist -force

Sleep 180

foreach($SSO in $SSOlist)

{

  FuncCheckService -ServiceName "VMwareIdentityMgmtService" -Server "$SSO"

}

function FuncCheckService

{

    param($ServiceName, $Server)

    $arrService = Get-Service -Name $ServiceName -ComputerName $Server

    if ($arrService.Status -ne "Running")

    {

        $Servicestatus= $arrservice|select machinename,DisplayName,status|sort machinename

  Send-MailMessage @sMail

  Exit

    }

    else

    {

  $Servicestatus=$arrservice|select machinename,DisplayName,status|sort machinename

  Send-MailMessage @sMail

    }

}

restart-computer -computername $list -force

Sleep 180

$arrservice= Get-Service -Name vimQueryService,vpxd -ComputerName $list|select machinename,DisplayName,status|sort machinename

Send-MailMessage @sMail

$Header = @"

<style>

BODY {background-color:             #a3dbe7;}

TABLE {border-width:1px; border-style:solid; border-color:black; border-collapse:collapse;}

TH, TD {border-width:1px; padding:0px; border-style:solid; border-color:black;}

</style>

<title>

Title of my Report

</title>

"@

$sMail = @{

  From = "from mail id"

  To = "To mail id"

  Subject = "Weekly Reboot Status_"+(get-date).ToString("dd/MM/yyyy")

  BodyAsHtml = $true

  Body = $Servicestatus | ConvertTo-HTML -Head $Header -body "Week1"| Out-String

  SmtpServer = 'Smtp servername'

}

0 Kudos