
gyhms (gyhms) asked a question.
- # 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!
- "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/fiddlerhttp://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/DecryptHTTPS(http://docs.telerik.com/fiddler/Configure-Fiddler/Tasks/DecryptHTTPS)2. Use a PowerShell module to call Okta, such ashttps://github.com/gabrielsroka/OktaAPI.psm1orhttps://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