VMware Cloud Community
edelldieguez
Contributor
Contributor

PowerCLI: Shutdown your Virtual Infrastructure II

Hi guys,

4 weeks ago i started a discussion in how how can I automatized the shutdown process in case of outage here is the link: http://communities.vmware.com/thread/322682?start=0&tstart=0 , that script worked perfect but this time i want to modified it a little, here what i need:

Well, due to an extended maintenance, we need to turn off all hosts and the SAN in where all VMs are stored, only one HOST will be ON and all critical VMs (+/- 10) will be move to it local datastore, this way we can get more time on the APCs and get the most of them.

10 hosts running - only one host will be on, here its where all powered vms (local host datastore) will be after the shutdown complete.
200 vms running - only +/-10 will be running.

Script # 1: Turn Off Script:

1- Import all vms (Names) from a VMnames.txt file and shut them down (Shutdown-VMGuest) one by one using VMware tools (all have the latest version installed), if after 3 minutes still trying just do a turn-off and start the next one.

2- Send confirmation email saying "Email Subject" that all VMs in the text provided are shutdown.

3- After all VMs from the text file are shutdown, vMotion all running (critical) VMs to a Host called "HOST1" (3 vms at a time),

4- When the vmotion is done, start a storage vmotion to a datastore called "datastore1" (which be a host local datastore, 3 vms at a time).

* These critical VMs will be running all the time.

5- Send a confirmation email saying "Email Subject" that all VMs were moved to a HOST1 and to the local datastore.

6- Put in maintenance mode the rest of the HOSTS called = "HOST2", HOST3, HOST4, until "HOST10"  (one by one)

7- When its done, shutdown all HOSTS that are in maintenance mode.

8- Send a confirmation email saying "Email Subject" that all hosts are shutdown


Script # 2: Turn On Script:


1- (All hosts will be manually powered on)

2- Take all hosts out of maintenance mode one by one.

3- When its done, Send confirmation email saying "Email Subject" that all host are connected

4- Import text file with the VMs names (used in the turn off script) and turn them ON one by one (3 minutes interval between each).

5- After all VMs are running, send a confirmation email saying that all VMs are powered ON.

If is possible, after completed a task and before send the confirmation email will be nice to run some check to be sure that the task is completed.

Its a lot to do but I really need help, also I think this will be good for all in the community if they have to deal with something similar.

Thanks in advance for all your help.

Edell.

0 Kudos
7 Replies
Cumulus
Enthusiast
Enthusiast

http://www.vmware.com/support/developer/PowerCLI/PowerCLI41U1/html/index.html

Post your results when you create it. This could be really useful to me as well.

0 Kudos
cdu
Enthusiast
Enthusiast

Interesting I'm using scripts to do maintainence this weekend too (completed in one datacenter 2 weeks ago, working on second datacenter). I'd break the script to mulitple parts, shutdown script, vmotion script and svmotion script and run each manually.

Here is what I did

1. get powered on VMs and export to csv file.

2. shutdown VMs in cvs file one by one

3. manually validate VM states, didn't use scripts

3. I didn't need to shutdown hosts, it's network maintainence.

4. reconfigure HA after maintainence

5. power on VMs in csv file one by one (wait 1 min before next one) (csv is prepared before outage to acommodate startup order for some apps)

6. validate

I do have another script for svmotion and I migrated 150 VMs in 2 days.

0 Kudos
edelldieguez
Contributor
Contributor

Hi cumulus and cdu,

yeah, i'm agree, this will be a good script that will help a lot of VMware guys out there.

cdu, I'm ok with few different scripts, for example:

steps 1 and 2 in one script, 4 and 5 in another script and then 6,7 and 8 in another script.

In my case i prefer this way because first, i can understand better the script and what it will do, for troubleshoot, in case something goes wrong is better to see 2 lines of code that not the entire script though, anyway any help you can provide me will be really appreciated.

cdu I'm curious, how was your maintenance?, everything went well?, yeah we will have a power outage for 8-10 hours, we need to turn off everything, the plan is to have only one HOST and few VMs running but if we need to turn them off we will do it.

Edell.

0 Kudos
edelldieguez
Contributor
Contributor

Hi Guys

Trying to figure out how can i do this i came out with these scripts to put in maintenance mode and shutdown the hosts from a csv file and Start all VMs from a csv file, any help will be appreciate it.

Number-1

####Shutdown Hosts#####


# Import the csv with the Host i want shutdown
$hostNames = Import-Csv "C:\HOSTS-Emergency-Shutdown.csv" -UseCulture | %{$_.hostName}

# Put Hosts in Maintenance Mode
Get-VMHost -Name $hostNames | where {$_.PowerState -eq "PoweredOn"} | Set-VMHost -State "Maintenance" -Confirm:$false -RunAsync

# Host Shutdown
Get-VMHost | where {$_.PowerState -eq "Maintenance"} | Stop-VMHost -Confirm:$false -RunAsync

or:

# Import the csv with the Host i want shutdown
$hostNames = Import-Csv "C:\HOSTS-Emergency-Shutdown.csv" -UseCulture | %{$_.hostName}


#Put Host in Maintenance Mode and then PowerOff
Get-VMHost -Name $hostNames | where {$_.PowerState -eq "PoweredOn"} | Set-VMHost -State "Maintenance" -Confirm:$false -RunAsync | Stop-VMhost -Confirm:$false -RunAsync

Number-2

#####Power ON Hosts#####

# Import the csv with the Host i want Power-ON
$hostNames = Import-Csv "C:\HOSTS-PowerON.csv" -UseCulture | %{$_.hostName}

# Put Hosts in Connected Mode
Get-VMHost -Name $hostNames | where {$_.PowerState -eq "Maintenance"} | Set-VMHost -State "Connected" -Confirm:$false -RunAsync

Number-3

#####Power ON VMs#####

# Import the csv with VMs i want Power ON
$StartVMs | Export-Csv "C:\Poweredon-vms.csv" -UseCulture
$StartVMs = Get-VM | Where {$_.PowerState -eq "poweredOff"}
$StartVMs | Start-VM -Confirm:$false -RunAsync

What do you think guys??, also let see if i can add an email confirmation to this !!!

Now i'm trying to figure it out the vmotion part and the svmotion part, i'm working on it !!!!

Edell.

0 Kudos
cdu
Enthusiast
Enthusiast

My sample script for svmotion. It will move VMs in one datastore to another.

Note: No VM snapshot.

$VC = "Your vCenter"
$OLDDS = "Your Source Datastore"
$NEWDS = "Your Destination Datastore"
write-host "Connecting to vCenter: " $VC
Connect-VIServer $VC
$vmlist = Get-Vm -datastore $OLDDS
foreach ($vm in $vmlist)
{
write-host "Moving " $vm " from "$OLDDS " to " $NEWDS
Move-Vm $vm -datastore $NEWDS
write-host "Finishded moving "$vm " from "$OLDDS " to "$NEWDS
}
Disconnect-VIServer -confirm:$false

0 Kudos
edelldieguez
Contributor
Contributor

Guys,

Here the script to vMotion VMs from a csv list:

# Import the csv with the VMs you want to vMotion

$vmNames = Import-Csv "C:\VM-vMotion.csv" -UseCulture | %{$_.vmName}
Get-VM -Name $vmNames | Move-VM -Destination #HOST-NAME# -Confirm:$false -RunAsync

Please any help will be appreciated.

Edell.

0 Kudos
edelldieguez
Contributor
Contributor

cdu:

Thank you very much for your help, i'll test it with one Test VM and let you know.

Edell.

0 Kudos