Friday 21 August 2015

PowerCLI VM Host & DataStore affinity

We name our datastores with a three letter prefix which is the same three first letters of our hosts.  Folks who create VMs often forget to create the DRS rules so this script can be scheduled to run via Task Scheduler to alert us of VMs that need fixing:

____________________________________



### add snapin so PowerShell runs with PoweCLI features
Add-PSSnapin VMware.VimAutomation.Core
Connect-VIServer vSphere
#
$vms = Get-View -ViewType VirtualMachine -Property Name,Datastore,"Runtime.Host",  "Runtime.PowerState" 
### exclude Microsoft SQL Cluster VMs with RDMs that report disks from both sites 
$emailbody = foreach($vm in $vms){ 
    if ($vm.Name -ne "ClusterVM1" -and $vm.Name -ne "ClusterVM2") 
    {
### only report on VMs that are powered on
       if ($vm.Runtime.PowerState -eq "poweredOn") 
       {
### exclude hosts as needed
        $esx = Get-View $vm.Runtime.Host -Property Name
        if ($esx.Name.Split('.')[0] -ne "vh99" )
        {
            $ds = Get-View $vm.Datastore -Property Name
### report those datastores that don't match first three letters of hostname with first three letters of datastore name
            if ($ds | where {$_.Name.Substring(0,2) -notmatch $esx.Name.Substring(0,2)})
            
            {
                      
                       $vm | Select Name,
                        @{N="Host";E={$esx.Name.Split('.')[0]}},
                        @{N="DataStore";E={[string]::Join(',',($ds | %{$_.Name}))}} | out-string
               
            }
        }
        }
    }

### for email alert, get mail info from vCenter database
$vCenterSettings = Get-View -Id 'OptionManager-VpxSettings'
$MailSender = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.sender"}).Value
$MailSmtpServer = ($vCenterSettings.Setting | Where-Object { $_.Key -eq "mail.smtp.server"}).Value
$MailSendingTo = "email@email.com"

### test for VMs with running on wrong host according to datastore
if ($emailbody) 

{

write-host "These VMs are misconfigured and will fail during an outage of either data center, and they will fail with an outage of the link between data centres.  Fix the VMware DRS rule on $MailSender so these VMs run on host and disks in the same data center.  These VMs with disks and hosts spanning two locations are at risk of needless loss of service to our customers:

$emailbody"

$emailbody2 = "These VMs are misconfigured and will fail during an outage of either data center, and they will fail with an outage of the link between data centres.  Fix the VMware DRS rule on $MailSender so these VMs run on host and disks in the same data center.  These VMs with disks and hosts spanning two locations are at risk of needless loss of service to our customers:

$emailbody"

Send-MailMessage -from $MailSender -to $MailSendingTo -subject "alert: VM Host/Disk affinity violations" -body $emailbody2 -smtpServer $MailSmtpServer

}

else


write-host "ok: host/datastore affinity is correct for all VMs in $MailSender" 

$emailbody2 = "host/datastore affinity is correct for all VMs in $MailSender"

Send-MailMessage -from $MailSender -to $MailSendingTo -subject "ok: no VM Host/Disk affinity violations" -body $emailbody2 -smtpServer $MailSmtpServer

}

disConnect-VIServer vSphere -confirm:$false

________________________________________________

Thanks to LucD for doing the real work of this script.

No comments:

Post a Comment