VMware Cloud Community
MihirP
Enthusiast
Enthusiast
Jump to solution

Power-off VMs and ESXi via SSH

Hello,

I need to;

1) Shutdown all VMs running on ESXi host

2) Then shutdown that ESXi host

How to achieve this via SSH?

Thanks.

0 Kudos
1 Solution

Accepted Solutions
BenLiebowitz
Expert
Expert
Jump to solution

I've found it a lot easier to do this via PowerCLI... 

$vc = vcenter1

$dc = "Datacenter"

Connect-VIServer $vc

# Gets the list of all VMs in the datacenter (except the vCenter VM itself)

$vms = Get-datacenter -Name $dc | Get-VM | where {$_.Name -ne $vc -and $_.PowerState -eq "PoweredOn"}

# Shuts down the VMs in the datacenter that are in a powered on state.

Foreach ($vm in $vms){

  if($vm.ExtensionData.Config.Tools.ToolsVersion -ne "0"){

  #gracefully shutdown

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

  sleep -Seconds 60

  }

  #forcefully stopping VM

  Stop-VM -VM $vm -Confirm:$false

  }

# Shutdown vCenter VM (Comment out if vCenter is not virtual)

sleep -seconds 120

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

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.

View solution in original post

0 Kudos
6 Replies
vfk
Expert
Expert
Jump to solution

Hi,

You should really shut down the VMs gracefully, but if you wanted to force shutdown then have a look at this KB VMware KB: Powering off a virtual machine on an ESXi host

You can shutdown host using DCUI - VMware KB: ESXi/ESX 4.x and ESXi 5.x/6.0 shutdown and reboot commands

--- If you found this or any other answer helpful, please consider the use of the Helpful or Correct buttons to award points. vfk Systems Manager / Technical Architect VCP5-DCV, VCAP5-DCA, vExpert, ITILv3, CCNA, MCP
0 Kudos
fsckit
Enthusiast
Enthusiast
Jump to solution

Here is how I would do it, running commands from the vMA:

1.) vmware-cmd --server <esxi_hostname> -l

2.) loop though the above output, and ssh in to each VM, and issue the shutdown command.

You would have no need to shut down the VM; it should show as powered down once the OS shuts down.

0 Kudos
MihirP
Enthusiast
Enthusiast
Jump to solution

I need to do from PUTTY, how to do that?

Thanks.

0 Kudos
fsckit
Enthusiast
Enthusiast
Jump to solution

PuTTY is just another terminal program for connecting to remote servers via SSH (or other protocols).  This is not the place to learn how to use PuTTY or SSH.  In brief, you would already have an open PuTTY terminal, with an SSH connection to your vMA.  From that shell prompt is where you would run your vmware-cmd command and then ssh in to all the other servers. Ideally you'd have ssh keys set up on the remote servers so that you don't have to type your password each time. 

0 Kudos
fsckit
Enthusiast
Enthusiast
Jump to solution

Here is another way to do it that does not require SSH at all, just the vMA & the vi Perl Toolkit:

vmcontrol.pl --server <vCenter> --username <UID> --vmname <vm> --operation shutdown

0 Kudos
BenLiebowitz
Expert
Expert
Jump to solution

I've found it a lot easier to do this via PowerCLI... 

$vc = vcenter1

$dc = "Datacenter"

Connect-VIServer $vc

# Gets the list of all VMs in the datacenter (except the vCenter VM itself)

$vms = Get-datacenter -Name $dc | Get-VM | where {$_.Name -ne $vc -and $_.PowerState -eq "PoweredOn"}

# Shuts down the VMs in the datacenter that are in a powered on state.

Foreach ($vm in $vms){

  if($vm.ExtensionData.Config.Tools.ToolsVersion -ne "0"){

  #gracefully shutdown

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

  sleep -Seconds 60

  }

  #forcefully stopping VM

  Stop-VM -VM $vm -Confirm:$false

  }

# Shutdown vCenter VM (Comment out if vCenter is not virtual)

sleep -seconds 120

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

Ben Liebowitz, VCP vExpert 2015, 2016, & 2017 If you found my post helpful, please mark it as helpful or answered to award points.
0 Kudos