Script to create a new user and add them to a role?

I am the worst with scripting. I am sure someone has already created a working script to create a new user and add them to a role.

I've been trying all day but have not moved forward at all. I've used many posts here in the forum. I am using all default settings in my GW.

Would someone Pleeease share their working script with me?

Check out the example script given in the manual's documentation
system.user.getNewUser | Ignition User Manual (inductiveautomation.com)

I appreciate the response.

I have seen and attempted to use that information to no avail. To be honest, I don't even know where to start besides the script console tool in Designer. I've been spinning my wheels.

Post what you've tried and we can tell you what's wrong with it. Even if someone gives you something that works perfectly, cargo-culting scripts that way is an easy recipe for unmaintainable systems down the road.

We could also try this: Why are you reaching to scripting to perform this operation? If it's a one-off, the user interface on the gateway webpage should be sufficient. If you want it in your visualization system (Vision, Perspective) then you should be able to use either the built-in GUI tools or this Exchange resource. Scripting would only be necessary if you want to perform this "create user with roles operation" automatically based on some trigger condition (which?).

Attached are 2 that I've been working on.

NewUser.py (878 Bytes)

NewUser_attempt2.txt (1.1 KB)

I forgot to add...

I would like to use a script due to the fact that I need to update user accounts on approximately 40 Ign servers. I thought it would speed things up by using a script but with all the time I have wasted today, I probably could have had it done by now. Haha

Okay. Well, step 0 is "stop trusting ChatGPT". It doesn't know enough about Ignition to be helpful, and instead will lead you down a thousand dark alleys, confidently taking advantage of the fact that you don't know what you're doing yet.

I slightly modified the example on the page @dkhayes117 linked above.

Paste this into your script console, and confirm that it's working.

# Util for printing the responses.
def printResponse(responseList):
    if len(responseList) > 0:
        for response in responseList:
            print response
    else:
        print " None"
  
# Make a brand new 'blank' user.
user = system.user.getNewUser("", "ignuser")
  
# Let's fill in some fields. Note we have two ways to access property names.
user.set("firstname", "Naomi")
user.set(user.LastName, "Nagata")
user.set("password", "1234567890")

# We can add a role. If the role doesn't already exist, user save will fail, depending on user source.
user.addRole("Administrator")
  
# # We can add a lot of roles.
# roles = ["Administrator", "Operator"]
# user.addRoles(roles)

# Finally, we will save our new user and print responses.
response = system.user.addUser("", user)
  
print "Warnings are:"
printResponse(response.warns)
   
print "Errors are:"
printResponse(response.errors)

print "Infos are:"
printResponse(response.infos)
1 Like

Totally busted. I got desperate and did try ChatGPT. ha You're good!

With the script you provided, I am receive this error:

Errors are:
You are not authorized to modify the Gateway system user source.

Okay, so you need to check the 'Allow User Admin' box in the gateway's security settings: Gateway General Security Settings | Ignition User Manual

1 Like

You are officially my Hero of the day!

Thank you soo much!

One more question.

How can I use the script to update user information? Such as updating a password or role to an existing user?

Instead of using getNewUser (to...get a new user :smile:) you would use system.user.getUser to retrieve an existing user:

Then make the changes you want to that user, and use system.user.editUser as you did in the first script to apply those edits back.

1 Like