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