Hopefully i have posted on the correct forum.
I have just had a read through the Distributed Power Management PDF. I am looking to do something similar but not the same.
I need to be able to easily turn a cluster/site off with as little interaction as possible.
I need to gracefully shutdown all OS’s and then shut down the Hosts. Any easy way to do this, or do you have any recourses to point me in the right direction?
Possibly if i could use the same script against different sites depending on which on i choose that would be even better!
You can try below code snippet. For gracefully shutdown of OS, you need VMware tool installed.
Uncomment the line no 12, if you need to stop the VM forcefully.
Connect-VIServer <server> -User <user> -Password <password> $cluster = Read-Host "Enter cluster name" $vms = Get-Cluster -Name $cluster | Get-VM Foreach ($vm in $vms){ if ($vm.PowerState -eq "PoweredOn"){ $vmMOR = $vm|Get-View if($vmMOR.Config.Tools.ToolsVersion -ne "0"){ #gracefully shutdown Shutdown-VMGuest -VM $vm -Confirm:$false } #forcefully stopping VM #Stop-VM -VM $vm -Confirm:$false } } $vmhosts = Get-Cluster -Name $cluster | Get-VMHost Foreach ($vmhost in $vmhosts){ #shutdown host Stop-VMHost -VMHost $vmhost -Force:$true -Confirm:$false }
Hope this helps
To turn off the VM's gracefully and then shutdown the esx host, see the below link, which provides the scripts to do it.
http://communities.vmware.com/message/1161806
Jay
MCSE,VCP 310,VCP 410
Consider awarding points for "helpful" and/or "correct" answers.
i also need to disable DRS, and vmotion at the start of the script.
what are the commands to disable thsi for the cluster?
Powering back on will be left as a manual process.
For disabling the DRS on the cluster.Refer the below link.
http://communities.vmware.com/message/1524159;jsessionid=D8AFDC811069F33B8B5ECD160E3486AB
Jay
VCP 310,VCP 410,MCSE
Consider awarding points for "helpful" and/or "correct" answers.
you can also try,
$Cluster = Get-Cluster -Name <cluster>|Get-View $spec = New-Object VMware.Vim.ClusterConfigSpec $spec.DasConfig = New-Object VMware.Vim.ClusterDasConfigInfo $spec.DrsConfig = New-Object VMware.Vim.ClusterDrsConfigInfo $spec.DrsConfig.Enabled = $false $spec.DasConfig.Enabled = $false $Cluster.ReconfigureCluster_Task($spec,$true)
This will disable both HA and DRS feature
Thanks for your help so far, all great answers, but i need to be able to prompt what vcenter to connect to, and the username and password when connecting. This will save having it in plain text or trying to encrypt it.
Will the following work?
$server = Read-Host "Please input the server name"
$creds = Get-Credentials
I have the code below, it is still missing a few things.
First and last lines I am loading and unloading the VMWare module for powershell.
I am only disabling DRS and VMotion. I still cant find the command to disable HA. It could probably stay enabled to save time and it wont stop this script fro mworking.
Lastly I need to either pause for a few minutes while the VM's gracefully shutdown, or do something even smarter where it waits and checks until the VM's turn off. Only catch to this is if it is a Linux or a Windows 2000 VM, to machine does not fully power off automatically, it sits in the shutdown state. So the script will still be waiting there for manual interaction to power those virtual machines off??
But the main feature required here would be, do not shutdown the hosts until all the VM's are powered off.
Add-PSSnapin VMware.VimAutomation.Core "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\scripts\Initialize-VIToolkitEnvironment.ps1" Connect-VIServer vCenterServer $creds = Get-Credential $cluster = Read-Host "Enter cluster name" #Disable DRS and VMotion #$Cluster = Get-Cluster -Name <cluster>|Get-View $spec = New-Object VMware.Vim.ClusterConfigSpec $spec.DasConfig = New-Object VMware.Vim.ClusterDasConfigInfo $spec.DrsConfig = New-Object VMware.Vim.ClusterDrsConfigInfo $spec.DrsConfig.Enabled = $false $spec.DasConfig.Enabled = $false $Cluster.ReconfigureCluster_Task($spec,$true) $vms = Get-Cluster -Name $cluster | Get-VM Foreach ($vm in $vms){ if ($vm.PowerState -eq "PoweredOn"){ $vmMOR = $vm|Get-View if($vmMOR.Config.Tools.ToolsVersion -ne "0"){ #gracefully shutdown Shutdown-VMGuest -VM $vm -Confirm:$false } #forcefully stopping VM #Stop-VM -VM $vm -Confirm:$false } } $vmhosts = Get-Cluster -Name $cluster | Get-VMHost Foreach ($vmhost in $vmhosts){ #maintenance mode host vimsh -n -e /hostsvc/maintenance_mode_enter #shutdown host Stop-VMHost -VMHost $vmhost -Force:$true -Confirm:$false } Remove-PSSnapin VMware.VimAutomation.Core
Hi,
1) The script, shutdown the VM gracefully if “tools are installed” and forcefully shutdown if no tool is installed (no manual interaction).
2) Script disable the HA and DRS
3) The Shutdown-VMGuest, command is synchronous i.e. the script will wait until first VM completely shutdown and then switch to other. Still I have added sleep cmdlet, you can increase the second as per your requirement
Add-PSSnapin VMware.VimAutomation.Core "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\scripts\Initialize-VIToolkitEnvironment.ps1" $creds = Get-Credential Connect-VIServer vCenterServer -User $creds.UserName -Password $creds.Password $Cluster = Read-Host "Enter cluster name" $morCluster = Get-Cluster -Name $Cluster | Get-View #Disable DRS and HA $spec = New-Object VMware.Vim.ClusterConfigSpec $spec.DasConfig = New-Object VMware.Vim.ClusterDasConfigInfo $spec.DrsConfig = New-Object VMware.Vim.ClusterDrsConfigInfo $spec.DrsConfig.Enabled = $false $spec.DasConfig.Enabled = $false $morCluster.ReconfigureCluster_Task($spec,$true) $vms = Get-Cluster -Name $Cluster | Get-VM Foreach ($vm in $vms){ if ($vm.PowerState -eq "PoweredOn"){ $vmMOR = $vm|Get-View if($vmMOR.Config.Tools.ToolsVersion -ne "0"){ #gracefully shutdown Shutdown-VMGuest -VM $vm -Confirm:$false sleep -Seconds 20 } #forcefully stopping VM Stop-VM -VM $vm -Confirm:$false sleep -Seconds 20 } } $vmhosts = Get-Cluster -Name $Cluster | Get-VMHost Foreach ($vmhost in $vmhosts){ #shutdown host Stop-VMHost -VMHost $vmhost -Force:$true -Confirm:$false } Remove-PSSnapin VMware.VimAutomation.Core
-If you found this information useful, please consider awarding points for Correct or Helpful.
There is no need to use the SDK methods for this.
Try this (I assume the snapin is loaded and that you are connected to the vCenter)
$clusterName = Read-Host "Enter cluster name" $clus = Get-Cluster -Name $clusterName #Disable DRS and HA $clus | Set-Cluster -HAEnabled:$false -DrsEnabled:$false -Confirm:$false # Stop the powered on guest $clus | Get-VM | where {$_.PowerState -eq "PoweredOn"} | %{ if($_.Guest.State -eq "Running"){ Shutdown-VMGuest -Guest $_ -Confirm:$false } else{ Stop-VM -VM $_ -Confirm:$false } } # Stop the connected ESX(i) servers $clus | Get-VMHost | where {$_.State -eq "Connected"} | %{ $_ | Stop-VMHost -Confirm:$false -Force:$true }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks a lot for the script guys, really helpful. I do have one questions thoug. Would it be possible to exlude certain virtual machine? what i mean is lets say in my cluster i have a virtual machine which is called "engineering" and i want to turn off all the other machines, using the scripts provided, except for the "engineering" virtual machine. How would i go about doing that? Any help would be much appreciated.
You can add this to the condition in the where-clause
Change this
$clus | Get-VM | where {$_.PowerState -eq "PoweredOn"} | %{
into this
$clus | Get-VM | where {$_.PowerState -eq "PoweredOn" -and $_.Name -ne "engineering"} | %{
Note that you will have to change the stopping of the ESX(i) hosts, if there is still a guest running the host where it runs will not stop.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great! Thanks, LucD. Really appreciate the quick help.