Advertisement
pbmmc

CreateNewAADUser&DuplicateGroups_v1.ps1

Jul 16th, 2024 (edited)
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PowerShell 3.14 KB | Source Code | 0 0
  1. # import the Azure Active Directory module in order to be able to use Get-AzureADUserMembership and Add-AzureADGroupMember cmdlet
  2. import-Module AzureAD
  3.  
  4. Connect-AzureAD
  5. Connect-MgGraph -Scopes User.ReadWrite.All, Organization.Read.All
  6.  
  7. function Get-RandomPassword {
  8.     param (
  9.         [Parameter(Mandatory)]
  10.         [int] $length,
  11.         [int] $amountOfNonAlphanumeric = 1
  12.     )
  13.     Add-Type -AssemblyName 'System.Web'
  14.     return [System.Web.Security.Membership]::GeneratePassword($length, $amountOfNonAlphanumeric)
  15. }
  16.  
  17. # enter login name of the first user
  18. $userTemplate = Read-host "Enter username@domain to copy from: "
  19.  
  20. # Get ObjectId based on username of user to copy from
  21. $userTemplateObj = Get-AzureADUser -ObjectID $userTemplate
  22.  
  23. # Get new user info
  24. $fName = Read-host "Enter the new staff FIRST name: "
  25. $lName = Read-host "Enter the new staff LAST Name: "
  26. $usernameNew = "$($fName.Substring(0, [Math]::Min($fName.Length, 1)))$($lName)"
  27. $userEmailNew = "$($usernameNew)@domain"
  28.  
  29. $answer = read-host "Create new user $($userEmailNew) based on $($$userTemplate)? [Y] or [N]?"
  30.  
  31. if ($answer -eq 'Y') {
  32.     $jobTitle = $userTemplateObj.JobTitle
  33.     $showInAddressList = $true
  34.    
  35.     #Do not show the user in GAL if <dept>
  36.     if($jobTitle -Match "<dept>"){ $showInAddressList = $false }
  37.    
  38.     $PasswordProfile = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordProfile
  39.     $PasswordProfile.Password = Get-RandomPassword 12
  40.        
  41.     #Create user in AAD
  42.     New-AzureADUser -DisplayName "$($fName ) $($lName)" -PasswordProfile $PasswordProfile -UserPrincipalName "$($userEmailNew)" -AccountEnabled $true -MailNickName "$($usernameNew)" -JobTitle "$($jobTitle)" -ShowInAddressList $showInAddressList
  43.    
  44.     #Set properties
  45.     $manager = Get-AzureADUserManager -ObjectId $userTemplate
  46.     Set-AzureADUserManager -ObjectId $userEmailNew -RefObjectId $manager
  47.    
  48.     #Assign the new user the licenses from the template user
  49.     $mgUser = Get-MgUser -UserId "$($userTemplate)"
  50.     Set-MgUserLicense -UserId "$($userEmailNew)" -AddLicenses $mgUser.AssignedLicenses -RemoveLicenses @()
  51.    
  52.     #Get Dynamic groups to skip in the next step to avoid errors
  53.     $dynamicGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'DynamicMembership')" -All:$true
  54.    
  55.     #Add the member to the groups
  56.     $membershipGroups = Get-AzureADUserMembership -ObjectId $userTemplateObj.ObjectId
  57.  
  58.     Write-Host "\-- Groups available to copy from" $userTemplate to $userEmailNew "--\" -ForegroundColor Yellow
  59.  
  60.     foreach($group in $membershipGroups) {
  61.         $isGroup = $true
  62.         foreach ($dgroup in $dynamicGroups){
  63.             if ($group.DisplayName -eq $dgroup.DisplayName){
  64.                 Write-Host "[!] - Skipping dynamic group " $dgroup.DisplayName " ... " -ForegroundColor Yellow
  65.                 $isGroup = $false
  66.             }
  67.         }
  68.         if ($isGroup){
  69.             Write-Host $group.DisplayName
  70.             Write-Host "[!] - Adding" $userEmailNew " to " $group.DisplayName "... " -ForegroundColor Green -nonewline
  71.             Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId $userEmailNew
  72.             Write-Host "Done"  
  73.         }
  74.     }
  75.     Write-Host "The temporary password for user: $($userEmailNew) is: $($PasswordProfile.Password)"
  76. }
  77. else {
  78.     Write-Host "Cancelling..."
  79.     return
  80. }
  81.  
  82.  
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement