Skip to content

Instantly share code, notes, and snippets.

@trisharia
Created March 27, 2017 20:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trisharia/d1be95abf8f926bed1c82cb938daf242 to your computer and use it in GitHub Desktop.
Save trisharia/d1be95abf8f926bed1c82cb938daf242 to your computer and use it in GitHub Desktop.
Start, stop, restart, or uninstall services of an ESXi Host in vCenter
// VMware vRealize Orchestrator action sample
//
// Get all the security profile service keys (IDs) of an ESXi Host, used as parameters for starting/stopping/restarting a service.
//
// For vRO 6.0+/vCenter 6.0+
//
// Action Inputs:
// host - VC:HostSystem - ESXi Host from which to retrieve services
// logging - boolean - If true, write verbosely to the vRO system logs
//
// Return type: Array/string - The service keys of a VC:HostSystem
var hostServiceSystem = host.configManager.serviceSystem;
hostServiceSystem.refreshServices();
var services = hostServiceSystem.serviceInfo.service;
var serviceKeys = new Array();
for each (svc in services){
if(logging == true){
System.log("Key: "+svc.key+" -- Label: "+svc.label+" -- Policy: "+svc.policy);
}
serviceKeys.push(svc.key);
}
return serviceKeys;
// VMware vRealize Orchestrator action sample
//
// Start, stop, restart, or uninstall security profile services of an ESXi Host.
// Optionally, specify whether a service policy is to start automatically when ports are open (automatic),
// start with server (on), or start manually (off).
// WARNING - no checks to see if service is required when attempting to uninstall.
//
// For vRO 6.0+/vCenter 5.5+
//
// Action Inputs:
// host - VC:HostSystem - ESXi Host to update
// serviceName - string - Key of the ESXi service to update (can get valid keys with getVcHostSystemServiceKeys.js)
// serviceAction - string - Can be: start, stop, restart, uninstall
// policyAction - string - Can be: on, off, automatic
//
// Return type: void
// Get the hostServiceSystem object from the host:
var hostServiceSystem = host.configManager.serviceSystem;
// refresh services info to make sure all properties are fresh:
hostServiceSystem.refreshServices();
var serviceObj = getService(hostServiceSystem, serviceName);
performServiceAction(hostServiceSystem, serviceObj, serviceAction);
performPolicyAction(hostServiceSystem, serviceObj, policyAction);
// Get Service:
function getService(hostServiceSystem, serviceName) {
var serviceObj = null;
var services = hostServiceSystem.serviceInfo.service;
for each (svc in services) {
if (svc.key === serviceName) {
System.log("Service Found! "+svc.label);
return svc;
}
}
// Throw exception if failed to get service:
throw "Unable to locate service '" + serviceName + "' on host: " + host.name;
}
// Perform service action:
function performServiceAction(hostServiceSystem, serviceObj, serviceAction) {
switch (serviceAction) {
case "start":
// before trying to start, make sure running is not true
if (serviceObj.running != true){
hostServiceSystem.startService(serviceObj.key);
}
break;
case "stop":
if (serviceObj.running == true){
hostServiceSystem.stopService(serviceObj.key);
}
break;
case "restart":
hostServiceSystem.restartService(serviceObj.key);
break;
case "uninstall":
try {
hostServiceSystem.uninstallService(serviceObj.key);
} catch(err) {
throw "Error uninstalling service " + serviceObj.key + " (" + err + ")";
}
break;
default:
System.warn("Invalid service action selected");
}
}
function performPolicyAction(hostServiceSystem, serviceObj, policyAction) {
if (policyAction != null && policyAction != "" && policyAction != "No Change") {
// only valid policy actions are: on, off, automatic
try {
hostServiceSystem.updateServicePolicy(serviceObj.key,policyAction);
} catch(err) {
throw "Error updating service policy " + serviceObj.key + " (" + err + ")";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment