Run a command on every server in domain.
(Powershell v2 compatible)
#############################################################################################################
#
# run-remote.ps1
#
# run a command on each server in the domain
# log results to file and e-mail report of failures
#
$rcmd = "ping cmsweb"
$success = "Reply from"
$logfile = ".\run-remote.log"
$outfile = ".\cmsweb-test.html"
$rptname = "cmsweb reachability"
$recipient = "user@domain.com"
############################################################################################
# Create $list of server names for all Windows servers in Active Directory
#
$strCategory = "computer"
$strOS = "Windows*Server*"
$objDomain = New-Object System.DirectoryServices.DirectoryEntry
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("OperatingSystem=$strOS")
$colProplist = "dnshostname"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}
$colResults = $objSearcher.FindAll()
foreach ($objResult in $colResults)
{
$objComputer = $objResult.Properties;
$Server = $objComputer.dnshostname
$Server = $Server -replace "\s{2,}", ""
$Server = $Server -replace "\.usa\.domain\.com", ""
if ($Server) { $list = $list + $Server } #skip a null value
}
$list = $list | sort-object
###
clear-host
$today = get-date
$today | out-file $logfile
"Run: $rcmd"
$username = read-host "Logon"
$pw = read-host -AsSecureString "Password"
$pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
[Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
############################################################################################
# Report Heading
$report=@'
Server Time Issues
'@
$report+=$today
$report+="Command: $rcmd"
$report+="
Server | IP Number | Result | "
############################################################################################
# Run for every server in list
foreach ($computer in $list) {
If (Test-Connection -computername $computer -Quiet -count 1){ #respond to ping?
#get IP address
$conn = test-connection -computername $computer -count 1
$ip = $conn.IPV4Address.IPAddressToString
"Executing remotely from $computer - $ip"
$cmd = "c:\util\psexec.exe /acceptEula \\$computer -u $username -p $pass -w c:\ $rcmd"
$result = invoke-expression $cmd
if ($result -like "$success*") { #success
"$computer - $ip" + ": <$rcmd> -> Success" | out-file $logfile -append
# $report+=('' + $computer + ' | ')
# $report+=('$ip | ')
# $report+=('Success | ')
# $report+=('
')
}
else { #fail
"$computer - $ip" + ": <$rcmd> -> Fail" | out-file $logfile -append
$report+=('' + $computer + ' | ')
$report+=('$ip | ')
$report+=('Fail | ')
$report+=('
')
}
}#end if connection
}#end foreach computer
clear-host
notepad $logfile
$report+="
"
$report | out-file $outfile
############################################################################################
#e-mail the report
$messageSubject = $rptname
$smtpServer = "smtp.domain.com"
$smtpFrom = "noreply@domain.com"
$smtpTo = $recipient
$message = $report
send-mailmessage -to $smtpTo -cc "admin@domain.com" -from $smtpFrom -subject $messageSubject -body $message -smtpserver $smtpServer -BodyAsHtml
###
No comments:
Post a Comment