VMware Cloud Community
Rashida11
Contributor
Contributor

VMWare San Full shutdown

we are shutting down the entire SAN to apply a patch to our SAN . looking at a script to

1) Get a list of all running Virtual machines in a specified datacenter and export to a file

2) shutdown all running virtual machines in the datacenter

3) shutdown all ESX hosts in datacenter

Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership

You could try this

$datacenter = <datacenter-name>

$vms = Get-Datacenter $datacenter| Get-VM
$servers = Get-Datacenter $datacenter | Get-VMHost

$vms | Export-Csv "C:\VM-datacenter.csv" -noTypeInformation
$vms | Shutdown-VMGuest - Confirm:$false
$servers | Set-VMHost -State Maintenance

$servers | % {
	$esx = $_ | Get-View 
	$taskMoRef = $esx.ShutdownHost_Tsk($true)
	$task = Get-View $taskMoRef 
	while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
		$task = Get-View $taskMoRef
	}
}


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

Reply
0 Kudos
Rashida11
Contributor
Contributor

Thanks for the response

just had a look at the script dumps all the VM in the csv file ..think i wanted just the ones that are powreded on so that only them get powered back on

Cheers

Reply
0 Kudos
LucD
Leadership
Leadership

Missed that but you can easily insert a Select-Object cmdlet.

$datacenter = <datacenter-name>

$vms = Get-Datacenter $datacenter| Get-VM | where-object {$_.PowerState -eq "PoweredOn"} 
$servers = Get-Datacenter $datacenter | Get-VMHost

$vms | Export-Csv "C:\VM-datacenter.csv" -noTypeInformation
$vms | Shutdown-VMGuest - Confirm:$false
$servers | Set-VMHost -State Maintenance

$servers | % {
	$esx = $_ | Get-View 
	$taskMoRef = $esx.ShutdownHost_Tsk($true)
	$task = Get-View $taskMoRef 
	while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
		$task = Get-View $taskMoRef
	}
}


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

Reply
0 Kudos
Rashida11
Contributor
Contributor

Cannot bind parameter 'Server'. Cannot convert value "Confirm:False" to type "VMware.VimAutomation.Types.VIServer". Error: "Invalid cast from 'System.String' to 'VMware.VimAutomation.Types.VIServer'."

At :line:24 char:23

+ $vms | Shutdown-VMGuest &lt;&lt;&lt;&lt; - Confirm:$false

Reply
0 Kudos
LucD
Leadership
Leadership

There was a blank between the dash and the keyword Confirm.

And we better add a -Confirm:$false to the Set-VmHost cmdlet as well.

$datacenter = <datacenter-name>

$vms = Get-Datacenter $datacenter| Get-VM | where-object {$_.PowerState -eq "PoweredOn"} 
$servers = Get-Datacenter $datacenter | Get-VMHost

$vms | Export-Csv "C:\VM-datacenter.csv" -noTypeInformation
$vms | Shutdown-VMGuest -Confirm:$false
$servers | Set-VMHost -State Maintenance -Confirm:$false

$servers | % {
	$esx = $_ | Get-View 
	$taskMoRef = $esx.ShutdownHost_Tsk($true)
	$task = Get-View $taskMoRef 
	while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
		$task = Get-View $taskMoRef
	}
}


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

Reply
0 Kudos
Rashida11
Contributor
Contributor

Thanks for the response . However therejust one thing

1) Set-VMHost 6818B1EC-AA27-4AB3-9AE2-D050D544EC77 There is one or more powered on virtual machines on host. It tends to fail on the host going into maintainance mode cause the Vm are still on the way down ..

Reply
0 Kudos
LucD
Leadership
Leadership

We can use a loop that tests on the powerstate of the guest.


$datacenter = <datacenter-name>

$vms = Get-Datacenter $datacenter| Get-VM | where-object {$_.PowerState -eq "PoweredOn"} 
$servers = Get-Datacenter $datacenter | Get-VMHost

$vms | Export-Csv "C:\VM-datacenter.csv" -noTypeInformation
$vms | % {
	$_ | Shutdown-VMGuest -Confirm:$false
	do {
		sleep 1
	} until ((Get-VM $_).PowerState -eq "PoweredOff")
}

$servers | Set-VMHost -State Maintenance -Confirm:$false

$servers | % {
	$esx = $_ | Get-View 
	$taskMoRef = $esx.ShutdownHost_Task($true)
	$task = Get-View $taskMoRef 
	while ($task.Info.State -eq "running" -or $task.Info.State -eq "queued") {
		$task = Get-View $taskMoRef
	}
}


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

Reply
0 Kudos
Rashida11
Contributor
Contributor

yes thatt will work ... was also thinking of adding a menu to the end of the scipt

  1. Display menu to exit or restart all virtual machines after hosts have been brought back up

function SHOW-MENU{

*

Write-Host " "

**

Write-Host "Select the task to perform."

**

Write-Host " "

**

Write-Host "1. Restart all running virtual machines"

**

Write-Host "0. Exit without startng virtual images"

**

Write-Host " "

*

$a = Read-Host "Select 1 or 0"

*

Write-Host " "

*

switch ($a)

{

0{

"** Quit **";

break;

}

1 {

Clear-Host

" Restarting Running Virtual machines"

$vms | Start-VM -Confirm:$false;

break;

}

default {

Clear-Host

"** The selection could not be determined **";

Invalid-Selection;

break;

}

}

}

function Invalid-Selection{

Clear-Host

Show-Menu

}

So that after the hosts come back up I can automatically restart the shutdown Vms ONLY

Reply
0 Kudos
gboskin
Enthusiast
Enthusiast

Hi Lucd ..

Once again a veru useful script. can you add a check as is seem to fail if any host in the datacenter is already in maintance mode or not responding.....

If it is a total shutdown is there a need to put in maintance mode before shutdown?

Cheers

Reply
0 Kudos
LucD
Leadership
Leadership

No, there is no need to place the ESX server in maintenance mode before doing the shutdown.

I added this as a safety measure should there still be guests running.

If you have DRS set to full automatic the running guest will be migrated first.


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

Reply
0 Kudos
gboskin
Enthusiast
Enthusiast

Lucd see tha attched sipt I have added a section to restart the running virtual machines when the hosts have been brought back up. I have done this based on $VMS..

is this the based way to go with this or do I (or can I) stsrt the vm from the C:\datacenter csv file ..

Cheers

P/S Having thought about this again I think it will be better if it can be started from the CSV file

Reply
0 Kudos
LucD
Leadership
Leadership

The $VMS variable will only keep its content while the script is running.

So it would be better, in my opinion, to save the information in a CSV file.

Btw, in the script I dumped the complete VM object to the CSV file while in fact you only need the name of the VM.

So the following would perhaps be a better way of creating the CSV file

...
$vms | Select Name | Export-Csv "C:\VM-datacenter.csv" -noTypeInformation
...

To get the names back into a variable you can do

...
$VMS = Import-Csv "C:\VM-datacenter.csv"


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

Reply
0 Kudos