Adding users to user groups with PowerShell script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ05-25-2022
01:08 PM
- last edited on
โ05-25-2022
01:23 PM
by
Ben_Garrison
Hi everyone,
I recently started using JumpCloud as my identity provider and using it with SSO on other platforms.
I want to extract email addresses as a CSV from my other platforms, compare them with the email addresses of the users in my JumpCloud admin console, and designate users with matching email addresses to a designated user group.
I started writing a script in PowerShell with a loop that connects to JumpCloud with API and imports a local CSV file and extracts only email addresses, but I got a bit stuck since I still don't know much about JumpCloud PowerShell module.
I will be happy for some assistance regarding the matter.
Tanks ๐
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
โ05-31-2022 02:35 PM
Hey @galr !
Took a stab at this but not sure how you're looking to do your group sorting. Went on the assumption that your matching based on a field in your CSV like Department, but really just wanted to showcase how to do logic matching with a switch statement. Take a look and let me know if you have any questions or hit any roadblocks!
# Connectong to the JumpCloud Admin console with API key
$JumpCloudAPIKey = "YOUR_API_KEY"
#Force parameter used to auth to JumpCloud API without update check
Import-Module JumpCloud
Connect-JCOnline -JumpCloudAPIKey $JumpCloudAPIKey -force
# Import our CSV from other systems
$user_list = Import-Csv -Path "C:\Users\galr\Downloads\export_users.csv"
# Get the list of users from JumpCloud
$jumpcloudUsers = Get-JCUser -returnProperties email, firstname, lastname, username
foreach ($usr in $user_list) {
if ($usr.email -in $jumpcloudUsers.email) {
Write-Host "Matching user found in JumpCloud:" $usr.email -ForegroundColor Green
$jcUsername = $jumpcloudUsers | Where-Object {$_.email -eq $usr.email} | Select-Object -ExpandProperty username
# Start matching user
switch ($usr) {
{$_.Department -like "*Marketing*"} {
# Add to Marketing group
Add-JCUserGroupMember -Username $jcUsername -GroupName "Marketing"
}
{$_.Department -eq "Sales"} {
# Add to Sales group
Add-JCUserGroupMember -Username $jcUsername -GroupName "Sales"
}
{$_.Department -like "*Support*"} {
# Add to Support group
Add-JCUserGroupMember -Username $jcUsername -GroupName "Support"
}
Default {
Write-Host "No group found for user:" $usr.email -ForegroundColor Yellow
}
}
} else {
Write-Host "No matching user found in JumpCloud:" $usr.email -ForegroundColor Red
}
}
![](/skins/images/1519CD93B86343CA58368DF3BFFB373D/responsive_peak/images/icon_anonymous_message.png)
![](/skins/images/1519CD93B86343CA58368DF3BFFB373D/responsive_peak/images/icon_anonymous_message.png)