Skip to content

Get-Cluster

VMware: Retrieves clusters available on a vCenter Server system

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core

[CmdletBinding(DefaultParameterSetName = "byName")]
Param(
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [Parameter(Mandatory = $true, ParameterSetName = "byVM")]
    [string]$VIServer,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [Parameter(Mandatory = $true, ParameterSetName = "byName")]
    [Parameter(Mandatory = $true, ParameterSetName = "byVM")]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true, ParameterSetName = "byID")]
    [string]$ClusterID,
    [Parameter(Mandatory = $true, ParameterSetName = "byVM")]
    [string]$VM,
    [Parameter(ParameterSetName = "byName")]
    [string]$ClusterName,
    [Parameter(ParameterSetName = "byID")]
    [Parameter(ParameterSetName = "byName")]
    [Parameter(ParameterSetName = "byVM")]
    [switch]$NoRecursion,
    [Parameter(ParameterSetName = "byID")]
    [Parameter(ParameterSetName = "byName")]
    [Parameter(ParameterSetName = "byVM")]
    [ValidateSet('*', 'Name', 'Id', 'HATotalSlots', 'HAUsedSlots', 'HAEnabled', 'HASlotMemoryGB', 'HASlotNumVCpus')]
    [string[]]$Properties = @('Name', 'Id', 'HATotalSlots', 'HAUsedSlots', 'HAEnabled', 'HASlotMemoryGB', 'HASlotNumVCpus')
)

Process {
    $vmServer = $null
    try {
        if ($Properties -contains '*') {
            $Properties = @('*')
        }
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"

        if ($PSCmdlet.ParameterSetName -eq "byID") {
            $result = Get-Cluster -Server $vmServer -Id $ClusterID -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object $Properties
        }
        elseif ($PSCmdlet.ParameterSetName -eq "byVM") {
            $result = Get-Cluster -Server $vmServer -VM $VM -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object $Properties
        }
        else {
            if ([System.String]::IsNullOrWhiteSpace($ClusterName)) {
                $result = Get-Cluster -Server $vmServer -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object $Properties
            }
            else {
                $result = Get-Cluster -Server $vmServer -Name $ClusterName -NoRecursion:$NoRecursion -ErrorAction Stop | Select-Object $Properties
            }
        }

        foreach ($item in $result) {
            $item | Add-Member -NotePropertyName 'Timestamp' -NotePropertyValue $timestamp -Force
            Write-Output $item
        }
    }
    catch {
        throw
    }
    finally {
        if ($null -ne $vmServer) {
            Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue
        }
    }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

ID of the cluster to retrieve

Name of a virtual machine to filter clusters that contain it

Name of the cluster to retrieve; if empty, all clusters are retrieved

Off

Disables recursive behavior of the command

List of properties to expand; use * for all properties

An interactive directory of PowerShell scripts.