# AD Pre-Flight Check Tool
# Run before executing any AD automation scripts
Write-Host "===== Active Directory Pre-Flight Check =====" -ForegroundColor Cyan
$errors = 0
# 1. Check Admin Rights
Write-Host "`n[1] Checking Admin Privileges..."
if (-not ([Security.Principal.WindowsPrincipal] `
[Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltinRole]::Administrator)) {
Write-Host "Not running as Administrator" -ForegroundColor Red
$errors++
} else {
Write-Host "Running as Administrator" -ForegroundColor Green
}
# 2. Check AD Module
Write-Host "`n[2] Checking Active Directory Module..."
if (Get-Module -ListAvailable -Name ActiveDirectory) {
Import-Module ActiveDirectory
Write-Host "AD Module Loaded" -ForegroundColor Green
} else {
Write-Host "ActiveDirectory module not installed" -ForegroundColor Red
$errors++
}
# 3. Check Domain Connectivity
Write-Host "`n[3] Checking Domain Connectivity..."
try {
if (Test-ComputerSecureChannel) {
Write-Host "Connected to Domain" -ForegroundColor Green
} else {
Write-Host "Not connected to Domain" -ForegroundColor Red
$errors++
}
} catch {
Write-Host "Domain check failed" -ForegroundColor Red
$errors++
}
# 4. Check Domain Controller
Write-Host "`n[4] Checking Domain Controller Availability..."
try {
$dc = Get-ADDomainController -Discover -ErrorAction Stop
Write-Host "DC Found: $($dc.HostName)" -ForegroundColor Green
} catch {
Write-Host "No Domain Controller reachable" -ForegroundColor Red
$errors++
}
# 5. Check Permissions (basic test)
Write-Host "`n[5] Checking AD Permissions..."
try {
Get-ADUser -Filter * -ResultSetSize 1 | Out-Null
Write-Host "AD Read Access OK" -ForegroundColor Green
} catch {
Write-Host "No permission to query AD" -ForegroundColor Red
$errors++
}
# 6. Execution Policy
Write-Host "`n[6] Checking Execution Policy..."
$policy = Get-ExecutionPolicy
Write-Host "Current Policy: $policy"
if ($policy -eq "Restricted") {
Write-Host "Execution Policy may block scripts" -ForegroundColor Yellow
}
# 7. Time Sync Check
Write-Host "`n[7] Checking Time Synchronization..."
try {
w32tm /query /status | Out-Null
Write-Host "Time service running" -ForegroundColor Green
} catch {
Write-Host "Time sync issue detected" -ForegroundColor Yellow
}
# 8. OU Validation (Optional Input)
$ou = Read-Host "`n[8] Enter OU to validate (or press Enter to skip)"
if ($ou) {
try {
Get-ADOrganizationalUnit -Identity $ou -ErrorAction Stop
Write-Host "OU exists: $ou" -ForegroundColor Green
} catch {
Write-Host "Invalid OU: $ou" -ForegroundColor Red
$errors++
}
}
# 9. CSV Validation (Optional Input)
$csvPath = Read-Host "`n[9] Enter CSV path to validate (or press Enter to skip)"
if ($csvPath) {
if (Test-Path $csvPath) {
try {
Import-Csv $csvPath | Select-Object -First 1 | Out-Null
Write-Host "CSV format looks valid" -ForegroundColor Green
} catch {
Write-Host "CSV format error" -ForegroundColor Red
$errors++
}
} else {
Write-Host "CSV file not found" -ForegroundColor Red
$errors++
}
}
# FINAL RESULT
Write-Host "`n===== RESULT ====="
if ($errors -eq 0) {
Write-Host "All checks passed. Safe to run AD scripts." -ForegroundColor Green
} else {
Write-Host "$errors issue(s) found. Fix before proceeding." -ForegroundColor Red
}
Write-Host "============================================="
Comments
Post a Comment