Try this one:
# Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass
# Connect to the vCenter Server
$vCenterServer = Read-Host -Prompt 'Enter Source vCenter FQDN: '
Connect-VIServer -Server $vCenterServer
# Specify the output CSV file path for DRS Rules
$outputFile = "Z:\PowerCLI\AffinityRulesReport.csv"
# Specify the output CSV file path for DRS VM Host Rules
$outputFile2 = "Z:\PowerCLI\AffinityVMHostRulesReport.csv"
# Get Cluster name as an input
$clusterName = Read-Host -Prompt 'Enter Cluster Name: '
# Retrieve all DRS rules for the cluster
$rules = Get-Cluster -Name $clusterName | Get-DrsRule
$rulesVMHost = Get-Cluster -Name $clusterName | Get-DrsVMHostRule
$vms = $null
# Create a detailed report for DRS Rules
$report = foreach ($rule in $rules) {
# Get associated VMs
if ($rule.VMIds) {
$associatedVMs = $rule.VMIds
foreach ($associatedVM in $associatedVMs)
{
$vms += Get-VM -Id $associatedVM | Select-Object -ExpandProperty Name
$vms += ","
}
} else {
"No VMs"
}
# Output the data in an object
[PSCustomObject]@{
RuleName = $rule.Name
Enabled = $rule.Enabled
RuleType = $rule.Type
AssociatedVMs = $vms
}
$vms = $null
}
# Create a detailed report for DRS Rules
$reportVMHost = foreach ($ruleVMHost in $rulesVMHost) {
# Output the data in an object
[PSCustomObject]@{
RuleName = $ruleVMHost.Name
Enabled = $ruleVMHost.Enabled
RuleType = $ruleVMHost.Type
VMGroup = $ruleVMHost.VMGroup
VMHostGroup = $ruleVMHost.VMHostGroup
}
}
# Export to CSV
$report | Export-Csv -Path $outputFile -NoTypeInformation
$reportVMHost | Export-Csv -Path $outputFile2 -NoTypeInformation
Original Message:
Sent: Nov 28, 2024 06:05 AM
From: Syed abthahir A
Subject: How can I generate particular affinity rule report from my cluster?
How can I generate particular affinity rule report from my cluster?