VMware Cloud Community
LPLAdmin
Enthusiast
Enthusiast

Power On Power Off Script for List of VMs from CSV

Anyone have a good Power Off and Power on Script from a list of VM's in a CSV?  Possibly one that can monitor progress?

Thanks

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

Are you intending on stopping/starting those VMs in one call?
Or do you want to do this in batches?


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

Reply
0 Kudos
LPLAdmin
Enthusiast
Enthusiast

I could do it either way.  I plan on using it for a DR Test next month where I have a CSV for the VM's in SRM sorted by priority order.  DBs (1) APP(2) Web(3).  I'd run the shutdown.  Then execute the recovery plans.  After a clean up.  Run the power up on csv to bring up the DBs, App, Web vm's in that order.

Reply
0 Kudos
LucD
Leadership
Leadership

Does the CSV file contain the order in a column?
If the entries are sorted, how would a script know when 1 is finished and where 2 starts?


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

Reply
0 Kudos
Gidrakos
Hot Shot
Hot Shot

Quick and simple would be:

$csvFile = [Path To File].csv

if (!Test-Path $csvFile) {

     $csvData = Import-CSV $csvFile

     foreach ($line in $csvData) {

          # Where each $line is a VM name.

          Get-VM $line | Stop-VM -confirm:$false

     }

}

Stop-VM won't move on until it's complete, and will give you a progress bar on top automatically.

I usually throw in a lot more checks and balances in scripts like this, but that's your call.

Reply
0 Kudos
LPLAdmin
Enthusiast
Enthusiast

Can you run that with -RunAsync?  So it does more than one at a time?

Reply
0 Kudos
Gidrakos
Hot Shot
Hot Shot

Yup! Stop-VM takes the -RunAsync parameter.

You can make your life a little easier by first placing all the Get-Vm objects in an array and passing the array to Stop-VM like so:

$csvFile = [Path To File].csv

$vms = @()

if (!Test-Path $csvFile) {

     $csvData = Import-CSV $csvFile

     foreach ($line in $csvData) {

          # Where each $line is a VM name.

          $vms += (Get-VM $line)

     }

     $vms | Stop-VM -RunAsync:$true -confirm:$false

}

Reply
0 Kudos