The below script we will have on schedule in Adaxes to run, pickup the CSV and create the user.
###START SETUP###
# Import the Adaxes module for modifying AD
Import-Module Adaxes
# Import the System.Web assembly for generating passwords
Add-Type -AssemblyName System.Web
# Path to the CSV file
$csvFilePath = "C:\HCM-IMPORT\Inbound-to-Adaxes\Inbound.csv"
# Import the CSV file sent from HCM
$importedUsers = Import-Csv -Path $csvFilePath -Delimiter '|'
###END SETUP###
###DECLARE FUNCTIONS###
# Function to check if a UPN exists
function Get-UPN {
param (
[string]$UPN
)
return Get-ADUser -Filter {UserPrincipalName -eq $UPN} -Properties UserPrincipalName
}
# Function to generate a unique UPN
function Generate-UniqueUPN {
param (
[string]$firstName,
[string]$lastName
)
$domain = "sjhmc.org"
$baseUPN = "$($lastName)$($firstName.Substring(0,1))@$domain"
$newUPN = $baseUPN
$counter = 1
while (Get-UPN -UPN $newUPN) {
$newUPN = "$($lastName)$($firstName.Substring(0,$counter+1))@$domain"
$counter++
}
return $newUPN
}
###END FUNCTIONS###
###BEGIN MAIN PROGRAM###
# Loop through each user in the CSV file
foreach ($user in $importedUsers) {
switch ($user.'Update Type') {
#Add users based on 'A' in the Update Type Column
'A' {
# Generate a unique UPN
$upn = Generate-UniqueUPN -lastName $user.'Last Name' -firstName $user.'First Name'
#Generate a Unique Password
$password = [System.Web.Security.Membership]::GeneratePassword(12, 4)
$password = ConvertTo-SecureString "$password" -AsPlainText -Force
# Add a new user
New-AdmUser -Name "$($user.'First Name') $($user.'Middle Name') $($user.'Last Name')" `
-SamAccountName $upn.Split('@')[0] `
-UserPrincipalName $upn `
-EmployeeID $user.'Employee ID' `
-Title $user.Title `
-Manager $user.Manager `
-Department $user.Department `
-Office $user.Location `
-Path "OU=SJH Users Test,DC=sjhmc,DC=sjhealthsys,DC=org" `
-Enabled $false `
-Description "Created by HCM Import"`
-AccountPassword $password -ChangePasswordAtLogon $true `
-AdaxesService "localhost"
Write-Output "Added user: $($user.'First Name') with UPN $upn"}
#Update users based on 'U' in the Update Type Column
'U' {
# Update an existing user
Set-AdmUser -Identity $user.UPN `
-GivenName $user.'First Name' `
-Surname $user.'Last Name' `
-Title $user.Title `
-Manager $user.Manager `
-Department $user.Department `
-Office $user.Location `
-AdaxesService "localhost"
Write-Output "Updated user: $($user.UPN)"
}
#Disable users based on 'T' in the Update Type Column
'T' {
# Terminate a user
Disable-AdmAccount -Identity $user.UPN.Split('@')[0] -AdaxesService "localhost"
Write-Output "Terminated user: $($user.UPN)"
}
default {
Write-Output "Unknown update type for user: $($user.'Employee ID')"
}
}
}