0 Replies Latest reply on Feb 24, 2020 12:19 PM by madhuri.tanniru

    Create a Active Directory Group and set default site role using REST API

    madhuri.tanniru

      Hi,

       

      I am trying to get AD groups from ActiveDirectory and add them to the site using REST API.

       

      The following is what I have and I am getting 400 error. Can someone please look into it and let me know what I am doing wrong here

       

       

      # Get AD Groups that starts has Tableau

       

      Get-ADGroup -Filter "Name -like '*Tableau*'" | export-csv -Path c:\scripts\ADGroups.csv -NoTypeInformation

       

      $path = "http://TableauServer/api/2.2/"

      $usernameTableau = ""

      $passwordTableau = ""

      $site = "Test1"

       

      Function signInToASite ([string] $uname, [string] $pwd, [string] $site){

       

          # generate body for initial sign in

           $signin_body = (

              ’<tsRequest>

                <credentials name=“’ + $uname + ’” password=“’+ $pwd + ’” >

                  <site contentUrl= “’ + $site + ’”/>

                </credentials>

              </tsRequest>’)

       

          $signin_uri = "$path"+"auth/signin"

          #write-host  $signin_uri

       

              $response = Invoke-RestMethod -uri $signin_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

              $myUserID = $response.tsResponse.credentials.user.id

              #write-host $signin_uri

              #write-host "authorization token : " $authtoken

              #write-host "SiteID : " $siteID

              #write-host "User ID  : " $myUserID

              return $authtoken, $siteID, $myUserID        

      }

       

      $auth,$siteid, $myId  = signInToASite $usernameTableau $passwordTableau $site

      write-host $auth,$siteid, $myId

       

      # Creates AD group in Tableau Server

       

      Function addGroupToSite([string] $newGrpName , [string] $siteID

                                  , [string] $siteRole , [string] $aToken){

       

          # set up header fields with auth token

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

          # add X-Tableau-Auth header with our auth token

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

       

          $uri = "$path"+"sites/$siteid/groups"

       

          $reqBody = (’<tsRequest>

                              <group name=“’ + $newGrpName + ’” >

                                 <import source=“ActiveDirectory”

                                      domainName=“test.int”

                                      siteRole=“Viewer” />

                              </group>

                       </tsRequest>’)

       

          write-host $reqBody

       

          $response = Invoke-RestMethod -Uri $uri -Headers $headers -Body $reqBody -Method Post

       

          $newGrpId = $response.tsResponse.group.id

       

          write-host $newGrpId

       

          return $newGrpId

      }

       

      $siteRole = "Viewer"

       

      $arrGroups = (Import-csv c:\scripts\ADGroups.csv).Name

       

      #write-host $arrGroups.Name[0]

       

      Foreach ($newGrp in $arrGroups) {

           write-host $newGrp

           $newGrpId = addGroupToSite $newGrp $siteid $siteRole $auth

       

           write-host $newGrpId   

      }

       

       

      The following is the error I am receiving

       

       

       

      Thanks

      Madhuri