Automation

 View Only
  • 1.  Power off / Power on script

    Posted Oct 08, 2010 09:52 PM

    I have maintence window coming up and I would like to see if it would be possbile to script this...

    I would like to shut down all powered on VM's in a cluster . If VM tools are not installed, power off the VM.

    In a second script, power on all VM's that were shut down or powered off from the previous script. I do not want to power on all VM's, only the ones that were running previously. I would also like to only power on 10 VM's at a time every 5 minutes (to avoid a boot storm and resource contention).

    - James



  • 2.  RE: Power off / Power on script

    Posted Oct 08, 2010 10:49 PM

    Let's start with the first script.

    You could do something like this

    $clusterName = <clustername>
    $vms = Get-Cluster -Name $clusterName | Get-VM | where {$_.PowerState -eq "PoweredOn"}
    $vms | where {$_.Guest.State -eq "NotRunning"} | Stop-VM -Confirm:$false
    $vms | where {$_.Guest.State -eq "Running"} | Shutdown-VMGuest -Confirm:$false
    $vms | Select Name | Export-Csv "C:\Temp\cluster-vm-stopped.csv" -NoTypeInformation
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 3.  RE: Power off / Power on script

    Posted Oct 14, 2010 01:55 PM

    Guys,

    Thanks for the example you show how to shutdown a cluster per cluster if you specify the cluster.

    I'd like to modify the script to shutdown per datastore. But instead of shutting down a VM at a time. I'd like to shutdown 4 at a time.

    When I use for-each it would do one at a time. Any suggestions or examples would be great.



  • 4.  RE: Power off / Power on script
    Best Answer

    Posted Oct 08, 2010 11:01 PM

    For the 2nd script we use the .csv file from the 1st script.

    Give this a try.

    $start = Get-Date
    $names = Import-Csv "C:\Temp\cluster-vm-stopped.csv"
    $total = 0
    while($total -le $names.Count){
      1..10 | %{
        if($names[$total]){
          Start-VM -VM (Get-VM -Name $names[$total]) -Confirm:$false -RunAsync
          $total++
        }
      }
      while((Get-Date - $start).TotalMinutes -lt 5){
        sleep 10
      }
      $start = Get-Date
    }
    

    ____________

    Blog: LucD notes

    Twitter: lucd22



  • 5.  RE: Power off / Power on script

    Posted Oct 08, 2010 11:09 PM

    Another sample :smileyhappy:

    # Script 1 - Shut down VMs in cluster
    
    $vms = Get-Cluster cluster02 | Get-VM 
    
    $vmstools = $vms | Where { $_.PowerState -eq "PoweredOn"} | Get-View | Where { $_.GuestHeartbeatStatus -ne "gray"}
    $vmsnotools = $vms |  Where { $_.PowerState -eq "PoweredOn"} | Get-View | Where { $_.GuestHeartbeatStatus -eq "gray"}
                
    $vms | Select name | Export-CSV c:\tmp\delete\list.csv -NoTypeInformation 
    
    $vmtools |  Shutdown-VMGuest -Confirm:false
    $vmnotools | Stop-VM -Confirm:false
    
    # Add code to check if all in $vmtools were powered down
    
    # Script 2 - start previously powered off VMs
     
    $i = 1
    $vms_on = Import-CSV c:\tmp\delete\list.csv
    ForEach ($vm in $vms_on) {
                   Start-VM -VM $vm.name -whatif
                   If (($i%10) -eq 0) {
                                        Write-host "Started " $i " VMs.  Taking a break."
                                        Sleep 300
                                        }
                    $i = $i + 1
                    }
    



  • 6.  RE: Power off / Power on script

    Posted Oct 15, 2010 03:29 PM

    You guys have help me before with my shutdown script which I have got from you guys anyway, I am looking to better it.

    So this is what I would like to do but I can't becasue I don't know how to. I took this script from an upgrade vmware tools and instead would like to use some of its context to ShutDown VM's from a list.txt file, check to make sure they are down create a log file that they are down then once they are all down send an email.

    I know I have to add probably a Sleep to wait for each one in here as well.

    I really appreciate your help.

    1. Get the VM's from a text file -

    $colVMs = Get-Content "C:\list.txt" | % { Get-VM $_ | Shutdown-VMGuest -Confirm:$false }

    2. Start the Shutdown process -

    3. Create a log file -

    $logFile = New-Item -ItemType File -Path "C:\Powershell\temp\power\VMware-PowerOff_$((get-date).toString('MM-dd-yyyy_hh.mmtt')).log"

    4. Get the information on each machine PowerState -

    foreach ($VM in $colVMs) {

    $VMPower = $VM.PowerState

    $VMName = $VM.name

    $OS = (Get-VMGuest $VM)

    $vmID = get-vm $VM | foreach {get-view $_.Id}

    5. Check that the VMs are off -

    if ($VMPower -notmatch "PoweredON") {

    Write-Host $VMName `t "Powered OFF" -ForegroundColor Red

    Add-Content -Path $logFile -Value ($VMName + "`t`tShutDown`t`tPowered OFF")

    }

    6. Send out an email with a log letting me know that they have ShutDown

    send-mailmessage -SmtpServer "smtp.com" `

    -from "VM-ShutDown Thank you,

    Express



  • 7.  RE: Power off / Power on script

    Posted Oct 15, 2010 06:15 PM

    I guess I should have posted as a new Post since this one showed Answered?






    Thank you,

    Express