Skip to content

Instantly share code, notes, and snippets.

@vmwarecode
Created August 10, 2016 21:40
Show Gist options
  • Save vmwarecode/8ed2e5f3a5b1c1eb690737c4d5c91175 to your computer and use it in GitHub Desktop.
Save vmwarecode/8ed2e5f3a5b1c1eb690737c4d5c91175 to your computer and use it in GitHub Desktop.
Reboot Guest OS on vSphere VM and wait
// Copyright 2016, VMware, Inc. All Rights Reserved
//
// VMware vRealize Orchestrator action sample
// Reboots a vSphere VM and waits for the guest OS to be
// back on the network
//
// Requires VMtools to be functional in the guest OS.
//
//Action Inputs:
// vm - VC:VirtualMachine
// guestRebootTimeoutMinutes - number
//
//Return type: void
vm.rebootGuest();
System.log("Rebooting vm '" + vm.name + "'");
System.log("Waiting for upto " + guestRebootTimeoutMinutes + " minutes for VMtools to stop...");
var now = new Date();
var endTime = new Date(now.getTime() + guestRebootTimeoutMinutes * 60 * 1000);
var timedOut = false;
System.log("VM = '" + vm.name + "', Tools Status = '" + vm.guest.toolsStatus.value + "'");
if (vm.guest.toolsStatus.value === VcVirtualMachineToolsStatus._toolsNotInstalled) {
throw("VM tools is not installed on guest of VM: "+vm.name+". Will not beable to determine the reboot status of the guest");
}
// wait until the vm is offline
while (true) {
now = new Date();
if (now > endTime) {
timedOut = true;
break;
}
if (vm.guest.toolsStatus.value == VcVirtualMachineToolsStatus._toolsNotRunning) {
System.log("VM = '" + vm.name + "', Tools Status = '" + vm.guest.toolsStatus.value + "'");
break;
}
// check every 5 seconds
System.sleep(5000);
}
if (timedOut) {
errorCode = "Timed out! Host '" + vm.name + "' is still not running after " + guestRebootTimeoutMinutes + " minutes!";
throw errorCode;
}
@jibanezvela
Copy link

jibanezvela commented Mar 22, 2019

dosen't work for me.

I thing you have to compare "value" with "value":

if (vm.guest.toolsStatus.value === VcVirtualMachineToolsStatus.toolsNotInstalled.value) { .... }

Also "_toolsXXXXX" is missing in my doc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment