Disclaimer
This is my personal blog. The opinions and views I express are my own. The information I provide is on an as-is basis. I make no representations as to accuracy, completeness, currentness, suitability, or validity of any information on this blog and will not be liable for any errors, omissions, or delays in this information or any losses, injuries or damages arising from its use.

Domain Controller Health script remotely (Powershell)

I use this script to run a few commands I find myself running on my DCs on a regular basis. I wanted a solution I could run from my client machine and output the results to a log file. Below is what I cam up with, it will run dcdiag, dcdiag /v, repadmin /showrepl or repadmin /replsummary depending on which you choose. It will output the results to c:\server_logs\dcname+date.log.

I have this setup to run as a scheduled task every couple days so all I have to do is take a look ot the log files.

Enjoy


$date = get-date -format M.d.yyyy

$server = Read-host "Server name"

# Path the where the logs will be stored
$logPath = "c:\server_logs\" + $server + "_" + $date + ".log"

# CMDs that can be ran as a part of this script
$dcdiag = {cmd.exe /c dcdiag}
$dcdiagverbose = {cmd.exe /c dcdiag /v}
$showrepl = {cmd.exe /c repadmin /showrepl}
$replsummary = {cmd.exe /c repadmin /replsummary}

# Error message displayed if an invalid entry is made
$errorMessage = "*** INVALID ENTRY ***"

# CMD Selection message
$selectionChoice = Write-Host -foregroundcolor Cyan "`n Choose from the following CMDs: `n" `
"1 = DCdiag `n 2 = DCdiag /v (Detailed information) `n 3 = RepAdmin /showrepl `n" `
"4 = RepAdmin /replsummary `n"`

# CMD input message
$cmdSelection = Read-Host "Which CMD"

# *** Start CMD Selection Code Block

$cmd = $cmdSelection

if(!($cmd)) {
	Throw " NO OPTION SELECTED "
	Exit
}

Switch($cmd) {
	1 {$cmd = $dcdiag}
    2 {$cmd = $dcdiagverbose}
	3 {$cmd = $showrepl}
    4 {$cmd = $replsummary}
	default {
    $errorMessage
    Exit
    }
}
# *** Start CMD Selection Code Block

# Run the chosen command on the server
$command = $cmd
write-host -foregroundcolor yellow "Please wait... Running command"
$runCommand = Invoke-Command -Computername $server -ScriptBlock $command
$runCommand | out-file $logPath -append
write-host -foregroundcolor yellow "Script complete, logfile is located at $logPath"

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.