A cloud-based identity and access management service for securing user authentication and resource access
Hi Gagan,
I'm assuming "IDM" and the second role are app roles on your Jenkins enterprise application (the roles you see under Enterprise applications → Jenkins → Users and groups → Assign). If they're something else, let me know.
With Microsoft Graph PowerShell, one script can create each user from the CSV, or reuse the user if it already exists, and assign both roles.
users.csv
DisplayName,UserPrincipalName,MailNickname
Jenkins User 1,******@contoso.com,jenkins.user1
Jenkins User 2,******@contoso.com,jenkins.user2
Script (run as a User Administrator plus an Application Administrator or Cloud Application Administrator)
Connect-MgGraph -Scopes "User.ReadWrite.All","AppRoleAssignment.ReadWrite.All","Application.Read.All"
$appName = "Jenkins" # enterprise app display name
$roleNames = @("IDM", "Developer") # app role display names to assign
$sp = Get-MgServicePrincipal -Filter "displayName eq '$appName'"
$roles = $sp.AppRoles | Where-Object { $_.DisplayName -in $roleNames }
foreach ($row in Import-Csv .\users.csv) {
$user = Get-MgUser -Filter "userPrincipalName eq '$($row.UserPrincipalName)'" -ErrorAction SilentlyContinue
if (-not $user) {
$user = New-MgUser -DisplayName $row.DisplayName -UserPrincipalName $row.UserPrincipalName `
-MailNickname $row.MailNickname -AccountEnabled `
-PasswordProfile @{ Password = "<REDACTED-random-initial-value>"; ForceChangePasswordNextSignIn = $true }
}
foreach ($role in $roles) {
New-MgServicePrincipalAppRoleAssignedTo -ServicePrincipalId $sp.Id `
-PrincipalId $user.Id -ResourceId $sp.Id -AppRoleId $role.Id | Out-Null
Write-Host "$($row.UserPrincipalName) -> $($role.DisplayName)"
}
}
Replace the placeholder with a randomly generated initial value, and give users their first sign-in through SSPR or a Temporary Access Pass rather than sending passwords around.
If you have Entra ID P1, there's a simpler long-term option: assign both roles once to a group such as Jenkins-IDM, and have the script only add users to that group (New-MgGroupMember).