PoshCode Logo PowerShell Code Repository

LibraryMSCS by Chad Miller 3 years ago (modification of post by Chad Miller view diff)
View followups from Chad Miller | diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/2426"></script>download | new post

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.

  1. # ------------------------------------------------------------------------
  2. ### <Script>
  3. ### <Author>
  4. ### Chad Miller
  5. ### </Author>
  6. ### <Description>
  7. ### Defines functions for working with Microsoft Cluster Service (MSCS)
  8. ### </Description>
  9. ### <Usage>
  10. ### . ./LibraryMSCS.ps1
  11. ### </Usage>
  12. ### </Script>
  13. # ------------------------------------------------------------------------
  14.  
  15. #######################
  16. function Get-Cluster
  17. {
  18.     param($cluster)
  19.    
  20.     gwmi -class "MSCluster_Cluster" -namespace "root\mscluster" -computername $cluster
  21.  
  22. } #Get-Cluster
  23.  
  24. #######################
  25. function Get-ClusterName
  26. {
  27.     param($cluster)
  28.    
  29.     Get-Cluster $cluster | select -ExpandProperty name
  30.  
  31. } #Get-ClusterName
  32.  
  33. #######################
  34. function Get-ClusterNode
  35. {
  36.     param($cluster)
  37.    
  38.     gwmi -class MSCluster_Node -namespace "root\mscluster" -computername $cluster | add-member -pass NoteProperty Cluster $cluster
  39.  
  40. } #Get-ClusterNode
  41.  
  42. #######################
  43. function Get-ClusterSQLVirtual
  44. {
  45.     param($cluster)
  46.    
  47.     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}}, `
  48.     @{n='ServerInstance';e={("{0}\{1}" -f $_.PrivateProperties.VirtualServerName,$_.PrivateProperties.InstanceName).TrimEnd('\')}}, `
  49.     @{n='Node';e={$(gwmi -namespace "root\mscluster" -computerName $cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($_.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name)}}
  50.    
  51. } #Get-ClusterSQLVirtual
  52.  
  53. #######################
  54. function Get-ClusterNetworkName
  55. {
  56.     param($cluster)
  57.    
  58.     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}}, `
  59.     @{n='Node';e={$(gwmi -namespace "root\mscluster" -computerName $cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($_.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name)}}
  60.        
  61. } #Get-ClusterNetworkName
  62.  
  63. #######################
  64. function Get-ClusterResource
  65. {
  66.     param($cluster)
  67.     gwmi -ComputerName $cluster -Namespace "root\mscluster" -Class MSCluster_Resource | add-member -pass NoteProperty Cluster $cluster |
  68.     add-member -pass ScriptProperty Node `
  69.     { gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($this.Name)'} WHERE AssocClass = MSCluster_NodeToActiveResource" | Select -ExpandProperty Name } |
  70.     add-member -pass ScriptProperty Group `
  71.     { gwmi -ComputerName $this.Cluster -Namespace "root\mscluster" -query "ASSOCIATORS OF {MSCluster_Resource.Name='$($this.Name)'} WHERE AssocClass = MSCluster_ResourceGroupToResource" | Select -ExpandProperty Name }
  72.        
  73. } #Get-ClusterResource
  74.  
  75. #######################
  76. function Get-ClusterGroup
  77. {
  78.     param($cluster)
  79.    
  80.     gwmi -class MSCluster_ResourceGroup -namespace "root\mscluster" -computername $cluster | add-member -pass NoteProperty Cluster $cluster  |
  81.     add-member -pass ScriptProperty Node `
  82.     { gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_ResourceGroup.Name='$($this.Name)'} WHERE AssocClass = MSCluster_NodeToActiveGroup" | Select -ExpandProperty Name } |
  83.     add-member -pass ScriptProperty PreferredNodes `
  84.     { @(,(gwmi -namespace "root\mscluster" -computerName $this.Cluster -query "ASSOCIATORS OF {MSCluster_ResourceGroup.Name='$($this.Name)'} WHERE AssocClass = MSCluster_ResourceGroupToPreferredNode" | Select -ExpandProperty Name)) }
  85.  
  86. } #Get-ClusterGroup

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me