Automation

 View Only
  • 1.  Power On Power Off Script for List of VMs from CSV

    Posted Sep 17, 2019 12:57 PM

    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



  • 2.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 17, 2019 01:20 PM

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



  • 3.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 17, 2019 04:09 PM

    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.



  • 4.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 18, 2019 05:00 AM

    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?



  • 5.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 18, 2019 04:43 PM

    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.



  • 6.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 19, 2019 01:16 PM

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



  • 7.  RE: Power On Power Off Script for List of VMs from CSV

    Posted Sep 20, 2019 12:09 PM

    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

    }