Powershell script for adding groups to applications? (Migrating domains) Skip to main content
https://support.okta.com/help/answers?id=9060z000000jjcqqac&refurl=http%3a%2f%2fsupport.okta.com%2fhelp%2fanswers
How satisfied are you with the Okta Help Center?
Thank you for your feedback!
How satisfied are you with the Okta Help Center?
1
2
3
4
5
Very Dissatisfied
Very satisfied
Enter content less than 200 characters.
Ask Search:
Paige SchellerPaige Scheller 

Powershell script for adding groups to applications? (Migrating domains)

So, we're migrating domains and I would like to add the equivalent groups from the new domain to the apps the groups from the old domain are already assigned... I've got this script together and logically it makes sense... I have a csv file with the appId and groupId I want assigned to that app. However, running the script gives me a 404 error. Can someone point out what it appears that I'm doing wrong with it?:
# Define headers for web request
$headers = @{"Authorization" = "SSWS <token>"; "Accept" = "application/json"; "Content-Type" = "application/json"}

# Import .csv from the location on computer
$csv = Import-Csv <location of .csv file> -Header "applicationId","groupId"

# This allows the variables to be pulled. The top of each column in the .csv needs to correspond with these line mapping names. The names in this script are applicationId and groupId.
foreach ($line in $csv) {
         try {
            # Add the group to the app using the groupId and stop processing if it fails
            $result = Invoke-WebRequest -Headers $headers -Method Put -uri "https://<company>.okta.com/api/v1/apps/$line.applicationId/groups/$line.groupId" -ErrorAction:Stop

            # Write message if adding the group to the app was successful
            if ( $result.StatusCode -eq 204 ) { Write-Output "Successfully added $($line.groupId)" }
        } catch {
            # Write message if adding the group to the app fails
            Write-Output "Failed adding group $($line.groupId) to app $($line.applicationId) - error: $($_.Exception.Response.StatusCode.Value__)"
        }
}
Thanks!
 
Best Answer chosen by Paige Scheller
Gabriel SrokaGabriel Sroka (Okta, Inc.)
Hi Paige
On line 11, try changing
"https://<company>.okta.com/api/v1/apps/$line.applicationId/groups/$line.groupId"

to
"https://<company>.okta.com/api/v1/apps/$($line.applicationId)/groups/$($line.groupId)"

adding parentheses, just like you did on lines 14 and 17​.

May I also recommend a few tips that I find helpful:
1. Use Fiddler to help troubleshoot (you'll have to set it up to use HTTPS): 
https://www.telerik.com/fiddler
http://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/DecryptHTTPS
2. Use a PowerShell module to call Okta, such as
https://github.com/gabrielsroka/OktaAPI.psm1
or
https://github.com/mbegan/Okta-PSModule

My module has function called Add-OktaAppGroup that you could call like this:
Add-OktaAppGroup $line.applicationId $line.groupId

Let us know how that works out.

Thanks,

Gabriel Sroka

All Answers

Gabriel SrokaGabriel Sroka (Okta, Inc.)
Hi Paige
On line 11, try changing
"https://<company>.okta.com/api/v1/apps/$line.applicationId/groups/$line.groupId"

to
"https://<company>.okta.com/api/v1/apps/$($line.applicationId)/groups/$($line.groupId)"

adding parentheses, just like you did on lines 14 and 17​.

May I also recommend a few tips that I find helpful:
1. Use Fiddler to help troubleshoot (you'll have to set it up to use HTTPS): 
https://www.telerik.com/fiddler
http://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/DecryptHTTPS
2. Use a PowerShell module to call Okta, such as
https://github.com/gabrielsroka/OktaAPI.psm1
or
https://github.com/mbegan/Okta-PSModule

My module has function called Add-OktaAppGroup that you could call like this:
Add-OktaAppGroup $line.applicationId $line.groupId

Let us know how that works out.

Thanks,

Gabriel Sroka
This was selected as the best answer
Dylann FezeuDylann Fezeu (Customer First Programs)
Hello Paige,

Thanks for posting your inquiry in Okta Community Portal.

If you receive a great answer to your question(s), please help readers find it by marking it the best answer. Hover over the answer and click "Best Answer." 

Thank you,

Dylann Fezeu
Okta Help Center Team
Paige SchellerPaige Scheller
Gabriel--- you got it! Worked out great! Also, thank you for introducing me to Fiddler!!! So useful!