Need a script to pull the DRS rules, Vms on the rule, and if the rule is disabled then need an automatic email to be sent. Can you please help . LucD
In that case, try like this.
ForEach-Object -Process {
$cluster.ExtensionData.ConfigurationEx.Rule |
Where-Object { $_ -is [VMware.Vim.ClusterVmHostRuleInfo] } |
ForEach-Object -Process {
$rule = $_
New-Object PSObject -Property ([ordered]@{
Cluster = $cluster.Name
Rule = $rule.Name
Enabled = $rule.Enabled
Type = & {
if ($rule.AffineHostGroupName -ne $null) { $runType = 'RunOn' } else { $runType = 'NotRunOn' }
if ($rule.Mandatory) { $runObligation = 'Shall' } else { $runObligation = 'Should' }
"$runObligation$runType"
}
VM = $rule.VmGroupName
Host = $rule.AffineHostGroupName, $rule.AntiAffineHostGroupName | Where-Object { $_ -ne $null }
VMs = $cluster.ExtensionData.ConfigurationEx.Group | Where-Object { $_.Name -eq $rule.VMGroupName } | % { (Get-View -Id $_.VM).Name -join '|' }
Hosts = $cluster.ExtensionData.ConfigurationEx.Group |
Where-Object { $_.Name -eq $rule.AffineHostGroupName -or $_.Name -eq $rule.AntiAffineHostGroupName } | % {
(Get-View -Id $_.Host).Name -join '|'
}
})
if (-not $rule.Enabled)
{
$sMail = @{
To = 'you@domain'
From = 'me@domain'
SmtpServer = 'Smtp@domain'
Subject = "Rule $($rule.Name) is disabled"
}
Send-MailMessage @sMail
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You can try something like this
Get-DrsVMHostRule |
ForEach-Object -Process {
New-Object PSObject -Property @{
Cluster = $cluster.Name
Rule = $_.Name
Enabled = $_.Enabled
Type = $_.Type
VM = $_.VMGroup
Host = $_.VMHostGroup
VMs= (Get-DrsClusterGroup -Cluster $cluster -Type VMGroup -Name $_.VMGroup).Member.Name -join '|'
Hosts = (Get-DrsClusterGroup -Cluster $cluster -Type VMHostGroup -Name $_.VMHostGroup).Member.Name -join '|'
}
if(-not $_.Enabled){
$sMail = @{
To = 'you@domain'
From = 'me@domain'
SmtpServer = 'Smtp@domain'
Subject = "Rule $($_.Name) is disabled"
}
Send-MailMessage @sMail
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD I will test it now. I cant thank you enough. Cant believe i can get such quick responses from you . Thanks a ton ! Appreciate it
This code is great but it requires 6.5 version. But unfortunately all our 20+ sites have powercli version 6.0 installed. It would take a while for us to update it. Meanwhile if you have a code that works in 6.0 , it would be great. But thanks again.
In that case, try like this.
ForEach-Object -Process {
$cluster.ExtensionData.ConfigurationEx.Rule |
Where-Object { $_ -is [VMware.Vim.ClusterVmHostRuleInfo] } |
ForEach-Object -Process {
$rule = $_
New-Object PSObject -Property ([ordered]@{
Cluster = $cluster.Name
Rule = $rule.Name
Enabled = $rule.Enabled
Type = & {
if ($rule.AffineHostGroupName -ne $null) { $runType = 'RunOn' } else { $runType = 'NotRunOn' }
if ($rule.Mandatory) { $runObligation = 'Shall' } else { $runObligation = 'Should' }
"$runObligation$runType"
}
VM = $rule.VmGroupName
Host = $rule.AffineHostGroupName, $rule.AntiAffineHostGroupName | Where-Object { $_ -ne $null }
VMs = $cluster.ExtensionData.ConfigurationEx.Group | Where-Object { $_.Name -eq $rule.VMGroupName } | % { (Get-View -Id $_.VM).Name -join '|' }
Hosts = $cluster.ExtensionData.ConfigurationEx.Group |
Where-Object { $_.Name -eq $rule.AffineHostGroupName -or $_.Name -eq $rule.AntiAffineHostGroupName } | % {
(Get-View -Id $_.Host).Name -join '|'
}
})
if (-not $rule.Enabled)
{
$sMail = @{
To = 'you@domain'
From = 'me@domain'
SmtpServer = 'Smtp@domain'
Subject = "Rule $($rule.Name) is disabled"
}
Send-MailMessage @sMail
}
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference