VMware Cloud Community
jonebgood_157
Enthusiast
Enthusiast
Jump to solution

Jscript code help for string exclusion in scriptable task

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....

 

var hosts = vcenter.allHostSystems;
for (var h in hosts) {
    var host = hosts[h];
System.log("ESXi Host " + host.name + ":");
var specAccount = new VcHostAccountSpec();
specAccount.id = username;
specAccount.password = password;

host.configManager.accountManager.updateUser(specAccount)};
0 Kudos
3 Solutions

Accepted Solutions
eoinbyrne
Expert
Expert
Jump to solution

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);
    }
}

View solution in original post

eoinbyrne
Expert
Expert
Jump to solution

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.

View solution in original post

eoinbyrne
Expert
Expert
Jump to solution

Hi again 🙂

For what you want here you can do like this

eoinbyrne_0-1687944456807.png

 

The member value host.runtimeInfo.connectionState is one of these Enums

eoinbyrne_1-1687944535764.png

 

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");
}

 

View solution in original post

10 Replies
eoinbyrne
Expert
Expert
Jump to solution

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);
    }
}
jonebgood_157
Enthusiast
Enthusiast
Jump to solution

@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

2022-11-18 10:10:59.569 -08:00 INFO__item_stack:/item1
2022-11-18 10:10:59.616 -08:00 INFO[INFO] Setting password of user 'root' for ESXi hosts in vcenter 'https://test.lab.com:443/sdk'.
2022-11-18 10:10:59.623 -08:00 INFO ESXi Host testmgt01.lab.com:
2022-11-18 10:10:59.624 -08:00 INFO ESXi Host testpoc01.lab.com:
2022-11-18 10:10:59.726 -08:00 INFO ESXi Host testpoc02.lab.com:
2022-11-18 10:10:59.840 -08:00 INFO ESXi Host testpoc03.lab.com:
2022-11-18 10:10:59.939 -08:00 INFO__item_stack:/item0
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

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.

jonebgood_157
Enthusiast
Enthusiast
Jump to solution

Exactly what i needed. thanks again!

0 Kudos
jonebgood_157
Enthusiast
Enthusiast
Jump to solution

@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

0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

Hi again 🙂

For what you want here you can do like this

eoinbyrne_0-1687944456807.png

 

The member value host.runtimeInfo.connectionState is one of these Enums

eoinbyrne_1-1687944535764.png

 

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");
}

 

jonebgood_157
Enthusiast
Enthusiast
Jump to solution

@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"

0 Kudos
jonebgood_157
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
jonebgood_157
Enthusiast
Enthusiast
Jump to solution

@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)

if (host.runtime.connectionState.value == "connected")
0 Kudos
eoinbyrne
Expert
Expert
Jump to solution

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

0 Kudos