Public/New-AdUserFromSyncHr.ps1
function New-AdUserFromSyncHr { [CmdletBinding(PositionalBinding = $true)] param ( [Parameter(Mandatory = $true)][object]$SyncHrNewHire, [Parameter(Mandatory = $true)][string]$NewHireOu, [Parameter(Mandatory = $true)][string]$NewHireDomainPrimary, [Parameter(Mandatory = $true)][string[]]$NewHireDomainProxys, [Parameter(Mandatory = $true)][string]$NewHireCompanyName ) $returnObj = New-Object -TypeName psobject -Property @{ Result = '' } $username = $SyncHrNewHire.fName + "." + $SyncHrNewHire.lname $name = $SyncHrNewHire.fName + " " + $SyncHrNewHire.lname $newEmpNumber = Convert-SyncHrEmpNo -SyncHrEmpNo $SyncHrNewHire.empNo $password = $($SyncHrNewHire.fName).SubString(0, 1) + $($SyncHrNewHire.lname).SubString(0, 1) + "#" + $newEmpNumber $password_ss = ConvertTo-SecureString $password -AsPlainText -Force $newEmail = "$username@$NewHireDomain" $manager = $null if ($SyncHrNewHire.manager_empNo.Length -gt 3) { $managerEmployeeNumber = Convert-SyncHrEmpNo -SyncHrEmpNo $emp.manager_empNo try { $manager = Get-ADUser -Filter { EmployeeNumber -eq $managerEmployeeNumber } -Properties manager -ErrorAction: Ignore | Select-Object -First 1 } catch { } } try { $newUserHash = @{ EmployeeNumber = $newEmpNumber SamAccountName = $username UserPrincipalName = $newEmail Email = $newEmail Name = $name DisplayName = $name GivenName = $SyncHrNewHire.fname Surname = $SyncHrNewHire.lname Enabled = $True ChangePasswordAtLogon = $True Title = $SyncHrNewHire.positionTitle Path = $NewHireOu Manager = $manager.DistinguishedName Office = $SyncHrNewHire.location_name StreetAddress = $SyncHrNewHire.location_street City = $SyncHrNewHire.location_city State = $SyncHrNewHire.location_state PostalCode = $SyncHrNewHire.location_zip Company = $NewHireCompanyName AccountPassword = $password_ss } Write-Log -LogText "Creating new user: $($newUserHash | ConvertTo-Json -Compress)" New-ADUser @newUserHash Start-Sleep -Seconds 3 $newUser = Get-AdUser $username -Properties Office, EmployeeNumber -ErrorAction Stop $returnObj | Add-Member -MemberType: NoteProperty -Name SamAccountName -Value $newUser.SamAccountName $returnObj | Add-Member -MemberType: NoteProperty -Name Office -Value $newUser.Office $returnObj | Add-Member -MemberType: NoteProperty -Name EmployeeNumber -Value $newUser.EmployeeNumber $returnObj | Add-Member -MemberType: NoteProperty -Name newPassword -Value $password $returnObj | Add-Member -MemberType: NoteProperty -Name managerName -Value $manager.Name } catch { Write-Log "Error with New-AdUser. SynrHr user: $name ($($SyncHrNewHire.empNo))" -LogType: error -ErrorObject $_ $returnObj.Result = "Error: $($_.Exception.Message)" } $proxyAddresses = @( "SMTP:$newEmail" ) foreach ($proxy in $NewHireDomainProxys) { $proxyAddresses += "smtp:$proxy" } try { #Set new user's proxyAddresses Set-ADUser -Identity $username -Add @{ProxyAddresses = $proxyAddresses } } catch { Write-Log "Error with Set-AdUser. SynrHr user: $name ($($SyncHrNewHire.empNo)). Command: ""Set-ADUser -Identity $username -Add @{ProxyAddresses = $proxyAddresses}""" -LogType: error -ErrorObject $_ } try { Add-ADGroupMember -Identity $SyncHrNewHire.positionTitle -Members $username } catch { Write-Log "Error with Add-ADGroupMember. SynrHr user: $name ($($SyncHrNewHire.empNo)). Command: ""Add-ADGroupMember -Identity $($SyncHrNewHire.positionTitle) -Members $username""" -LogType: error -ErrorObject $_ } return $returnObj } |