ESX PowerCLI script to activate a VM when a failover occurs

Posted on 2012-04-09
Last Modified: 2012-04-26
My customer has 2 Production DC sites & one DR site.

The production DC sites has 8 web servers (all on Win
2008) each.

The DR site currently has 8 VMs up & running, ready to
take over when any one of the Production DC site is
down.  However, the customer only have 8 Win 2008
licences for the 8 VMs in an ESXi host.

We're assuming that at any one time, only one of the
Production DC site is down.  The customer wants the
DR site to be able to provide full support (ie provide
8 full VMs to support whichever DC that was hit).

Anyone has any solution without buying more Win 2008
server licences for the DR site to meet the customer's

I thought of configuring 4 active VMs to support the 1st
Production site with another 4 'shutdown' or 'network-
disconnected' VMs for the 1st Production site.  Similarly
configure 4 active VMs (with another 4 'shutdown' VMs)
to support the 2nd Production site.   So in the event a
disaster strikes Production Site 1, a script in the ESXi
host will shut down the 4 active VMs for Site 2 &
bring up (or boot up) the 4 'shutdown' VMs for Site 1,
thus providing the full 8 Web servers to Site 1.
In this way, I will not be violating Windows 2008 server
licencing as I only have 8 VMs up at any one time.

Does anyone has any PowerCLI (for ESXi Ver 5) scripts
& commands to perform the above requirement?

Pls suggest a few good websites/links that give good
examples on ESXi automation (esp using scripts )
Question by:sunhux
  • 4
  • 3

Author Comment

ID: 37823650
Guess I'm looking for something mentioned in Point 6 in
the link / url below :

Also from the url below,
I'll need to clarify:
a) is this "Invoke-VMScript" a script & where can I download its codes?
b) can I run "Invoke-VMScript" to disable the NIC port (just like right-
    clicking the NIC icon on the bottom right bar of my desktop & select
    disable) to disable a VM that I want to stay 'shutdown' & to bring up
    a VM, invoke this script with parameter to enable it.  This is better
    than shutdown & booting up a VM (as this takes longer).  
    By disabling a NIC of a VM, can I say I've legally disabled the use
    of one Windows 2008 (Guest OS) licence?

Assisted Solution

un0ri earned 500 total points
ID: 37830480
Are the production boxes physical or virtual?  How are the sites interconnected? If they are all virtual with high speed links is High Availability not an option?

Deploying VMs from a template would not help (unless there was a different angle to step 6 you were thinking of?) as these would just deploy a brand new machine that would need configuring.

If you are wanting to change the state of a network port through vcenter you could use this

This would allow you to change the 'state' of the adapter to be connected or unconnected. (similar to plugging in or removing the network cable).  

There are also some 3rd party products that may allow better failovers.

In terms of the licensing although I would assume they would be happy as long as only one instance of a licensed server is available at any one time, you would really need to speak to your MS license supplier for confirmation that this would be abiding by Microsoft's EULA.

Author Comment

ID: 37837378
So if I want to 'deactivate' or disconnect VM3 & deallocate its CPU/RAM
resource, the PowerCLI codes would be:
    $vmnet = Get-VM VM3 | Get-NetworkAdapter | where {$_.Name -eq "Network Adapter 1"}
    Set-VM -vm VM3 -Numcpu 2 -Confirm:$false
    Set-VM -VM VM3 -MemoryMB 8192 -Confirm:$false
    $vmnet | Set-NetworkAdapter -NetworkName $VM.Network -Connected:$false
-StartConnected:$false -Confirm:$false

& if I want to 'activate' or connect VM9 & allocate its CPU/RAM
resource, the PowerCLI codes would be:
    $vmnet = Get-VM VM9 | Get-NetworkAdapter | where {$_.Name -eq "Network Adapter 1"}
    Set-VM -vm VM9 -Numcpu 2 -Confirm:$false
    Set-VM -VM VM9 -MemoryMB 8192 -Confirm:$false
    $vmnet | Set-NetworkAdapter -NetworkName $VM.Network -Connected:$true
-StartConnected:$true -Confirm:$false
Easy, flexible multimedia distribution & control

Coming soon!  Ideal for large-scale A/V applications, ATEN's VM3200 Modular Matrix Switch is an all-in-one solution that simplifies video wall integration. Easily customize display layouts to see what you want, how you want it in 4k.


Accepted Solution

un0ri earned 500 total points
ID: 37837593
You wouldn't want to be deallocating cpu and ram on a system that is turned on.  What would you be gaining by doing this anyway?

If you just wanted to disable a network adapter you could actually just do

connect-viserver <server-address>
get-networkadapter <server-name> | set-networkadapter -connected:$false -startconnected:$false -confirm:$false

the way you have written it would only return the value of get-network adapter, when you then tried to set it it would fail as there would be no vm to associate it with.

Starting it would just need -connected:$true and -startconnected:$true

Are you just talking about web servers?  Or domain controllers as well?

How would you be intending to synchronise this?  
If you bring an outdated web server online your site will be potentially be out of date.
If you brought a severely out of date DC into the mix there would be a number of problems.  If you are wanting to leave it in a 'prepared' state have a look at this.

Author Comment

ID: 37848099
>You wouldn't want to be deallocating cpu and ram on a system that is turned
>on.  What would you be gaining by doing this anyway?
By deallocating VMs that are disconnected, it would free up more CPU
& RAM for me to allocate to VMs that I'm connecting up, isn't it?  In
this way those VMs that are no longer 'active' will have 0 RAM & 0
CPU so that I can allocate more resource to the newly brought up

Btw, to enable the use of PowerCLI script (those  *.ps1 such as the
ones listed in the links below), what must I install?  PowerShell or
PowerCLI package?  & is this installed on the vCentre, vSphere,
ESX host itself or the individual VMs?  Pls provide me the links to
download these packages (for vSphere/vCentre 5 & ESXi 5)

Assisted Solution

un0ri earned 500 total points
ID: 37848186
No, you can over allocate as long as the machines are in a powered down state it doesn't matter how much you have allocated (although ESX5 licensing does care about allocations, this shouldn't kick in unless you were running a much bigger infrastructure).

If you are leaving this back-up systems in an powered down state I wouldn't bother disconnecting the networking either.  Just prep them with the correct IP's and shut them down.  Do remember you will need to synch them when you power them up to ensure they are up-to-date.  (one way to ensure they don't jump on accidentally is to have a separate private lan that is not routed outside of the DR infrastructure.  When it is ready to go live you can just switch the guest over to the correct network.)

Powershell is installed by default on all 2008, however the latest version can be downloaded from

Then youjust need to download and install the powercli snapin

This targets your Virtual Centre but does not need to be installed there.

Author Closing Comment

ID: 37897796

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If we need to check who deleted a Virtual Machine from our vCenter. Looking this task in logs can be painful and spend lot of time, so the best way to check this is in the vCenter DB. Just connect to vCenter DB(default DB should be VCDB and using…
In this article, I will show you HOW TO: Install VMware Tools for Windows on a VMware Windows virtual machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, using the VMware Host Client. The virtual machine has Windows Server 2016 instal…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video shows you how to use a vSphere client to connect to your ESX host as the root user. Demonstrates the basic connection of bypassing certification set up. Demonstrates how to access the traditional view to begin managing your virtual mac…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question