jonebgood_157
Enthusiast
Enthusiast

Jscript code help for string exclusion in scriptable task

Jump to solution

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
2 Solutions

Accepted Solutions
eoinbyrne
Expert
Expert

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

4 Replies
eoinbyrne
Expert
Expert

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

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

Exactly what i needed. thanks again!

0 Kudos