Skip to content

Instantly share code, notes, and snippets.

@YagmurOzden
Last active July 30, 2023 07:56
Show Gist options
  • Save YagmurOzden/7955de7cb33c538c3691e6bd9317fdc6 to your computer and use it in GitHub Desktop.
Save YagmurOzden/7955de7cb33c538c3691e6bd9317fdc6 to your computer and use it in GitHub Desktop.
Select virtual machine by its name (also, this action checks whether VM tools are installed, if VM is powered on, etc.)
// VMware vRealize Orchestrator action sample
// vRA 8.8.0
//input type : hostname [string]
//return type: VC:VirtualMachine
var vmSearch = VcPlugin.getAllVirtualMachines(null, hostname);
if (vmSearch.length > 1) {
for each (var vmObj in vmSearch) {
if (vmObj.summary.guest) {
System.log("Matched ## " + vmObj.summary.guest.hostName);
}
}
throw new Error("Multiple VMs found with name " + hostname)
}
if (vmSearch.length === 0) {
System.log("VM Search Length: "+vmSearch.length)
throw new Error("VM with name " + hostname + " not found")
}
if (vmSearch[0].runtime.powerState.name !== "poweredOn") {
throw new Error("VM " + hostname + " is not in Powered On state")
}
if (!vmSearch[0].summary.guest) {
throw new Error("No guest information accessible for VM " + hostname)
}
if (vmSearch[0].summary.config.numEthernetCards !== 2) {
throw new Error("Unsupported amount of network adapter for VM " + hostname)
}
var vmInfo = vmSearch[0].summary.guest;
if (vmInfo.hostName !== hostname) {
System.log("Matched: " + vmInfo.hostName);
throw new Error("No exact Match found for Hostname " + hostname)
}
if (vmSearch[0].name !== hostname) {
System.log("Matched: " + vmInfo.hostName);
throw new Error("No exact Match found for Hostname " + hostname)
}
if (vmInfo.toolsStatus.name !== "toolsOk") {
System.log("Status: " + vmInfo.toolsStatus);
throw new Error("VMware tools not in a healty state on VM " + hostname)
}
if (vmInfo.toolsRunningStatus !== "guestToolsRunning") {
System.log("Status: " + vmInfo.toolsRunningStatus);
throw new Error("VMware tools are not in a running state on VM " + hostname)
}
if (vmInfo.guestFullName !== "Red Hat Enterprise Linux 7 (64-bit)") {
System.log("OS version: " + vmInfo.guestFullName);
throw new Error("Unsupported OS found on VM " + hostname)
}
if (!vmInfo.ipAddress) {
throw new Error("No primary IP address found for VM " + hostname)
}
var sshTest = System.getModule("someModulename").executeSSHCommand(
hostname,
"echo test",
true
);
if (sshTest.stderr) {
System.log(JSON.stringify(sshTest, null, 2));
throw new Error("No SSH connection possible to VM " + hostname)
}
return vmSearch[0];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment