Skip to content

Instantly share code, notes, and snippets.

@Krovikan-Vamp
Last active June 5, 2024 15:47
Show Gist options
  • Save Krovikan-Vamp/1f60e990dfa7176523e0d5c02efb2bb5 to your computer and use it in GitHub Desktop.
Save Krovikan-Vamp/1f60e990dfa7176523e0d5c02efb2bb5 to your computer and use it in GitHub Desktop.
AD User attribute updater script.
Function Write-Message([String]$Message) { Add-Content -Path YOUR_LOG_FILE.log $Message }
Function UserUpdate($reset_logs, $max_users_count, $csvFileName) {
# Delete log files if they exist
if ($reset_logs -eq $true) {
if (Test-Path "*.log") {
Remove-Item "*.log"
}
}
# Import the CSV file
$users = Import-Csv $csvFileName -Delimiter "," -Encoding "UTF8"
$startDate = Get-Date -Format "dd/MM/yyyy [HH:mm:ss]"
Write-Output $max_users_count
if ($null -eq $max_users_count) { # If max_users_count is not provided, update all users
$max_users_count = $users.Count
}
Write-Message("============ Starting user properties update $startDate ============")
Write-Message("$startDate`: (DEBUG) Uploading $max_users_count of $($users.Count) users")
foreach ($user in $users[0..($max_users_count - 1)]) {
$currentTime = Get-Date -Format "dd/MM/yyyy [HH:mm:ss]"
$userPrincipalName = $user.EMAIL -split "@" | Select-Object -First 1
try {
Write-Message("$currentTime`: (INFO) Processing $($user.NAME) ($userPrincipalName)")
# Uncomment below to update user properties
# Set-ADUser $userPrincipalName -Title $user.POSITION -OfficePhone $user.OFFICE_NUMBER -StreetAddress $user.LOCATION -Mobile $user.DIRECT_NUMBER -Fax $user.OFFICE_FAX
# Set-ADUser $userPrincipalName -PasswordNeverExpires $true
# Acquire the user's full name (DisplayName)
$userFullName = (Get-ADUser -Identity $userPrincipalName -Properties *).DisplayName
$userEmail = (Get-ADUser -Identity $userPrincipalName -Properties *).EmailAddress
$userProxyEmail = (Get-ADUser -Identity $userPrincipalName -Properties *).ProxyAddresses
Write-Output "$userFullName`: ($userEmail), proxies: $userProxyEmail"
# Split the full name into first name, middle name, and last name
$splitName = $userFullName -split " " # This takes care of the middle name [firstname, ..., lastname]
if ($splitName.length -gt 2) {
Write-Output "Manual intervention required for $userFullName"
Write-Message "$currentTime`: (WARN) Manual intervention required for $userFullName. Multiple names detected."
} else {
$combinedNewProxy = "$($splitName[0]).$($splitName[-1])@YOURDOMAIN.com"
# Set-ADUser $userPrincipalName -Add @{proxyAddresses="smtp:$combinedNewProxy"}
Write-Message("$currentTime`: (INFO) Added proxy addresses for $userEmail. smtp:$combinedNewProxy,SMTP:$userEmail")
}
}
catch {
Write-Message("$currentTime`: (ERROR) Failed to update $($user.NAME) ($userPrincipalName): $_")
}
break # comment to break after the first user
}
Write-Message("============ Finished user properties update at $currentTime ============")
# Sync Changes to AzureAD
# Start-ADSyncSyncCycle -PolicyType Delta
# Write-Message("$(Get-Date -Format "dd/MM/yyyy [HH:mm:ss]"): (DEBUG) Started AzureAD delta sync.")
}
UserUpdate -reset_logs $false -csvFileName "YOUR_USERS_CSV.csv"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment