Import-Module Adaxes
# Define the base path for the CSV file
$baseCsvPath = "C:\HCM-IMPORT\Outbound-to-HCM\"
# Function to get the timestamped filename
function Get-TimestampedFilename {
$timestamp = Get-Date -Format "yyyyMMddHHmm"
return "${baseCsvPath}OutboundUpdates_$timestamp.csv"
}
# Function to get the latest CSV file in the base directory
function Get-LatestCsvFile {
$csvFiles = Get-ChildItem -Path $baseCsvPath -Filter "OutboundUpdates_*.csv" | Sort-Object LastWriteTime -Descending
if ($csvFiles.Count -gt 0) {
return $csvFiles[0].FullName
} else {
return $null
}
}
# Function to log the modified attributes to a pipe-delimited file
function Log-ModifiedAttributes {
param (
[string]$employeeNumber,
[string]$username,
[string]$usernameAD,
[string]$email,
[string]$emailAD,
[string]$phone,
[string]$phoneAD,
[string]$mobile,
[string]$mobileAD
)
# Check if the file exists, if not create it with a timestamped filename
$csvPath = Get-LatestCsvFile
if (-not $csvPath) {
$csvPath = Get-TimestampedFilename
# Add header to the new file
"EmployeeNumber|Username|UsernameAD|Email|EmailAD|Phone|PhoneAD|Mobile|MobileAD" | Out-File -FilePath $csvPath
}
# Create a custom object with the modified attributes
$modifiedUser = [PSCustomObject]@{
EmployeeNumber = $employeeNumber
Username = $username
UsernameAD = $usernameAD
Email = $email
EmailAD = $emailAD
Phone = $phone
PhoneAD = $phoneAD
Mobile = $mobile
MobileAD = $mobileAD
}
# Convert the custom object to a pipe-delimited string
$pipeDelimitedString = $modifiedUser.PSObject.Properties.Value -join '|'
# Append the pipe-delimited string to the file
Add-Content -Path $csvPath -Value $pipeDelimitedString
}
# Get the modified user object
$user = $Context.TargetObject
# Retrieve the employeeID directly from the user object
$employeeNumber = $user.Get("employeeID")
# Check if the properties are modified
$username = if ($Context.IsPropertyModified("userPrincipalName")) { $Context.GetModifiedPropertyValue("userPrincipalName") } else { $null }
$email = if ($Context.IsPropertyModified("mail")) { $Context.GetModifiedPropertyValue("mail") } else { $null }
$phone = if ($Context.IsPropertyModified("telephoneNumber")) { $Context.GetModifiedPropertyValue("telephoneNumber") } else { $null }
$mobile = if ($Context.IsPropertyModified("mobile")) { $Context.GetModifiedPropertyValue("mobile") } else { $null }
# Set the AD fields to 'A' for Add or Update if the properties are modified
$usernameAD = if ($username) { "A" } else { $null }
$emailAD = if ($email) { "A" } else { $null }
$phoneAD = if ($phone) { "A" } else { $null }
$mobileAD = if ($mobile) { "A" } else { $null }
# Log the modified attributes to the pipe-delimited file only if at least one property is modified
if ($usernameAD -or $emailAD -or $phoneAD -or $mobileAD) {
Log-ModifiedAttributes -employeeNumber $employeeNumber -username $username -usernameAD $usernameAD -email $email -emailAD $emailAD -phone $phone -phoneAD $phoneAD -mobile $mobile -mobileAD $mobileAD
}