Get cluster information remotely using PowerShell

Today I want to show you how to get cluster information remotely using simple PowerShell function. To pull all needed information about our cluster we will use some dedicated cmdlets which can be found here.

Please check also one of the newest articles about cluster report – link.

Get cluster

On the beginning script will check if module for failover clustering is installed – Import-Module FailoverClusters.

Then we need to ensure that server provided as a parameter exist and cluster service is running. To do this I used simple method which is checking servers IP address. If it can’t determine what is the server address it will throw warning message – Write-Warning “Server does not exist”. Additionally we can use Get-Service command to just simple check if cluster is running. If not, script will ends.

Please remember to add your cluster server names in function parameter:


    param
    (
        [Parameter(Position=1, Mandatory = $True, HelpMessage="Select cluster server", ValueFromPipeline = $true)]
        [ValidateSet("Server01","Server02","Server03","Server04")]
        $Server
    )

Final script:

function Get-ClusterData {
         
    [CmdletBinding()]
    param
    (
        [Parameter(Position=1, Mandatory = $True, HelpMessage="Select cluster server", ValueFromPipeline = $true)]
        [ValidateSet("Server01","Server02","Server03","Server04")]
        $Server
    )
 
        # Checking module
        Try
        {
            Import-Module FailoverClusters -ErrorAction Stop
        }
        Catch
        {
            $_.Exception.Message
            Write-Output "Failover cluster module not installed!"
            Break
        }
 
        Write-Host "Processing $server" -ForegroundColor Green
      
            # Checking if server exist           
            $hostname = [System.Net.Dns]::GetHostAddresses($Server)    
      
                # Proceed if IP address found
                If($hostname.ipaddresstostring)
                {
                    # Check if cluster service is running
                    Try 
                    { 
                        If (!(Get-Service -ComputerName $Server |  Where-Object  {$_.Displayname -match "Cluster*"}).Status -eq "Running") {throw}
                    }
                    Catch 
                    {
                        Write-Warning 'Cluster service is not "Running"'
                        Read-Host -Prompt "Press Enter to exit..."
                        Break
                    } 
 
		            Try
		            {
			            # If checks passed then get cluster information
            		    Invoke-Command -ComputerName $Server -ErrorAction Stop -ScriptBlock{                   
                    	
                            Write-Output "Checking cluster recources:"
		    		        Get-ClusterResource  | select ownernode,iscoreresource,name,resourcetype,state,characteristics | ft -auto
 
                    		Write-Output "Checking cluster owner nodes:"
                    		Get-ClusterGroup  | select cluster,Iscoregroup,ownernode,state,name | sort state | ft -AutoSize
 
                    		Write-Output "Checking cluster nodes:"
                            Get-ClusterNode | select cluster,name,state,nodeweight | ft -auto
 
                    		Write-Output "Checking file share witness availablity:"
                    	 	Get-ClusterResource | Where-Object  {$_.Name -eq "File Share Witness"}  | select name,state | ft -AutoSize
			            }
		            }
		            Catch
		            {
 			            $_.Exception.Message
            		    Break
		            }
                }
                Else
                {
                    Write-Warning "Server does not exist"
                }
 }

6 thoughts on “Get cluster information remotely using PowerShell

    1. Its difficult to tell because you didn’t provide details. Did you put your servers into:
      [ValidateSet(“Server01″,”Server02″,”Server03″,”Server04”)]

      What prompt message do you get? Do you have admin rights to get cluster information? What about connectivity from place where you running this script?

      Please also check if you have failover cluster module installed.
      Get-Module FailoverClusters

      Kind regards

  1. Is there a way to get the active IP of a cluster? i’m trying to write something similar with get-clusterresource but it only comes back with the IP of the DR subnet. It just says Cluster IP Address for the one that is online.

    I’m thinking i’ll have to ping all my clusters to get the current online IP

    1. Hi Alen,
      I’m not sure if this is what you are looking for 🙂 In my environment below code returns IP Address for my cluster:

      $Server = “Cluster01”
      Invoke-Command $Server -ScriptBlock{ Get-ClusterResource | Where-Object {$_.ResourceType -like “*IP Address*” -and $_.state -eq “Online” } | Get-ClusterParameter | Where-Object {$_.name -eq “Address”} | Select-Object ClusterObject,Name,Value}

      1. thx. I was messing around yesterday and figured out something similar. The only thing is that I’m trying to return the state of the resource and it keeps coming back blank. If I just run get-clusterresource it tells me if it’s online or offline, but not in my script

        function Get-SQL-Clusters
        {
        param([string]$server)

        $servers = Get-Content -literalpath “C:\temp\sql_clusters.txt”

        if ($server -ne 1)
        {

        $files = ForEach ($box in $servers) {invoke-command -ComputerName $box {Get-ClusterResource| Get-ClusterParameter} |
        Where-Object {$_.Name -eq “Address”} |
        Format-Table PSComputerName, ClusterObject, State, Name, Value -AutoSize}
        }
        else
        {Write-Warning “‘$server’ is not a valid path.”}
        return $files
        }

        1. Unfortunately State is a property of Get-ClusterResource command only. You can use $State parameter to check if cluster resource is online:

          function Get-Clusters{
          param([string]$server)

          $servers = Get-Content -literalpath “C:\temp\sql_clusters.txt”
          $State = “Online”
          $Array = @()

          If ($server -ne 1)
          {
          ForEach ($box in $servers)
          {
          $ClusterResor = Invoke-Command -ComputerName $box{

          Get-ClusterResource | Where-Object {$_.state -eq $using:State} | Get-ClusterParameter | Where-Object {$_.Name -eq “Address”}

          } -ArgumentList $State | Select-Object ClusterObject, Name, Value -ExcludeProperty RunspaceId

          If($ClusterResor)
          {
          Foreach($Item in $ClusterResor)
          {
          $Object = New-Object PSObject -Property ([ordered]@{

          Server = $Box
          State = $State
          ClusterObject = $item.clusterobject
          Name = $item.name
          Value = $item.value
          })

          # Add custom object to our array
          $Array += $Object
          }
          }
          }
          }
          else
          {
          Write-Warning “‘$server’ is not a valid path.”
          }
          $Array
          }

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.