Advertisement
mfgwicked

New AD user

Dec 17th, 2024
1,209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function New-CADUser {
  2. <#
  3. .SYNOPSIS
  4. Calls an advanced function to streamline New-ADUser creation by individual user or multiple users.
  5.  
  6. .DESCRIPTION
  7. This CMDlet is designed to prompt for each required parameter field dictated by the Domain Admin. It is also designed to take input by csv.
  8. It also adds the parameter 'Groups' and 'SourceUser' which will allow you to copy the memberships from a Source User, or add a specific Group(s). The password for the account is supplied by a random password generator and is shown in the console and copied to the clipboard.
  9.  
  10. .PARAMETER FirstName
  11. Passes the Firstname to Given Name, Name, and SamAccountName
  12.  
  13. .PARAMETER LastName
  14. Passes LastName to Surname, Name, and SamAccountName
  15.  
  16. .PARAMETER SourceUser
  17. Passes details from the source user account like the Path, Title, Email and Department to directly place them into the new users info.
  18.  
  19. .PARAMETER Groups
  20. Passes specific groups to be added to the new user that are outside the scope of the Sourceuser.
  21.  
  22. .EXAMPLE
  23. Will prompt each mandatory parameter (which is all except Groups).
  24.  
  25. PS> New-CADUser
  26.  
  27. .EXAMPLE
  28. Add multiple users from a CSV with all mandatory parameters included.
  29.  
  30. PS> Import-CSV .\users.csv | foreach-object {New-CADUser}
  31.  
  32.  
  33. #>
  34.     [CMDletBinding(SupportsShouldProcess)]
  35.     param(
  36.         [Parameter(
  37.                 Mandatory=$True,
  38.                 ValueFromPipeline=$True,
  39.                 ValueFromPipelinebyPropertyName=$True
  40.         )]
  41.         [string]$FirstName,
  42.         [Parameter(
  43.                 Mandatory=$True,
  44.                 ValueFromPipeline=$True,
  45.                 ValueFromPipelinebyPropertyName=$True
  46.         )]
  47.         [string]$LastName,
  48.         [Parameter(
  49.                 Mandatory=$True,
  50.                 ValueFromPipeline=$True,
  51.                 ValueFromPipelinebyPropertyName=$True
  52.         )]
  53.         [string]$SourceUser,
  54.         [Parameter(
  55.                 Mandatory=$False,
  56.                 ValueFromPipeline=$True,
  57.                 ValueFromPipelinebyPropertyName=$True
  58.         )]
  59.         [array]$Groups
  60.     )
  61.         try {
  62.                 $spw = Invoke-RandomPassword -Length 10 | Tee-Object -Variable pw | ConvertTo-SecureString -AsPlainText -Force #Converts the pw to a securestring
  63.                 #
  64.                 $SourceUserInfo = Get-ADUser -Identity $SourceUser -Properties Title,Department,Emailaddress #Applies the SourceUserInfo to progagate the Title, Department, and Path.
  65.                 #
  66.                 $SourceDistinguishedName = (($SourceUserInfo.Distinguishedname).split(',')) #Calls the DistinguishedName of the SourceUser to a variable and splits each section into objects
  67.                 #
  68.                 $First, $Rest = $SourceDistinguishedName #assigns the CN entry to the first variable and assigns the rest to the rest variable
  69.                 #
  70.                 $Path = $Rest -join ',' #loads the remaining objects and rejoins them to use as a path for the new user
  71.                 #
  72.                 $FirstLast = $FirstName[0] + $LastName #Joins the first letter of firstname and lastname
  73.                 #
  74.  
  75.                 $userparam = @{
  76.                         Name            = $FirstLast
  77.                         SamAccountName  = $FirstLast
  78.                         GivenName       = $FirstName
  79.                         Surname         = $LastName
  80.                         Title           = $SourceUserInfo.Title
  81.                         Department      = $SourceUserInfo.Department
  82.                         Path            = $Path
  83.                         Email           = "$($FirstLast)@$($SourceUserinfo.Emailaddress.split('@')[1])"
  84.                         AccountPassword = $spw
  85.                         Enabled         = $true
  86.                 }
  87.                 #
  88.                 $NewUser = New-ADUser @userParam -ErrorAction Stop #Actual use of New-ADUser with all parameters
  89.                 Write-Host "Created user account for '$FirstLast'"
  90.                 $Sourceusergroups = Get-ADPrincipalGroupMembership -Identity $SourceUser | Select-Object -ExpandProperty SamAccountName | Where-Object -FilterScript {"$_ -notlike 'Domain Users'"} #Creates a joined string of all of the groups the SourceUser is a member of.
  91.                 foreach ($sourceusergroup in $sourceusergroups) {
  92.                         try {
  93.                                 Add-ADPrincipalGroupMembership -Identity $FirstLast -MemberOf $_ -ErrorAction Continue
  94.                                 Write-Verbose "Group '$Sourceusergroup' added to user '$FirstLast' from the source user '$sourceuser'."
  95.                         } #adds groups from the source user
  96.                         catch {
  97.                                 Write-Error "Unable to add group '$sourceusergroup' to user '$FirstLast' from source user '$sourceuser'. : $_"
  98.                         }
  99.                 }
  100.                 #
  101.                 if ($Groups) { #checks for values in groups#
  102.                         foreach ($group in $groups) {
  103.                                 try {
  104.                                         Add-ADPrincipalGroupMembership -Identity $FirstLast -MemberOf $Group -ErrorAction Continue #allows for seperatre groups to be added
  105.                                         Write-Verbose "Group '$group' added to user '$FirstLast'."
  106.                                 }
  107.                                 catch {
  108.                                         Write-Error "Unable to add group '$group' to user '$FirstLast'. : $_"
  109.                                 }
  110.                         }
  111.                 }
  112.         }
  113.         catch {
  114.                 Write-Error "Unable to create user account for '$FirstLast'. : $_"
  115.         }
  116.         finally {
  117.                 Write-Output $NewUser # writes the output of the user properties
  118.                 $pw | clip #passes the password to the clipboard
  119.         }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement