Pages

1/15/2014

Check Time on VMWare Hosts

Check Time on VMware Hosts Similar to my previous script reporting on Windows Server time.

##########################################################################################
# esx-time.ps1
#
# Gather local time and time zone for all servers in AD.
# Report servers with time different from machine on which script is run
# E-Mail report to designated recipient.
#
# Requires vmware power cli
# "set-powercliconfiguration -InvalidCertificateAction Ignore" to ignore cert errors
#
#
##########################################################################################

Add-PSSnapin VMware.VimAutomation.Core  -ErrorAction SilentlyContinue

#VCS
$vcs="privcs01" # PRIVDIVC1, PRIDEVVC1, PRIVCSVDI01, SECVCS01

# E-Mail Recipient 
$recipient="administrator@DOMAIN.com"

# Greatest acceptable time difference
$maxdiff=59

$outfile = "c:\dev\esx-time.html"
$user = "usa\svc_vmware"
$pw = Read-Host "Password for $user" -AsSecureString
$rootpw = Read-Host "Password for root" -AsSecureString
#convert $pw to plain text
    $pass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($pw))
#convert $rootpw to plain text
    $rootpass = [Runtime.InteropServices.Marshal]::PtrToStringAuto(
        [Runtime.InteropServices.Marshal]::SecureStringToBSTR($rootpw))

############################################################################################
# Get List of Host Names from VCS server
#
write-host "Getting Server List from VCS"
connect-viserver -Server $vcs -User $user -Password $pass -ErrorAction SilentlyContinue > $null
$hosts = get-vmhost
disconnect-viserver -confirm:$false -ErrorAction SilentlyContinue > $null
$list = @()
foreach ($Server in $hosts) { 
  $Server = $Server -replace "\s{2,}", ""
  $Server = $Server -replace "\.usa\.DOMAIN\.com", ""
  if ($Server) { $list = $list + $Server } #skip a null value
  } 
$list = $list | sort-object
write-host "COMPLETE"
###


#clear-host
$today = get-date

############################################################################################
# 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>VMWare Host Time Issues</H2> 
<H4>
'@
$report+=$today
$report+="</H4><table><th>Server</th><th>Date</th><th>Time</th><th>Off(s)</th></tr>"

############################################################################################
# Query every server in list 

write-host "Checking each server:"

foreach ($item in $list) {
  $flag=" "
  $dt=""
  $srvtime=""
  $hour = ""
  $min = ""
  $sec = ""
  $month = ""
  $day = ""
  $year = ""
  $comp = ""
  $d = ""
  $t = ""
   $computer = $item 
  $comp = "{0,-16}" -f $computer
  $comp = $comp.padright(16," ")

write-host ">$computer<"

 If (Test-Connection -computername $computer -Quiet -count 1){ #respond to ping?
write-host "    PING - SUCCESS"
write-host "    Time Query"
  $output=""
  $block = @'
  $machine = $args[0]
  $pass = $args[1]
  Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
  connect-viserver -server $machine -user "root" -password $pass -WarningAction SilentlyContinue -ErrorAction SilentlyContinue > $null
#  $time = (get-view serviceinstance).CurrentTime()
#  $time
  ((get-view serviceinstance).CurrentTime())
'@
  $sb = [scriptblock]::create($block)
  $args=@()
  $args = ($computer, $rootpass)
  start-job -name $computer -scriptblock $sb -argumentlist $args > $null
  Wait-Job -name $computer -Timeout 60 > $null 
  $output = Receive-Job $computer
#$output
  #disconnect-viserver -server $computer -confirm:$false #-ErrorAction SilentlyContinue > $null
  if ($output) {  
  $dt = $output
write-host "DT=$dt"
  }
  else {   
  write-host "$computer`tERROR:  No time Query Output"
   $report+='<tr><td class="error">' + $comp + '</td>' 
    $report+='<td colspan="4" class="error">' + "ERROR - No response to date/time query" + '</td>'  
   $report+='</tr>' 
  continue
  } # end if output
 }#end if ping
 else { # No PING response
  write-host "$computer`tERROR:  No PING Response"
   $report+='<tr><td class="error">' + $comp + '</td>' 
          $report+='<td colspan="4" class="error">' + "CONNECTION ERROR:  No response to PING" + '</td>'  
   $report+='</tr>' 
  continue
  }
 [string] $month = [System.Convert]::ToString($dt.Month)
 $month = $month.padleft(2,"0")
 [string] $day = [System.Convert]::ToString($dt.Day)
 $day = $day.padleft(2,"0")
 [string] $year = [System.Convert]::ToString($dt.Year)
 $year = $year.padleft(4,"0")
 $d = $month + "/" + $day + "/" + $year
write-host "    Date=$d"
 [string] $hour = [System.Convert]::ToString($dt.Hour)
 $hour = $hour.padleft(2,"0")
 [string] $min = [System.Convert]::ToString($dt.Minute)
 $min = $min.padleft(2,"0")
 [string] $sec = [System.Convert]::ToString($dt.Second)
 $sec = $sec.padleft(2,"0")
 $t = $hour + ":" + $min + ":" + $sec
write-host "    Time=$t"
 if ($dt) { #$dt not null
  $srvtime=($dt.Minute)*60+($dt.Second)
  $refmin = ""
  $refsec = ""
  $now = get-date
  $cmptime = ($now.Minute)*60+($now.Second)
  [string] $min = [System.Convert]::ToString($now.Minute)
  $refmin = $min.padleft(2,"0")
  [string] $sec = [System.Convert]::ToString($now.Second)
  $refsec = $sec.padleft(2,"0")
  $reftime=$refmin+":"+$refsec
  #Compare server time to script machine time.
  $diff=$cmptime-$srvtime
write-host "    Time Diff=$diff"
  if (($diff -gt $maxdiff) -or ($diff -lt (-1*$maxdiff))) { $flag="*" }
   write-host "$flag$comp`t$d`t$t`t$flag$diff$flag`t" $timezone.Description 
   if ($flag -eq "*") { #time difference is too great
    $report+=('<tr><td>' + $comp + '</td>') 
    $report+=('<td >' + $d + '</td>')  
    $report+=('<td >' + $t + '</td>')  
    $report+=('<td >' + $diff + '</td>')  
    $report+=('</tr>') 
    }
  }# end if null 
}#end foreach computer
###
$report+="</TABLE></BODY></HTML>" 

$report | out-file $outfile 

############################################################################################
#e-mail the report

$messageSubject = "VMWare Host Time Report"
$smtpServer = "smtp.DOMAIN.com"
$smtpFrom = "noreply@DOMAIN.com"
$smtpTo = $recipient
$message = $report
send-mailmessage -to $smtpTo -from $smtpFrom -subject $messageSubject -body $message -smtpserver $smtpServer -BodyAsHtml 
###


remove-job * -force

No comments: