Thursday, 2 June 2016

How to reboot multiple Virtual Machines on an ESXi host

Recently I got a request to schedule the reboot of multiple VMs on an ESXi host on every weekend.

Let me explain the scenario first. 

1. We need to schedule the reboot of all 50 VMs on every Saturday at 10.00PM (all those machines are Windows servers)

2. Both ESXi and vCenter are version 5.5

3. PowerCLI is installed on vCenter server.



Step-by-step Instructions


1. RDP to your vCenter server (if you are using vCenter server appliance you need to make changes on the below steps accordingly)

2. Make sure that you have enabled access for your domain user (domain administrator)\local server administrator so that they can access vCenter. If you haven't done this please follow https://virtuallylg.wordpress.com/2013/09/29/vsphere-5-5-how-to-add-domain-users-to-sso/

3. Open a notepad and copy paste the below script, save the file as 'VMReboot.ps1'

param(
    [parameter(Mandatory = $true)]
    [string[]]$vCenter,
    [parameter(Mandatory = $true)]
    [string[]]$vmName
)

$VIServer = Connect-VIServer $vCenter
If ($VIServer.IsConnected -ne $true){
    Write-Host "error connecting to $vCenter" -ForegroundColor Red
    exit
}

foreach($vm in $vmName){
        Write-Host "Going to restart $vm"
        Restart-VMGuest -VM (Get-VM $vm) -Confirm:$false
}

Disconnect-VIServer -Confirm:$false


4. Open PowerShell and test the script first (see below screenshot).


You need to mention your vCenter server and name of VMs you need to reboot. If you have multiple VMs separate the names with comma or if all VMs start with a common prefix use wild card (eg. "TestVM*")

See the VMs you have mentioned are getting rebooted. If the script works then schedule this job using Windows Task Scheduler

5. Open Task Scheduler and Create a basic task 

6. Mention your schedule and on Actions > Start a Program

Program/script : C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

Add arguments-PSConsoleFile "C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" " &  "C:\VMReboot.ps1" -vCenter VCENTER-SRV -vmName "TestVM*"


Verify if the scheduled task is working by running it.

Enable Task History to make sure that the scheduled task is working properly



All the best!

7 comments:

  1. Thanks for the informative article. This is one of the best resources I have found in quite some time. Nicely written and great info. I really cannot thank you enough for sharing.

    Herbalife in Chennai
    Wellnesscentres in Chennai
    Weight loss in Chennai
    Weight gain in Chennai

    ReplyDelete
  2. Your site is amazing and your blogs are informative and knowledgeable to my websites.This is one of the best tips in my life.I have in quite some time.Nicely written and great info.Thanks to share the more information's.

    Seo Experts
    Seo Company
    Web Designing Company
    Digital Marketing
    Web Development Company
    App Development

    ReplyDelete
  3. Testers can build, enhance, and maintain scripts to regression test their mobile applications. Hands-on instruction is provided for those who want to explore the power of using Appium. The course covers content from installation to execution and reporting . The focus is on the practical application of Appium to resolve common mobile automated testing challenges. This course focuses on getting started with Appium.
    course/appiumtraininginsholinganallur/

    ReplyDelete
  4. As you have now understood the usage of ‘Record and Playback’ tool, the following are the different posts using which you can explore the functioning of ‘Selenium IDE’
    selenium Training in chennai

    ReplyDelete
  5. All the latest updates from the Python Automationminds team. Python Automationminds lets you program in Python, in your browser. No need to install any software, just start coding straight away. There's a fully-functional web-based console and a programmer's text-editor
    Phyton training in Chennai

    ReplyDelete
  6. Its very impressive post please continue posting like this and if you are interested please find or refer the links to get updates relevant to interior designs and approaches from:
    Leading Architects in India
    Turnkey Interior Contractors in Chennai
    Architecture Firms in Chennai
    Warehouse Architect
    Factory Architect Chennai
    Office Interiors in Chennai
    Rainwater Harvesting chennai
    Turnkey Interior Contractors in Chennai

    ReplyDelete