##########################################################################################
# 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=@'
VMWare Host Time Issues
'@
$report+=$today
$report+="
Server Date Time Off(s) "
############################################################################################
# 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+='' + $comp + ' '
$report+='' + "ERROR - No response to date/time query" + ' '
$report+=' '
continue
} # end if output
}#end if ping
else { # No PING response
write-host "$computer`tERROR: No PING Response"
$report+='' + $comp + ' '
$report+='' + "CONNECTION ERROR: No response to PING" + ' '
$report+=' '
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+=('' + $comp + ' ')
$report+=('' + $d + ' ')
$report+=('' + $t + ' ')
$report+=('' + $diff + ' ')
$report+=(' ')
}
}# end if null
}#end foreach computer
###
$report+="
"
$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
1/15/2014
Check Time on VMWare Hosts
Check Time on VMware Hosts
Similar to my previous script reporting on Windows Server time.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment