2,187
edits
(→Script Extracts: Re-orged sections) |
(→ESX: Added "Daily ESX Up/Down Check") |
||
Line 877: | Line 877: | ||
Stop-Transcript | Stop-Transcript | ||
</source> | |||
==== Daily ESX Up/Down Check ==== | |||
Simple script that runs every morning just before I start work to provide a basic sanity check email to glance through over the first tea of the day. | |||
<source lang="Powershell"> | |||
############################################################################################# | |||
# | |||
# ESX Checker | |||
# | |||
############################################################################################# | |||
# | |||
# By Simon Strutt | |||
# | |||
# Version 1 - Aug 10 | |||
# - Initial creation | |||
# | |||
# Version 2 - Oct 10 | |||
# - Fixed VIServer handling (was never good but stuffed by upgrade to PowerCLI 4.1) | |||
# - Improved email in case of vCentre connection failure | |||
# | |||
# Version 2.1 - Nov 10 | |||
# - Minor text changes | |||
# | |||
# Version 2.2 - Nov 10 | |||
# - Added InvalidLogon catch to vCentre logon to prevent a/c lockout | |||
# | |||
# Version 3 - Dec 10 | |||
# - Truncated displayed ESX hostname (stripped domain) | |||
# - Added "since <datetime>" info to ESX's that aren't in a good state | |||
# - Bugfix: Incorrect email config used when VC list config file load failed | |||
# | |||
# Version 3.1 Jan 11 | |||
# - Workaround: Powershell/MS DateTime object returns incorrect (US) date format | |||
# | |||
# Version 3.2 Jan 11 | |||
# - Bugfix: Extraneous " hrs" on end of v3.1 fixed datetimes | |||
# | |||
############################################################################################# | |||
$DateFormat = "%R hrs, %a %d %b %Y" | |||
$EmailBody = "Results of ESX status check as of " + (get-date -uFormat "$DateFormat.`n`n") | |||
$EmailRecipients = "person1@domain.com,person2@domain.com" | |||
$EmailSender = "VI-Mgmt@domain.com" | |||
$smtp = New-Object Net.Mail.SmtpClient -arg "smtp-server.domain.com" | |||
$VCAlertToSend = 0 | |||
$ESXAlertToSend = 0 | |||
$start = Get-Date | |||
try { | |||
$ToCheck = Import-CSV "ESX-Check.csv" | |||
} catch { | |||
$EmailBody = "ERROR: Failed to load config file, no checks have been done.`n" | |||
$EmailBody += $_ | |||
$smtp.Send($EmailSender, $EmailRecipients, "ESX-Check: Check failed", $EmailBody) | |||
Exit | |||
} | |||
$pass = Get-Content "Cred.fil" | ConvertTo-SecureString | |||
$cred = New-Object System.Management.Automation.PsCredential("MPADGLOBAL\simonstrutt",$pass) | |||
foreach ($vc in $ToCheck) { | |||
Write-Host "Checking ESXs on" $vc.vc | |||
$VChasBadESX = 0 | |||
try { | |||
$VCconn = Connect-VIServer -Server $vc.vc -Credential $cred -NotDefault -errorAction "Stop" | |||
} catch [VMware.VimAutomation.ViCore.Types.V1.ErrorHandling.InvalidLogin] { | |||
$EmailBody += ($vc.vc).ToUpper() + "`t" + "Unable to connect to vCentre, invalid logon error !!`n`n" | |||
$EmailBody += "Abandoning further script processing in order to prevent potential account lockout.`n" | |||
$VCAlertToSend = 1 | |||
Break | |||
} catch { | |||
$EmailBody += ($vc.vc).ToUpper() + "`t" + "Unable to connect to vCentre, ESX's not checked !!`n" | |||
$VCAlertToSend = 1 | |||
Continue | |||
} | |||
$ESXs = Get-VMHost -Server $vc.vc | Where {$_.State -ne "Connected"} | Sort -property Name | |||
if ($ESXs) { | |||
foreach ($ESX in $ESXs) { | |||
# Attempt to work out when ESX went into its bad state | |||
Switch ($ESX.State) { | |||
NotResponding { | |||
$events = Get-VIEvent -Entity $ESX -Types Error | Where {$_.fullFormattedMessage -like "*is not responding*"} | |||
} | |||
Maintenance { | |||
$events = Get-VIEvent -Entity $ESX -MaxSamples 1000 | Where {$_.fullFormattedMessage -like "*has entered maintenance mode*"} | |||
} | |||
Disconnected { | |||
$events = Get-VIEvent -Entity $ESX -MaxSamples 1000 | Where {$_.fullFormattedMessage -like "*Disconnected from*"} | |||
} | |||
Default { | |||
$events = 0 | |||
} | |||
} | |||
If ($events) { | |||
$SinceString = " since " + (Get-Date $events[0].CreatedTime -uFormat $DateFormat) | |||
} else { | |||
$SinceString = "" | |||
} | |||
$EmailBody += $vc.vc + "`t" + ($ESX.Name.Split(".")[0]).ToUpper() + "`t" + $ESX.State + $SinceString + "`n" | |||
$ESXAlertToSend = 1 | |||
$VChasBadESX = 1 | |||
} | |||
} | |||
if (!$VChasBadESX) { | |||
$EmailBody += $vc.vc + "`t" + "All good`n" | |||
} | |||
Disconnect-VIServer -Server $VCconn -Confirm:$false | |||
} | |||
$end = Get-Date | |||
$EmailBody += "`n`n`nCheck started : " + (Get-Date $start -uFormat $DateFormat) + "`n" | |||
$EmailBody += "Check finished : " + (Get-Date $end -uFormat $DateFormat) + "`n" | |||
$EmailBody += "Generated by script: " + ($MyInvocation.MyCommand.Name) + "`n" | |||
$EmailBody += "Sent from machine : $env:computername" | |||
$EmailBody | |||
Write-Host "Sending email..." | |||
if ($VCAlertToSend) { | |||
$smtp.Send($EmailSender, $EmailRecipients, "ESX-Check: Unable to connect to some/all vCentres", $EmailBody) | |||
} elseif ($ESXAlertToSend) { | |||
$smtp.Send($EmailSender, $EmailRecipients, "ESX-Check: Some ESX's are NOT CONNECTED", $EmailBody) | |||
} else { | |||
$smtp.Send($EmailSender, $EmailRecipients, "ESX-Check: All connected OK", $EmailBody) | |||
} | |||
if (-not $?) { | |||
Write-Host "SMTP send failed!!" | |||
Start-Sleep(30000) | |||
} | |||
</source> | </source> | ||