If(($Service -eq $null) -or ($ProcessError -ne $null))
{
ThrowScriptError "Unable to connect." $e
}
$oGroup = $null
try{
if($Is_OS_More_Than_2012)
{
try{
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}catch{
import-module cimcmdlets
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
}else{
$oGroup = Get-WmiObject -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
}catch{
$e = $_.Exception.Message
}
If(($oGroup -eq $null) -or ($ProcessError -ne $null))
{
ThrowScriptError "The Query returned an invalid result set. Please check to see if this is a valid WMI Query." $e
}
ForEach($oRealGroup in $oGroup)
{
switch($MethodName)
{
"Delete"{
DeleteGroup($oRealGroup)
break;
}
"Move"{
MoveToNextNode($oRealGroup)
break;
}
"Online" {
$ResourceGroupState = ConvertNumericResourceGroupStateToString $oRealGroup.State
Write-Host "The state of the " $oRealGroup.Name " resource group is " $ResourceGroupState
Write-Host "Attempting to bring the resource group online"
if($Is_OS_More_Than_2012)
{
try{
invoke-CimMethod -InputObject $oRealGroup -MethodName BringOnline –Arguments @{ TimeOut = $timeoutInSeconds }
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}catch{
import-module cimcmdlets
invoke-CimMethod -InputObject $oRealGroup -MethodName BringOnline –Arguments @{ TimeOut = $timeoutInSeconds }
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
}else
{
$oRealGroup.BringOnline($timeoutInSeconds)
$oGroup = Get-WmiObject -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
$ResourceGroupState = ConvertNumericResourceGroupStateToString $oGroup.State
Write-Host "The state of the " $oRealGroup.Name " resource group is " $ResourceGroupState
break;
}
"Offline"{
$ResourceGroupState = ConvertNumericResourceGroupStateToString $oRealGroup.State
Write-Host "The state of the " $oRealGroup.Name " resource group is " $ResourceGroupState
Write-Host "Attempting to take the resource group offline"
if($Is_OS_More_Than_2012)
{
try{
invoke-CimMethod -InputObject $oRealGroup -MethodName TakeOffline –Arguments @{ TimeOut = $timeoutInSeconds }
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}catch{
import-module cimcmdlets
invoke-CimMethod -InputObject $oRealGroup -MethodName TakeOffline –Arguments @{ TimeOut = $timeoutInSeconds }
$oGroup = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
}else
{
$oRealGroup.TakeOffline($timeoutInSeconds)
$oGroup = Get-WmiObject -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_ResourceGroup where Name = ""$GroupName""" -ErrorVariable ProcessError -ErrorAction Stop
}
$ResourceGroupState = ConvertNumericResourceGroupStateToString $oGroup.State
Write-Host "The state of the " $oRealGroup.Name " resource group is " $ResourceGroupState
break;
}default{
write-host "[Error]: " "Unrecognized method " $MethodName " used"
}
}
}
write-host "`n[Trace]: " "Finished at machine local time: " (get-date).ToString()
}
Function ConvertNumericResourceGroupStateToString($state)
{
$textualState
}catch{
$e = $_.Exception.Message
}
If(($oActiveNode -eq $null) -or ($e -ne $null)){
ThrowScriptError "The Query for active node returned an invalid result set. Please check to see if this is a valid WMI Query." $e
}
If(($oActive -eq $null) -or ($oActive -eq ""))
{
break
}
If($e -ne $null){
ThrowScriptError "Did not get a Node. Please check to see if this is a valid WMI Query." $e
}
$oNodes = $null
try
{
if($Is_OS_More_Than_2012)
{
try{
$oNodes = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_Node where Name != ""$oActive""" -ErrorVariable ProcessError -ErrorAction Stop
}catch{
import-module cimcmdlets
$oNodes = Get-CimInstance -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_Node where Name != ""$oActive""" -ErrorVariable ProcessError -ErrorAction Stop
}
}else
{
$oNodes = Get-WmiObject -Computer $NodeName -NameSpace 'root\mscluster' -Query "select Name, State from MSCluster_Node where Name != ""$oActive""" -ErrorVariable ProcessError -ErrorAction Stop
}
}catch{
$e = $_.Exception.Message
}
If(($oNodes -eq $null) -or ($e -ne $null)){
ThrowScriptError "The Query for nodes returned an invalid result set. Please check to see if this is a valid WMI Query." $e
}
ForEach($oNextNode in $oNodes)
{
# only move when node is online
If($oNextNode.State -eq 0)
{
$newNodeName = $oNextNode.Name
#==========================================================================
# Main
#==========================================================================