Pages

1/30/2014

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=@'
<STYLE>
BODY{font-family: Verdana, Arial, Helvetica, sans-serif;font-size:12;font-color: #000000}
TABLE{border-width: 2px;padding: 1px;border-style: solid;border-color: black;border-collapse: collapse;} 
TH{border-width: 2px;padding: 1px;border-style: solid;border-color: black;background-color: #dddddd;font-size:16;font-weight:bold}
TD{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #efefef; font-size:12;font-weight:normal} 
TD.error{border-width: 2px;padding: 2px;border-style: solid;border-color: black;background-color: #ffffff;font face="monospace";font-size:10;font-color: #cccccc}
</STYLE> 
<HTML> 
<HEAD> 
<TITLE></TITLE> 
</HEAD> 
<BODY> 
<H2>Server Time Issues</H2> 
<H4>
'@
$report+=$today
$report+="Command:  $rcmd"
$report+="</H4><table><th>Server</th><th>IP Number</th><th>Result</th></tr>"

############################################################################################
# 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+=('<tr><td>' + $computer + '</td>') 
#   $report+=('<td >$ip</td>')  
#   $report+=('<td >Success</td>')  
#   $report+=('</tr>')
   } 
  else { #fail 
   "$computer - $ip" + ": <$rcmd> -> Fail" | out-file $logfile -append
   $report+=('<tr><td>' + $computer + '</td>') 
   $report+=('<td >$ip</td>')  
   $report+=('<td >Fail</td>')  
   $report+=('</tr>')
   }
  }#end if connection
 }#end foreach computer
clear-host
notepad $logfile

$report+="</TABLE></BODY></HTML>" 

$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: