VMware Cloud Community
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

DRS Rules output customize

I have written below script.

$DRSRules = Get-Cluster | Get-DrsRule
&{ForEach ($DRSRule in $DRSRules)
{
"" | Select-Object -Property @{N="DRS_Rule_Name";E={$DRSRule.Name}},
@{N="Cluster";E={$DRSRule.Cluster.Name}},
@{N="KeepTogether";E={$DRSRule.KeepTogether}},
@{N="Type";E={$drsrule.Type}},
@{N="VMs";E={$drsrule.vmids |%{ (Get-VM -id $_).name -join "`n"}}},
@{N="vCenter";E={$drsrule.uid.split("@:")[1]}}
} } | export-csv .\DRS_Rules.csv -notypeinformation

Output of above Script is below:

DRS_Rule_NameClusterKeepTogetherTypeVMsvCenter
Rule1Cluster1FALSEVMAntiAffinityVM1 VM2 VM3

vcenter1

 

My Expected output is below:

DRS_Rule_NameClusterKeepTogetherTypeVMsvCenter
Rule1Cluster1FALSEVMAntiAffinityVm1vcenter1
Rule1Cluster1FALSEVMAntiAffinityVM2vcenter1
Rule1Cluster1FALSEVMAntiAffinityVM3vcenter1

 

Can someone please help on same

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

 

Get-Cluster -PipelineVariable cluster | 
    Get-DrsRule -PipelineVariable rule |
    ForEach-Object -Process {
        $obj = New-Object -TypeName PSObject -Property ([ordered]@{
                Cluster      = $cluster.Name
                KeepTogether = $rule.KeepTogether
                Type         = $rule.Type
                VM          = ''
                vCenter      = ([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host
            })
        if ($rule.vmids) {
            $rule.vmids |
                ForEach-Object -Process {
                    $obj.VM = (Get-VM -Id $_).Name
                    $obj
                }
            } else {
                $obj
            }
        } | Export-Csv .\DRS_Rules.csv -NoTypeInformation

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

 

Get-Cluster -PipelineVariable cluster | 
    Get-DrsRule -PipelineVariable rule |
    ForEach-Object -Process {
        $obj = New-Object -TypeName PSObject -Property ([ordered]@{
                Cluster      = $cluster.Name
                KeepTogether = $rule.KeepTogether
                Type         = $rule.Type
                VM          = ''
                vCenter      = ([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host
            })
        if ($rule.vmids) {
            $rule.vmids |
                ForEach-Object -Process {
                    $obj.VM = (Get-VM -Id $_).Name
                    $obj
                }
            } else {
                $obj
            }
        } | Export-Csv .\DRS_Rules.csv -NoTypeInformation

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Hi Lucd,

Getting below error.

 

WARNING: The 'KeepTogether' property of DrsVMAntiAffinityRule type is deprecated. Use the 'Type' property instead.

Exception setting "VM": "The property 'VM' cannot be found on this object. Verify that the property exists and can be

set."

At line:14 char:21

+                     $obj.VM = (Get-VM -Id $_).Name

+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException

    + FullyQualifiedErrorId : ExceptionWhenSetting

 

 

Tried $obj and below is the output

PS C:\Users\Username\Desktop> $obj


Cluster : Cluster1
KeepTogether : False
Type : VMAntiAffinity
VMs :
vCenter : vCenter FQDN

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, that was a typo, the property should be VM instead of VMs.
I corrected the code above.

You can ignore the warning.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Sivaramsharmar
Enthusiast
Enthusiast
Jump to solution

Thanks Lucd for the support.

 

Appended DRS_Rule_name in your script and here is the updated script.

Get-Cluster -PipelineVariable cluster |
Get-DrsRule -PipelineVariable rule |
ForEach-Object -Process {
$obj = New-Object -TypeName PSObject -Property ([ordered]@{
DRS_Rule_name = $rule.name
Cluster = $cluster.Name
KeepTogether = $rule.KeepTogether
Type = $rule.Type
VM = ''
vCenter = ([System.Uri]$cluster.ExtensionData.Client.ServiceUrl).Host
})
if ($rule.vmids) {
$rule.vmids |
ForEach-Object -Process {
$obj.VM = (Get-VM -Id $_).Name
$obj
}
} else {
$obj
}
} | Export-Csv .\DRS_Rules.csv -NoTypeInformation

Tags (1)
0 Kudos