Topic
2 replies Latest Post - ‏2012-12-05T17:42:57Z by llandale
SystemAdmin
SystemAdmin
3180 Posts
ACCEPTED ANSWER

Pinned topic Add all user to a group(help needed)

‏2012-12-04T19:11:31Z |

Hello,

I'm trying to write a script to place all users in the "DB Admin" group and below is the code I am currenly having issue with; what am I doing wrong? It attempts to add the first user to the groups then it fails and I get a prue vitrual error???

const Group g = find("DB Admin") 
if(null g)
{ halt }
 
Skip AllUserList = create()
 
int cnt = 0
User u = null 
for u in userList do {
    string s = u.name
        put(AllUserList, cnt++, s)
}
 
string sUserName = null
for sUserName in AllUserList do {
        User cUser = find(sUserName)
        if(null cUser) continue
        addMember(g, cUser)
        
        // save new user list
        if (saveDirectory() == null) 
        {
                // refresh
                if (loadDirectory() == null)
                {
                        // inform user
                        infoBox("User '"sUserName"'was added successfully.\n")
                } 
                else 
                {
                        // warn user
                        errorBox("Failed to load user list.\n")
                }
        } 
        else 
        {
                // warn user
                errorBox("Failed to save user list.\n")
        }
}
 
delete AllUserList
Updated on 2014-01-09T10:32:25Z at 2014-01-09T10:32:25Z by iron-man
  • SystemAdmin
    SystemAdmin
    3180 Posts
    ACCEPTED ANSWER

    Re: Add all user to a group(help needed)

    ‏2012-12-04T20:34:15Z  in response to SystemAdmin

    I believe I fixed my issue is my logic correct now?
     

    pragma runLim, 0
     
    const string sgroup = "DB Admin"
     
    // Environment variable
    const string USER_PROFILE = getenv("USERPROFILE") // Desktop variable
    // -------------------------------------------------------------------
    const string OUT_FILE = USER_PROFILE "\\Desktop\\UserGroupUpdate.txt"
    const Stream OUTPUT
    // -------------------------------------------------------------------
    bool writeFileHeader(string filename)
    { // Try to open output file
      bool returnValue = false
      if (canOpenFile(filename, true))
      { // User can write to Stream
        returnValue = true
        OUTPUT = write filename
      } // User can write to Stream
      return returnValue
    } // Try to open output file
    // -------------------------------------------------------------------
        
    void msgPrint(string msg)
    {
            OUTPUT << msg
    }
     
    void UpdateUsers(string sUserName, string sgroup)
    {
            bool saving = false
            
            if(existsGroup(sgroup))
            {
                    Group g = find(sgroup)
                    if(g == null)
                    {
                            // warn user
                            msgPrint("Failed to find group.\n")
                    }
                    else
                    {
                            User cUser = find(sUserName)
                            if(cUser == null)
                            {
                                    // warn user
                                    msgPrint("Failed to find user. '"sUserName".\n")
                            }
                            else
                            {
                                    if(!member(g,cUser))
                                    {            
                                            // add user group
                                            addMember(g, cUser)
                                            saving = true
                                    }
                            }
                    }
            }
            else
            {
                    // warn user
                    msgPrint("Group doesn't exist '"sgroup"'.\n")
            }
            
            if(saving)
            {
                    // save new user list
                    if (saveDirectory() == null) 
                    {
                       // refresh
                       if (loadDirectory() == null)
                       {
                             // inform user
                             msgPrint("User '"sUserName"'was added successfully.\n")
                       } 
                       else 
                       {
                             // warn user
                             msgPrint("Failed to load user list.\n")
                       }
                    } 
                    else 
                    {
                            // warn user
                            msgPrint("Failed to save user list.\n")
                    }
            }
    }
     
     
     
    /************************************
            MAIN
    *************************************/
    if (writeFileHeader(OUT_FILE))
    {
            OUTPUT << (dateAndTime(today) "\n")
            
            Skip AllUserList = create
            int cnt = 0
            
            User user = null 
            for user in userList do {
                    string temp = user.name
                    put(AllUserList, cnt++, temp)
            }
     
            string cuuser = null 
            for cuuser in AllUserList do {
                    UpdateUsers(cuuser, sgroup)
            }
            delete AllUserList
    }
    close OUTPUT
     
    exit_
    
    Updated on 2014-01-09T10:32:48Z at 2014-01-09T10:32:48Z by iron-man
    • llandale
      llandale
      2943 Posts
      ACCEPTED ANSWER

      Re: Add all user to a group(help needed)

      ‏2012-12-05T17:42:57Z  in response to SystemAdmin
      Your saveDirectory-loadDirectory combination..
      • .. I'm guessing may cause some timing problems; but that is just my intuition (or paranoia) talking.
      • .. I don't think you need the loadDirectory in any case
      • .. you should saveDirectory after making all the changes.
      • .. I think you need an initial loadDirectory call.

      You can put the "User" handle directly into your Skip, instead of converting and de-converting to a string name. I don't think you need the Skip in any case; you can put the user in the group inside the userList loop (you are neither creating nor deleting users).

      I don't see the problem with the original code.

      -Louie

      • close(OUTFILE) should move up inside the bracket.
      • consider showing the file to the user when done:
        • system ("notepad.exe " OUT_FILE)
      • I don't think you should want to "exit_" when done.