r/PowerShell • • 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

0 Upvotes

17 comments sorted by

View all comments

Show parent comment

-5

u/[deleted] Apr 15 '26

[removed] — view removed comment

2

u/cheetah1cj Apr 16 '26

They are criticizing someone for expecting others to do their work for them to make them look good at work. Sometimes people need some criticism. This subreddit exists to help people improve their powershell skills or overcome challenges, not for people too lazy too even try to understand their own script or do their own work.