VMware Cloud Community
Curious5
Contributor
Contributor
Jump to solution

Need a script to pull the DRS rules, Vms on the rule, and if the rule is disabled then need an email to be sent. Can you please help .

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

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

In that case, try like this.

Get-Cluster -PipelineVariable cluster |

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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

You can try something like this

Get-Cluster -PipelineVariable cluster |

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

Curious5
Contributor
Contributor
Jump to solution

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

Reply
0 Kudos
Curious5
Contributor
Contributor
Jump to solution

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.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

In that case, try like this.

Get-Cluster -PipelineVariable cluster |

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