r/PowerShell • u/TuneIcy582 • Apr 15 '26
Could someone please verify syntax and function on this script
I am still learning PowerShell scripting I would love it if someone could help me verify this so I can look like a superhero in my job:
Import-Module ActiveDirectory
# =========================
# CONFIGURATION
# =========================
$CsvPath = "C:\Temp\ProxyOnlyUpdate.csv"
$LogPath = "C:\Temp\ProxyOnlyUpdate.log"
$ReportPath = "C:\Temp\ProxyOnlyUpdate_Report.csv"
$WhatIf = $false # Dry-run mode
$Rollback = $false # Set to $true to restore from log
# =========================
# Ensure log directory exists
# =========================
$logDir = Split-Path $LogPath
if (-not (Test-Path $logDir)) {
New-Item -ItemType Directory -Path $logDir -Force | Out-Null
}
# =========================
# ROLLBACK MODE
# =========================
if ($Rollback) {
Write-Host "=== ROLLBACK MODE ENABLED ===" -ForegroundColor Yellow
if (-not (Test-Path $LogPath)) {
Write-Warning "Log file not found. Cannot rollback."
return
}
$logEntries = Get-Content $LogPath | Where-Object { $_ -match "SUCCESS:" }
foreach ($line in $logEntries) {
if ($line -match "SUCCESS:\s(?<user>[^|]+)\|\sOLD:(?<old>[^|]+)\|\sNEW:") {
$samAccountName = $matches['user'].Trim()
$oldProxies = $matches['old'].Trim() -split ';'
try {
Set-ADUser $samAccountName -Replace @{ proxyAddresses = $oldProxies }
Write-Host "Rolled back: $samAccountName" -ForegroundColor Cyan
}
catch {
Write-Warning "Rollback failed for $samAccountName"
}
}
}
return
}
# =========================
# LOAD CSV
# =========================
$users = Import-Csv $CsvPath
$report = @()
foreach ($user in $users) {
# -------------------------
# Validate CSV row
# -------------------------
if (-not $user.SamAccountName -or -not $user.NewPrimarySMTP) {
Write-Warning "Invalid row detected (missing data)"
continue
}
# Normalize input
$samAccountName = $user.SamAccountName.Trim()
$newPrimarySMTP = $user.NewPrimarySMTP.Trim().ToLower()
$oldSMTP = if ($user.OldSMTP) { $user.OldSMTP.Trim().ToLower() }
# -------------------------
# Get AD User
# -------------------------
$adUser = Get-ADUser $samAccountName `
-Properties proxyAddresses `
-ErrorAction SilentlyContinue
if (-not $adUser) {
Write-Warning "User not found: $samAccountName"
continue
}
# Capture BEFORE state
$before = @($adUser.proxyAddresses)
# -------------------------
# Build new proxy list
# -------------------------
$proxies = @($before)
# Remove primary SMTP
$proxies = $proxies | Where-Object { $_ -notmatch "^SMTP:" }
# Remove any instance of new primary
$escapedNew = [regex]::Escape($newPrimarySMTP)
$proxies = $proxies | Where-Object { $_ -notmatch "(?i)^smtp:$escapedNew$" }
# Add old SMTP as alias
if ($oldSMTP) {
$escapedOld = [regex]::Escape($oldSMTP)
if ($proxies -notmatch "(?i)^smtp:$escapedOld$") {
$proxies += "smtp:$oldSMTP"
}
}
# Add new primary
$proxies += "SMTP:$newPrimarySMTP"
# Remove duplicates
$proxies = $proxies | Sort-Object -Unique
# Normalize aliases (optional consistency)
$proxies = $proxies | ForEach-Object {
if ($_ -cmatch "^SMTP:") { $_ } else { $_.ToLower() }
}
# -------------------------
# Apply or simulate
# -------------------------
if ($WhatIf) {
Write-Host "[DRY-RUN] $samAccountName" -ForegroundColor Yellow
}
else {
try {
Set-ADUser $adUser -Replace @{ proxyAddresses = $proxies }
Write-Host "Updated: $samAccountName" -ForegroundColor Green
Add-Content $LogPath "$(Get-Date) SUCCESS: $samAccountName | OLD:$($before -join ';') | NEW:$($proxies -join ';')"
}
catch {
Write-Warning "Failed: $samAccountName"
Add-Content $LogPath "$(Get-Date) ERROR: $samAccountName | $($_.Exception.Message)"
}
}
# -------------------------
# Add to report
# -------------------------
$report += [PSCustomObject]@{
SamAccountName = $samAccountName
Before = ($before -join ';')
After = ($proxies -join ';')
}
}
# =========================
# EXPORT REPORT
# =========================
$report | Export-Csv $ReportPath -NoTypeInformation
Write-Host "Report exported to $ReportPath" -ForegroundColor Cyan
View all comments
Show parent comment
6
u/hihcadore Apr 16 '26
I can tell you right now they used Claude code. I’ve used it enough myself to spot it off the bat.
They also didn’t prompt it with best practices so my guess here is, it’s not working and they can’t figure out why and are too afraid to hammer AD with a failing script in production.
AI is so helpful, but only if you have a solid base to back it up.