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/thehuntzman Apr 16 '26
... and OP has the option to be more descriptive than "here's a bunch of code I don't understand; help me look good at work please"
I have a serious issue with people running scripts at work they don't understand that they copy and pasted from some random website or a generative AI model because the security and data integrity implications can be huge. I'm more than happy to help teach but the question wasn't about learning it was along the lines of "here's some code vomit I need someone to do all the work for me so I can take credit" - so yeah I'm going to be a dick about that.