74 lines
2.5 KiB
PowerShell
Executable File
74 lines
2.5 KiB
PowerShell
Executable File
#!/usr/bin/env pwsh
|
|
# Enable SMTP AUTH on O365 accounts in batch
|
|
# Connects to each tenant and enables SMTP Client Auth
|
|
|
|
$ErrorActionPreference = "Continue"
|
|
|
|
# Get accounts from DB
|
|
$connStr = "Host=localhost;Database=adx_system;Username=admin;Password=admin123"
|
|
$accounts = @()
|
|
|
|
# Use psql to get accounts
|
|
$csv = & psql -h localhost -U admin -d adx_system -t -A -F '|' -c @"
|
|
SELECT DISTINCT oa.admin_email, oa.admin_password, oa.tenant_domain
|
|
FROM office_accounts oa
|
|
WHERE LOWER(oa.status) IN ('active','verified','warming')
|
|
AND oa.admin_password IS NOT NULL AND oa.admin_password != ''
|
|
AND oa.tenant_domain LIKE '%.onmicrosoft.com'
|
|
ORDER BY oa.tenant_domain
|
|
LIMIT 20
|
|
"@
|
|
|
|
$env:PGPASSWORD = "admin123"
|
|
$results = @()
|
|
$success = 0
|
|
$failed = 0
|
|
|
|
foreach ($line in $csv) {
|
|
if ([string]::IsNullOrWhiteSpace($line)) { continue }
|
|
$parts = $line.Split('|')
|
|
if ($parts.Count -lt 3) { continue }
|
|
|
|
$email = $parts[0].Trim()
|
|
$password = $parts[1].Trim()
|
|
$tenant = $parts[2].Trim()
|
|
|
|
Write-Host "[$($success+$failed+1)] Connecting to $tenant as $email..."
|
|
|
|
try {
|
|
$secPwd = ConvertTo-SecureString $password -AsPlainText -Force
|
|
$cred = New-Object System.Management.Automation.PSCredential($email, $secPwd)
|
|
|
|
# Connect to Exchange Online
|
|
Connect-ExchangeOnline -Credential $cred -ShowBanner:$false -ErrorAction Stop
|
|
|
|
# Enable SMTP AUTH for all mailboxes in this tenant
|
|
$mailboxes = Get-CASMailbox -ResultSize Unlimited -ErrorAction Stop
|
|
foreach ($mb in $mailboxes) {
|
|
Set-CASMailbox -Identity $mb.Identity -SmtpClientAuthenticationDisabled $false -ErrorAction Stop
|
|
Write-Host " OK: $($mb.PrimarySmtpAddress) SMTP AUTH enabled"
|
|
}
|
|
|
|
# Also enable at org level
|
|
try {
|
|
Set-TransportConfig -SmtpClientAuthenticationDisabled $false -ErrorAction Stop
|
|
Write-Host " OK: Org-level SMTP AUTH enabled"
|
|
} catch { Write-Host " WARN: Org-level setting skipped" }
|
|
|
|
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
|
|
$success++
|
|
$results += "$email|OK"
|
|
}
|
|
catch {
|
|
$err = $_.Exception.Message
|
|
Write-Host " FAIL: $($err.Substring(0, [Math]::Min(80, $err.Length)))"
|
|
Disconnect-ExchangeOnline -Confirm:$false -ErrorAction SilentlyContinue
|
|
$failed++
|
|
$results += "$email|FAIL|$err"
|
|
}
|
|
}
|
|
|
|
Write-Host "`n=== SUMMARY: $success OK, $failed FAILED ==="
|
|
$results | Out-File /tmp/smtp_auth_results.txt
|
|
|