Extract configuration

As mentioned in the previous post, I am gathering an overview of vSphere infrastructure in an easy, absorbable format.

What other information is good to understand.

  • vSphere Cluster information
    • Cluster name
    • Number of hosts
  • DRS
    • Automation level
  • HA
  • Affinity rules
  • Anti Affinity rules

# Import VMware PowerCLI module
Import-Module VMware.PowerCLI

# Suppress invalid certificate warnings
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false

# Connect to vCenter Server
$vCenterServer = "your-vcenter-server"
$username = "your-username"
$password = "your-password"
Connect-VIServer -Server $vCenterServer -User $username -Password $password

# Retrieve Cluster Information
Write-Host "Fetching vSphere Cluster Information..."
$clusters = Get-Cluster | Select-Object Name, 
    @{Name="Number of Hosts"; Expression={(Get-VMHost -Location $_).Count}},
    @{Name="DRS Enabled"; Expression={$_.ExtensionData.Configuration.DrsConfig.Enabled}}, 
    @{Name="HA Enabled"; Expression={$_.ExtensionData.Configuration.DasConfig.Enabled}}, 
    @{Name="DRS Automation Level"; Expression={$_.ExtensionData.Configuration.DrsConfig.DefaultVmBehavior}}

Write-Host "Cluster Information:"
$clusters | ForEach-Object {
    Write-Host "Cluster Name: $($_.Name)"
    Write-Host "Number of Hosts: $($_."Number of Hosts")"
    Write-Host "DRS Enabled: $($_."DRS Enabled")"
    Write-Host "HA Enabled: $($_."HA Enabled")"
    Write-Host "DRS Automation Level: $($_."DRS Automation Level")"
    Write-Host "------------------------------"
}

# Retrieve ESXi Host Software Versions
Write-Host "Fetching ESXi Host Software Versions..."
$hosts = Get-VMHost | Select-Object Name, @{Name="Cluster"; Expression={(Get-Cluster -VMHost $_).Name}}, Version

Write-Host "Host Software Versions:"
$hosts | ForEach-Object {
    Write-Host "Host Name: $($_.Name)"
    Write-Host "Cluster: $($_.Cluster)"
    Write-Host "Version: $($_.Version)"
    Write-Host "------------------------------"
}

# Retrieve Affinity and Anti-Affinity Rules
Write-Host "Fetching Affinity and Anti-Affinity Rules..."
$rules = Get-Cluster | ForEach-Object {
    Get-DrsRule -Cluster $_ | Select-Object Name, Enabled, @{Name="Type"; Expression={
        if ($_.KeepTogether -eq $true) { "Affinity Rule" } else { "Anti-Affinity Rule" }
    }}
}

Write-Host "Affinity and Anti-Affinity Rules:"
$rules | ForEach-Object {
    Write-Host "Rule Name: $($_.Name)"
    Write-Host "Enabled: $($_.Enabled)"
    Write-Host "Type: $($_.Type)"
    Write-Host "------------------------------"
}

# Export Cluster Information, Host Versions, and Rules to CSV
$outputClusterFile = "ClusterInfo.csv"
$outputHostsFile = "HostSoftwareVersions.csv"
$outputRulesFile = "AffinityRules.csv"

Write-Host "Exporting Cluster Information to $outputClusterFile..."
$clusters | Export-Csv -Path $outputClusterFile -NoTypeInformation -Encoding UTF8

Write-Host "Exporting Host Software Versions to $outputHostsFile..."
$hosts | Export-Csv -Path $outputHostsFile -NoTypeInformation -Encoding UTF8

Write-Host "Exporting Affinity and Anti-Affinity Rules to $outputRulesFile..."
$rules | Export-Csv -Path $outputRulesFile -NoTypeInformation -Encoding UTF8

Write-Host "Data export completed."

# Disconnect from vCenter Server
Disconnect-VIServer -Server $vCenterServer -Confirm:$false

    Output:

    Fetching vSphere Cluster Information…
    Cluster Information:
    Cluster Name: ClusterName
    Number of Hosts: 2
    DRS Enabled: True
    HA Enabled: True
    DRS Automation Level: fullyAutomated

    Cluster Name: Cluster2
    Number of Hosts: 36
    DRS Enabled: True
    HA Enabled: True
    DRS Automation Level: fullyAutomated

    Fetching ESXi Host Software Versions…
    Host Software Versions:
    Host Name: ESX01
    Cluster: ClusterName
    Version: 7.0.2

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Share on Social Media