
vir8k (vir8k) asked a question.
Having worked with PowerShell and Okta I can get group members and list groups but would like some help with taking group members from a group and adding them into another group. Passing the variables like I normally do with AD PowerShell doesn't work.

I should be able to modify something like this to read groups based on a filter and then add them to another group based on a different filter.
function Export-UsersAndGroups() {
$totalUsers = 0
$exportedUsers = @()
# for more filters, see https://developer.okta.com/docs/api/resources/users#list-users-with-a-filter
$params = @{filter = 'status eq "ACTIVE"'}
do {
$page = Get-OktaUsers @params
$users = $page.objects
foreach ($user in $users) {
$userGroups = Get-OktaUserGroups $user.id
$groups = @()
foreach ($userGroup in $userGroups) {
if ($userGroup.type -eq "OKTA_GROUP") {
$groups += $userGroup.profile.name
}
}
$exportedUsers += [PSCustomObject]@{id = $user.id; login = $user.profile.login; groups = $groups -join ";"}
}
$totalUsers += $users.count
$params = @{url = $page.nextUrl}
} while ($page.nextUrl)
$exportedUsers | Export-Csv c:\users\jspit\desktop\exportedUsersGroups.csv -notype
"$totalUsers users exported."
}
Hello @vir8k (vir8k),
Please check the following link with information: https://help.okta.com/en/prod/Content/Topics/users-groups-profiles/usgp-create-group-rules.htm
Also, feel free to post this question on our Okta Developer Forums: https://devforum.okta.com,
and they should be able to help you with this.
Regards,
Natalia
Okta Inc.
Thanks. I will move the question over and add some clarity to it
Also the group's rules are great but I need to do this to about 130 groups. Not untenable there but I want to expand my powershell library to perform these big functions. Thanks
Based on the API docs, it appears that you can't add multiple users to a group at once, only one at a time. Retrieve or build your array of users to add, then use a foreach loop to add them to the group one by one.
Here is what I have now. It doesn't work. I can create arrays just trying to figure out how to fit that into the code where it doesn't just create a giant list of all the users from all the first set of groups and dump them into all of the second set of groups.
function get-oktaInvokeGroupMembers () {
$groups = Invoke-Method GET "/api/v1/groups?filter=type+eq+%22APP_GROUP%22&q=Test"
Write-Output $groups
foreach ($group in $groups) {
$members = Get-OktaGroupMember $group.id
Write-Output $members
}
$oktagroups = Invoke-Method GET "/api/v1/groups?filter=type+eq+%22OKTA_GROUP%22&q=Test"
Write-Output $oktagroups
foreach ($okta in $oktagroups) {
$oktamembers = Get-OktaGroupMember $user.id
PUT "/api/v1/$okta.id/users/$user.id"
}
}
Can you hit me with an example of gathering an array for the users from each group? I'll work on some stuff on this end and see if I can get there because I do believe you are correct, I'm just used to working with AD/Powershell and not API's