I have a functional vro workflow that changes all the ESXI root passwords for a given input vCenter. Works fine, however I want to exclude certain hosts that match a string "mgt" in their hostnames. I can't figure out the jScript code to do this. I would think it would be something in my var statement....
Try this?
var hosts = vcenter.allHostSystems;
for (var h in hosts) {
var host = hosts[h];
System.log("ESXi Host " + host.name + ":");
// test the name does NOT have mgt in it
if(host.name.indexOf("mgt") == -1)
{
var specAccount = new VcHostAccountSpec();
specAccount.id = username;
specAccount.password = password;
host.configManager.accountManager.updateUser(specAccount);
}
}
var hosts = vcenter.allHostSystems;
for (var h in hosts) {
var host = hosts[h];
System.log("ESXi Host " + host.name + ":");
// test the name does NOT have mgt in it
if(host.name.indexOf("mgt") == -1)
{
var specAccount = new VcHostAccountSpec();
specAccount.id = username;
specAccount.password = password;
host.configManager.accountManager.updateUser(specAccount);
}
else
{
System.log("Skipping MGT host " + host.name);
}
}
I just added the else clause with the info message.
Hi again 🙂
For what you want here you can do like this
The member value host.runtimeInfo.connectionState is one of these Enums
So you need the ".value" on the end to extract the String form. Then for your test for "disconnected" you can do this
if(h.runtimeInfo.connectionState.value == "disconnected")
{
// do stuff here
}
else
{
System.log("Skipping Host " + h.name + " as it is disconnected right now");
}
Try this?
var hosts = vcenter.allHostSystems;
for (var h in hosts) {
var host = hosts[h];
System.log("ESXi Host " + host.name + ":");
// test the name does NOT have mgt in it
if(host.name.indexOf("mgt") == -1)
{
var specAccount = new VcHostAccountSpec();
specAccount.id = username;
specAccount.password = password;
host.configManager.accountManager.updateUser(specAccount);
}
}
@eoinbyrne Thanks! that worked. so another question. I had to test it on each one to verify. is there a way to make my system.log code more intelligent. The workflow skipped my host testmgt01.lab.com, which is what I wanted, but the log didnt really give me an idea if it skipped or change the password on it. here was the log...
Ideally, it would just show me the hosts it actually changed the password on
var hosts = vcenter.allHostSystems;
for (var h in hosts) {
var host = hosts[h];
System.log("ESXi Host " + host.name + ":");
// test the name does NOT have mgt in it
if(host.name.indexOf("mgt") == -1)
{
var specAccount = new VcHostAccountSpec();
specAccount.id = username;
specAccount.password = password;
host.configManager.accountManager.updateUser(specAccount);
}
else
{
System.log("Skipping MGT host " + host.name);
}
}
I just added the else clause with the info message.
Exactly what i needed. thanks again!
@eoinbyrne Hey there. you helped me a lot last time when I was trying to figure this out. The script is working and excludes the string match that you gave me. However, I noticed that when the script hits an ESXi host that is "disconnected" state in vCEnter, the job hangs on it, times out, and fails. Do you know how I can skip those hosts that are in that "disconnected" state? thanks a ton in advance
Hi again 🙂
For what you want here you can do like this
The member value host.runtimeInfo.connectionState is one of these Enums
So you need the ".value" on the end to extract the String form. Then for your test for "disconnected" you can do this
if(h.runtimeInfo.connectionState.value == "disconnected")
{
// do stuff here
}
else
{
System.log("Skipping Host " + h.name + " as it is disconnected right now");
}
@eoinbyrne Do i need to install a different enum plugin to get this to work? I tried creating the first step like you did as an Action Script to get the hostconnection state but it errors with "TypeError: Cannot read property "runtime" from undefined"
scratch that last comment; i figured that out with wrong code. however, in my script run I get the following:
TypeError: Cannot read property "connectionState" from undefined
@eoinbyrne I got it figured out after numerous failures. I changed the if, then for if state is "connected" do stuff, else skip the "disconnected" ones. I did that because I was thinking they could be disconnected OR not responding. I wasn't sure how to include both states in your code so I went the opposite direction to look for "connected" state.
BUT, I do have a question. Previously you helped me to include a statement if(host.name.indexOf("mgt") == -1)
which would skip over any host with "mgt" in its name. How can I combine that statement WITH the host connection state? The two conditions are:
if(host.name.indexOf("mgt") == -1)
Hi again
You can do what need to do here like this
if( (host.name.indexOf("mgt") == -1) && (host.runtime.connectionState.value == "connected") )
{
// do stuff here
}
else
{
System.log("Skipping MGT or disconnected host " + host.name + ", status " + host.runtime.connectionState.value);
}
- HTH