3 Replies Latest reply on Oct 31, 2017 7:21 AM by Elizabeth Shelley

    Automatically add all Site Admins to a user group

    Elizabeth Shelley

      Hi, we have built a custom server report using Tableau's database, and what we'd like to do is have something running that will automatically add all Site Admins on our server to a particular user group on the site where we've published this report. I was thinking that we should be able to make use of one of Tableau's APIs (I was reading up on the REST API, but I'm not sure if this is the right one?) but to be honest I've never done anything with API's before so I'm not sure how we would do this. Or at least I somewhat get how we could update the user group, but I don't know how we get the list of users to add to it?  (I'm thinking also - that as Site Admins change over time, we would want to delete all users from the group and re-add them when this process runs?)

       

      Thanks in advance for any help

        • 1. Re: Automatically add all Site Admins to a user group
          Jeff Strauss

          One possible option is to leverage tabcmd addusers and place this into a script that is triggered via task scheduler   tabcmd Commands

           

          What you need to do first though is query the database to get a list of all siteusers, then use tabcmd export to get this into a csv format that then will flow into the addusers.

          • 2. Re: Automatically add all Site Admins to a user group
            Glen Robinson

            Hi Elizabeth

            Below is some powershell scripting, which uses REST API to create a group "SiteAdmins" and add all users on that site who have that role to the group

            You will need to update server name, user , site,  etc

            The script assumes that the group doesnt exist, and will fail if it does. Could add some code to check for group, if that is useful to you..

            to run, save the script as *.ps1, and run in powershell on a windows machine.

             

            Anyway, hope this helps

            All the best

             

            Glen

             

             

            $server = "http://localhost"

            $username = "glen"

            $password = "password"

            $siteID =""

             

            # Login to Server

            # generate body for sign in

             

             

            $signin_body = (’<tsRequest>

              <credentials name=“’ + $username + ’” password=“’+ $password + ’” >

               <site contentUrl="’ + $siteID +’"/>

              </credentials>

            </tsRequest>’)

             

            $Uri = "$server/api/2.7/auth/signin"

            $response = Invoke-RestMethod -Uri $uri -Body $signin_body -Method Post 

             

            # get the auth token, site id and my user id

            $authToken = $response.tsResponse.credentials.token

            $siteID = $response.tsResponse.credentials.site.id

             

            $headers = New-Object “System.Collections.Generic.Dictionary[[String],[String]]”

            $headers.Add(“X-Tableau-Auth”, $authToken)

            $authToken

            $siteID

            $GroupName = "SiteAdmins"

            #Create SiteAdminsGroup

            $body = ('<tsRequest><group name="' + $GroupName +  '" /></tsRequest>')

            $response = Invoke-RestMethod -Uri $server/api/$api_ver/sites/$siteID/groups -Headers $headers -Method POST -Body $body

            $GroupID = $response.tsResponse.group.id

            $GroupID

             

            #List Users who are Site Admins

            $response = Invoke-RestMethod -Uri $server/api/$api_ver/sites/$siteID/users?filter=siteRole:eq:SiteAdministrator -Headers $headers -Method Get

             

            ForEach ($user in $response.tsResponse.Users.User)

              {

               $user.ID

               #Add users to Group

               $body = ('<tsRequest><user id="' + $user.ID +  '" /></tsRequest>')

               $response = Invoke-RestMethod -Uri $server/api/$api_ver/sites/$siteID/groups/$GroupID/users -Headers $headers -Method POST -Body $body

               $response.tsResponse.user

              }  

             

            # Sign Out of Server

            $response = Invoke-RestMethod -Uri $server/api/2.7/auth/signout -Headers $headers -Method Post

            • 3. Re: Automatically add all Site Admins to a user group
              Elizabeth Shelley

              Thanks both, will have a chat internally to see which way we want to proceed.