VMware Cloud Community
emcclure
Enthusiast
Enthusiast
Jump to solution

Trying to check if tools is running in order to reboot or reset VM

Hello,

I'm trying to reboot VM's in a specified folder, but I first want to check if the VM has tools running or not.  If it's running I want it to use the Restart-VMGuest, if it's not running and powered on I want to use Restart-VM and if it's powered off I want it to do nothing.  Below is a snippet of what I have:

$vmlocation = Get-VM -Location $myfolder | sort Name #Gets folder where VM's are located, if folder name is not unique in vCenter it will reboot VM's in all folders with same name
foreach ($vm in $vmlocation) {

$VMname = Get-VM -Name $vm | Select Name, @{N="Powerstate";E={($_).powerstate}} #Checking power state#
if ($VMname.Guest.ToolsStatus -eq "Running")
{
Write-Host "VM -->", $VMname.Name, "is powered on and tools running, script will restart the VM"
Restart-VMGuest -VM $VMname.Name -Verbose:$false -Confirm:$false  #Rebooting thru Guest OS, tools must be installed for this to work
}
elseif ($VMname.Powerstate -eq "PoweredOff")
{
Write-Host "VM-->", $VMname.Name, "is powered off, so ignoring"
}
else
{
Restart-VM -VM $VMname.Name -Verbose:$false -Confirm:$false #Restarts remaining VMs that don't have tools running
}
}

So for the most part this works for me.  I can get it to ignore the VM's that are powered off, however it just goes to do the Restart-VM each time instead of the Restart-VMGuest.  I know I'm missing something, but anytime I make a change it doesn't help.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmlocation = Get-VM -Location $myfolder | Sort-Object -Property Name #Gets folder where VM's are located, if folder name is not unique in vCenter it will reboot VM's in all folders with same name

foreach ($vm in $vmlocation) {

  if ($vm.Guest.State -eq "Running") {

   Write-Host "VM -->", $vm.Name, "is powered on and tools running, script will restart the VM"

   Restart-VMGuest -VM $vm -Confirm:$false  #Rebooting thru Guest OS, tools must be installed for this to work

  }

  elseif ($vm.Powerstate -eq "PoweredOff") {

   Write-Host "VM-->", $vm.Name, "is powered off, so ignoring"

  }

  else {

   Restart-VM -VM $vm.Name -Confirm:$false #Restarts remaining VMs that don't have tools running

  }

}


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

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmlocation = Get-VM -Location $myfolder | Sort-Object -Property Name #Gets folder where VM's are located, if folder name is not unique in vCenter it will reboot VM's in all folders with same name

foreach ($vm in $vmlocation) {

  if ($vm.Guest.State -eq "Running") {

   Write-Host "VM -->", $vm.Name, "is powered on and tools running, script will restart the VM"

   Restart-VMGuest -VM $vm -Confirm:$false  #Rebooting thru Guest OS, tools must be installed for this to work

  }

  elseif ($vm.Powerstate -eq "PoweredOff") {

   Write-Host "VM-->", $vm.Name, "is powered off, so ignoring"

  }

  else {

   Restart-VM -VM $vm.Name -Confirm:$false #Restarts remaining VMs that don't have tools running

  }

}


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

0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

Hi LucD,

That worked.  Thanks so much.  Interesting though one of the VM's I tested on shows tools as 'not running' but it still rebooted the VM without doing the reset.  Odd, but it works, so that's what matters.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could the Guest.State property have shown an incorrect value?


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

0 Kudos
emcclure
Enthusiast
Enthusiast
Jump to solution

It's possible it was just a browser issue.  I refreshed it again and now it shows tools are running.  Odd for sure.

0 Kudos