private/New-RBACUser.ps1
function New-RBACUser { [CmdletBinding(SupportsShouldProcess=$true)] Param ( [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [String]$GivenName, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=1)] [String]$Surname, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [String]$Title, [Parameter(Mandatory=$true, ValueFromPipelineByPropertyName=$true, Position=2)] [String]$PhoneNumber ) BEGIN { $UsersOU = "OU={0},OU={1},{2}" -f $UsersOU, $GlobalOUStruct.Name, $GlobalOUStruct.Path $DNSDomain = (get-addomain).dnsroot } Process { $password = get-randomPassword $securePassword = $password | ConvertTo-SecureString -AsPlainText -force $userParams = @{ name = "{0}.{1}" -f $GivenName, $Surname GivenName = $GivenName SurName = $Surname samaccountName = "{0}.{1}" -f $GivenName, $Surname DisplayName = "{0} {1}" -f $GivenName, $Surname EmailAddress = "{0}.{1}@{2}" -f $GivenName, $Surname, $DNSDomain Title = $title OtherAttributes = @{ telephoneNumber = $phoneNumber } Enabled = $true Path = $usersOU AccountPassword = $securePassword UserPrincipalName = "{0}.{1}@{2}" -f $GivenName, $Surname, $DNSDomain } try { $User = new-aduser @userParams -passthru write-Host ("User '{0}' created at {1}." -f $user.userprincipalName, $userParams.path) write-host "PASSWORD: $password" } catch { throw $_ } } } |