cancel
Showing results for 
Search instead for 
Did you mean: 

Adding users to user groups with PowerShell script

galr
Novitiate I

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 🙂 

 
---------------------------------------------------------------------------------------------------------------------------------------------

# Connectong to the JumpCloud Admin console with API key
$JumpCloudAPIKey = "API-KEY"

Import-Module JumpCloud

#Force parameter used to auth to JumpCloud API without update check
Connect-JCOnline -JumpCloudAPIKey $JumpCloudAPIKey -force


$user_list = Import-Csv -Path "C:\Users\galr\Downloads\export_users.csv"


foreach($usr in $user_list)
{  

    $usr.email

}
1 REPLY 1

RyanBailey
Novitiate III

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
    }
}