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.