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_Name | Cluster | KeepTogether | Type | VMs | vCenter |
Rule1 | Cluster1 | FALSE | VMAntiAffinity | VM1 VM2 VM3 | vcenter1 |
My Expected output is below:
DRS_Rule_Name | Cluster | KeepTogether | Type | VMs | vCenter |
Rule1 | Cluster1 | FALSE | VMAntiAffinity | Vm1 | vcenter1 |
Rule1 | Cluster1 | FALSE | VMAntiAffinity | VM2 | vcenter1 |
Rule1 | Cluster1 | FALSE | VMAntiAffinity | VM3 | vcenter1 |
Can someone please help on same
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
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
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
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
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