Skip to content

Instantly share code, notes, and snippets.

@fsackur
Created October 11, 2017 00:57
Show Gist options
  • Save fsackur/3ee637d76448602da8fa865bd215c859 to your computer and use it in GitHub Desktop.
Save fsackur/3ee637d76448602da8fa865bd215c859 to your computer and use it in GitHub Desktop.
<#
Usage:
Get-ClusterName
Get-ClusterIpAddress
Get-ClusterSqlInstanceName
#>
#Get all the DNS names used on a cluster
function Get-ClusterName {
Get-ClusterNode | select @{Name = "ClusterResource"; Expression={"Cluster Node"}}, OwnerGroup, Name, DnsSuffix
Get-Cluster | Get-ClusterResource | ?{$_.ResourceType -like "Network Name"} | %{
$_ | select `
@{Name = "ClusterResource"; Expression={$_.Name}},
OwnerGroup,
@{Name="Name"; Expression={$_ | Get-ClusterParameter -Name Name | select -ExpandProperty Value}},
@{Name="DnsSuffix"; Expression={$_ | Get-ClusterParameter -Name DnsSuffix | select -ExpandProperty Value}}
}
}
#Get all the IP addresses used by a cluster
function Get-ClusterIpAddress {
Get-Cluster | Get-ClusterResource | ?{$_.ResourceType -like "IP Address"} | %{
$_ | select `
@{Name = "ClusterResource"; Expression={$_.Name}},
OwnerGroup,
@{Name="Address"; Expression={$_ | Get-ClusterParameter -Name Address | select -ExpandProperty Value}},
@{Name="SubnetMask"; Expression={$_ | Get-ClusterParameter -Name SubnetMask | select -ExpandProperty Value}}
}
}
#Get all SQL cluster instance names
function Get-ClusterSqlInstanceName {
if (-not (Get-Command Get-ClusterName -ErrorAction SilentlyContinue)) {throw "Please also import the Get-ClusterName function from https://github.com/fsackur"}
$ClusterNames = Get-ClusterName;
$ClusterGroups = Get-ClusterGroup
$Namespace = (Get-WmiObject -Namespace "ROOT\Microsoft\SqlServer" -Class "__Namespace" -Filter "Name LIKE 'ComputerManagement%'" | sort Name -Descending | select -First 1 @{Name="Namespace"; Expression={$_.__NAMESPACE + "\" + $_.Name}}).Namespace
$SqlInstanceWmi = Get-WmiObject -Namespace $Namespace -Class "SqlService" -Filter "SqlServiceType = 1"
$SqlInstanceWmi | ForEach-Object {
$InstanceId = $_.ServiceName
[bool]$DefaultInstance = $InstanceId -like "MSSQLSERVER"
$Instance = $InstanceId -replace '^MSSQL\$'
$ClusteredWmi = Get-WmiObject -Namespace $Namespace -Class "SqlServiceAdvancedProperty" -Filter "PropertyName = 'CLUSTERED' AND ServiceName = `'$InstanceId`'"
[bool]$Clustered = $ClusteredWmi.PropertyNumValue -ne 0
# Virtual Server object, for clusters
$VsNameWmi = Get-WmiObject -Namespace $Namespace -Class "SqlServiceAdvancedProperty" -Filter "PropertyName = 'VSNAME' AND ServiceName = `'$InstanceId`'"
$VsName = $VsNameWmi.PropertyStrValue
if ($VsName) {$NetworkName = $VsName} else {$NetworkName = $env:COMPUTERNAME}
if ($DefaultInstance) {$InstanceName = $NetworkName} else {$InstanceName = $NetworkName + "\" + $Instance}
$ClusterName = $ClusterNames | ?{$_.Name -like $NetworkName}
return New-Object psobject -Property @{
InstanceName = $InstanceName;
OwnerGroup = $ClusterName.OwnerGroup
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment