Powershell script to find SQL Cluster Nodes

Below is the powershell script to find sql cluster nodes. This is similar to the sql script here except this is powershell and there is no need to connect to database server.

Param ([String[]] $ServerInstance)
$Nodelist=Invoke-Sqlcmd -Query "IF ServerProperty('ISClustered')=1 Begin SELECT NodeName,case when serverproperty('instancename') is null then 'MSSQLSERVER' Else serverproperty('instancename') End as
InstanceName,ServerProperty('ServerName') as DatabaseServer FROM sys.dm_os_cluster_nodes End" -ServerInstance $ServerInstance
for($i=0;$i -le $Nodelist.count-1; $i++)
{
get-service -computerName $Nodelist[$i].NodeName |where-Object {$_.DisplayName -like '*'+$NodeList[$i].InstanceName+'*'}|
Select-Object @{Name="ComputerName";Expression={$Nodelist[$i].NodeName}},@{Name="DatabaseServer";Expression={$Nodelist[$i].DatabaseServer}},DisplayName,Status | FT -Auto
}

Advertisement