# encoding: ascii
# api: powershell
# title: LibraryMSCS
# description: Although Windows Server 2008 R2/Powershell 2.0 includes a FailoverClusters module, it only works on a machine running cluster service and NOT against Windows 2003/2008 clusters. This script provides a library functions for working with Microsft Cluster Services (MSCS) using the WMI MSCluster* class. Base class properties are extended with additional info from association classes.
# version: 0.1
# type: script
# author: Chad Miller
# license: CC0
# function: Get-Cluster
# x-poshcode-id: 2426
# x-derived-from-id: 2886
# x-archived: 2016-03-14T03:35:53
# x-published: 2011-12-29T14:53:00
#
#
# ------------------------------------------------------------------------
### <Script>
### <Author>
### Chad Miller 
### </Author>
### <Description>
### Defines functions for working with Microsoft Cluster Service (MSCS)
### </Description>
### <Usage>
### . ./LibraryMSCS.ps1
### </Usage>
### </Script>
# ------------------------------------------------------------------------

#######################
function Get-Cluster
{
    param($cluster)
    
    gwmi -class "MSCluster_Cluster" -namespace "root\mscluster" -computername $cluster

} #Get-Cluster

#######################
function Get-ClusterName
{
    param($cluster)
    
    Get-Cluster $cluster | select -ExpandProperty name

} #Get-ClusterName

#######################
function Get-ClusterNode
{
    param($cluster)
    
    gwmi -class MSCluster_Node -namespace "root\mscluster" -computername $cluster | add-member -pass NoteProperty Cluster $cluster

} #Get-ClusterNode

#######################
function Get-ClusterSQLVirtual
{
    param($cluster)
    
    gwmi -class "MSCluster_Resource" -namespace "root\mscluster" -computername $cluster  | where {$_.type -eq "SQL Server"} | Select @{n='Cluster';e={$cluster}}, Name, State, @{n='VirtualServerName';e={$_.PrivateProperties.VirtualServerName}}, @{n='InstanceName';e={$_.PrivateProperties.InstanceName}}, `
    @{n='ServerInstance';e={("{0}\{1}" -f $_.PrivateProperties.VirtualServerName,$_.PrivateProperties.InstanceName).TrimEnd('\')}}, `
    @{n='Node';e={$(gwmi -namespace "root\mscluster" -computerName $cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($_.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name)}}
    
} #Get-ClusterSQLVirtual

#######################
function Get-ClusterNetworkName
{
    param($cluster)
    
    gwmi -class "MSCluster_Resource" -namespace "root\mscluster" -computername $cluster  | where {$_.type -eq "Network Name"} | Select @{n='Cluster';e={$cluster}}, Name, State, @{n='NetworkName';e={$_.PrivateProperties.Name}}, `
    @{n='Node';e={$(gwmi -namespace "root\mscluster" -computerName $cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($_.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name)}}
       
} #Get-ClusterNetworkName

#######################
function Get-ClusterResource
{
    param($cluster)
    gwmi -ComputerName $cluster -Namespace "root\mscluster" -Class MSCluster_Resource | add-member -pass NoteProperty Cluster $cluster | 
    add-member -pass ScriptProperty Node `
    { gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($this.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name } |
    add-member -pass ScriptProperty Group `
    { gwmi -ComputerName $this.Cluster -Namespace "root\mscluster" -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($this.Name)'} WHERE AssocClass = MSCluster_ResourceGroupToResource" | Select -ExpandProperty Name }
        
} #Get-ClusterResource

#######################
function Get-ClusterGroup
{
    param($cluster)
    
    gwmi -class MSCluster_ResourceGroup -namespace "root\mscluster" -computername $cluster | add-member -pass NoteProperty Cluster $cluster  | 
    add-member -pass ScriptProperty Node `
    { gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_ResourceGroup.Name='$($this.Name)'} WHERE AssocClass = MSCluster_NodeToActiveGroup" | Select -ExpandProperty Name } |
    add-member -pass ScriptProperty PreferredNodes `
    { @(,(gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_ResourceGroup.Name='$($this.Name)'} WHERE AssocClass = MSCluster_ResourceGroupToPreferredNode" | Select -ExpandProperty Name)) }

} #Get-ClusterGroup
