VMware Cloud Community
vin01
Expert
Expert
Jump to solution

graceful shutdown of vms if vm tools installed

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.

Regards Vineeth.K
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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


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

View solution in original post

0 Kudos
9 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Get-VM | %{

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

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

    }

    else{

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

   }

}


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

0 Kudos
vin01
Expert
Expert
Jump to solution

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

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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


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

0 Kudos
vin01
Expert
Expert
Jump to solution

Thanks a lot LucD..thats worked.. I'm learning a lot from youSmiley Happy 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..


Regards Vineeth.K
0 Kudos
vin01
Expert
Expert
Jump to solution

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

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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}}


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

0 Kudos
vin01
Expert
Expert
Jump to solution

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

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Can you give it another try ?


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

0 Kudos
vin01
Expert
Expert
Jump to solution

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.

Regards Vineeth.K
0 Kudos