Automation

 View Only
  • 1.  graceful shutdown of vms if vm tools installed

    Posted Jun 21, 2014 07:54 AM

    Hi

    Is there any script to shutdown vms gracefully where vm tools installed if not stop them forcefully and wait until all vms shutdown and export this output.



  • 2.  RE: graceful shutdown of vms if vm tools installed

    Posted Jun 21, 2014 09:02 AM

    Try like this

    Get-VM | %{

         if($_.ExtensionData.GuestInfo.ToolsRunningStatus -ne "GuestToolsNotRunning"){

            Stop-VM -VM $_ -Confirm:$false

        }

        else{

            Shutdown-VMGuest -VM $_ -Confirm:$false

       }

    }



  • 3.  RE: graceful shutdown of vms if vm tools installed

    Posted Jun 21, 2014 09:09 AM

    Thanks LucD..can I export like this after completing power off..

    |export-csv -path pwoff.csv..

    and one thing I already kept these all vms in a variable like $vmspwof.. so how can I  import that  variable into .ps1 file



  • 4.  RE: graceful shutdown of vms if vm tools installed
    Best Answer

    Posted Jun 21, 2014 09:40 AM

    What do you want to export, the names of the VMs and how they were stopped ?

    If you put the above code in a separate .ps1 file, you can add a param statement and use that on the Name parameter of the Get-VM cmdlet.

    Like this

    Param([string[]]$VMNames)

    Get-VM -Name $VMNames | %{

         if($_.ExtensionData.Guest.ToolsRunningStatus -eq "GuestToolsNotRunning"){

            Stop-VM -VM $_ -Confirm:$false

        }

        else{

            Shutdown-VMGuest -VM $_ -Confirm:$false

       }

    }

    Now you can call this

    .\myscript.ps1 -VMNames $vmspwof



  • 5.  RE: graceful shutdown of vms if vm tools installed

    Posted Jun 21, 2014 10:04 AM

    Thanks a lot LucD..thats worked.. I'm learning a lot from you:smileyhappy: I'm already Owned

    VMware vSphere PowerCLI Reference: Automating vSphere Administration book. With this book it's saving lot of Time while working on vmware environment..




  • 6.  RE: graceful shutdown of vms if vm tools installed

    Posted Nov 13, 2014 11:57 AM

    Hi Luc

    Sorry to come back on this:smileyconfused: This code is not working as per my requirement..For every VM mentioned in variable its just powering off.If the tools are running it should do guest shutdown but that doesn't happen with this code.. Can you correct this..

    Used as below:

    .\gracefullpoweroffvms.ps1 -VMNames $VMNames



  • 7.  RE: graceful shutdown of vms if vm tools installed

    Posted Nov 13, 2014 12:03 PM

    What states are VMware Tools having on the VMs that are not gracefully shutdown ?

    Currently the script only checks for GuestToolsNotRunning.

    Run the following to get an overview, and let me know which states the VMs that were incorrectly stopped are having.

    Get-VM | Select Name,@{N="VMware Tools State";E={$_.Extensiondata.Guest.ToolsStatus}}



  • 8.  RE: graceful shutdown of vms if vm tools installed

    Posted Nov 13, 2014 12:22 PM

    I checked on a single VM where Tools state is running(Out-of-date),with the above code this vm should perform Shutdown-VMGuest but it was poweredoff.(Stop-VM)

    Out put for a VM

    Name                                                         VMware Tools State

    ----                                                                 ------------------

    MLXISGTEMP                                                             toolsOld



  • 9.  RE: graceful shutdown of vms if vm tools installed

    Posted Nov 13, 2014 12:59 PM

    Just noticed there was an error in the script, I updated the code above.

    Can you give it another try ?



  • 10.  RE: graceful shutdown of vms if vm tools installed

    Posted Nov 13, 2014 01:18 PM

    Yes now its working..Thanks Luc..Even earlier i  tried changing the operator to ne to eq..but i didn't noticed this GuestInfo.ToolsRunningStatus.