Submitted By: Damon Fischer

Moves cluster resources between cluster nodes. "Drain" moves all resources off a cluster node so maintenance can be performed. "Balance" moves resources back to their preferred owners when maintenance is complete. Note: Must have the preferred owners field set on all cluster groups.

Visual Basic
Edit|Remove
'==============================================================================
'LANG       : VBScript
'NAME       : ClusterResourceMover.vbs
'AUTHOR     : Damon Fischer
'DATE       : 8/14/2006
'DESCRIPTION: Moves cluster resources so the cluster node can be patched.
'COMMENT    : "Script Usage: cscript DrainCluster.vbs [Drain] | [Balance] | [PullAll]"
'==============================================================================
'==============================================================================
'Variable Declarations
Option Explicit
On error resume next

Const ComputerName = ""
Const intTimeOut=600  'Number of seconds to wait for cluster resource to fail over.

Dim WshNetwork
Dim objCluster
Dim res
Dim resGroup
Dim strLocalServerName
Dim objPrefNodes
Dim objPrefNode
Dim strPrefNode
Dim strCurrentNode
Dim bolInCurrent
Dim strAction
Dim intExitCode

'==============================================================================
'==============================================================================
'Main Body

if WScript.Arguments.Count <> 1 then 
	wscript.echo "ERROR: Invalid Argument"
	Wscript.Echo "Script Usage: cscript DrainCluster.vbs [Drain] | [Balance] | [PullAll]"
	Wscript.quit (1)
End if

Select Case UCase(WScript.Arguments.Item(0))
	Case "DRAIN"
		strAction="D"
	Case "BALANCE"
		strAction="B"
	Case "PULLALL"
		strAction="P"		
	Case Else
		wscript.echo "ERROR: Invalid Argument"
		Wscript.Echo "Script Usage: cscript DrainCluster.vbs [Drain] | [Balance] | [PullAll]"
		Wscript.quit (1)
End Select

wscript.echo "Script Mode:" & WScript.Arguments.Item(0)

'Get local machine name
Set WshNetwork = WScript.CreateObject("WScript.Network")
strLocalServerName = WshNetwork.ComputerName 
wscript.echo "Running on Server " &  strLocalServerName

Set objCluster = CreateObject("MSCluster.Cluster")
objCluster.Open ComputerName

'Initialize Exit code at 0
intExitCode=0

For Each res in objCluster.Nodes
	For Each resGroup in res.ResourceGroups
    	wscript.echo VBCrlf
        wscript.echo "Resource Group Name:" & resGroup.name 
        wscript.echo "Current Node:" & resGroup.OwnerNode.Name
        wscript.echo "Current State:" & resgroup.state 
        strCurrentNode = resGroup.OwnerNode.Name
        strPrefNode = resgroup.PreferredOwnerNodes.item(1).name
        wscript.echo "Preferred Node: " & strPrefNode
		if strPrefNode = strCurrentNode then
			wscript.echo "Resource Group is on preferred Node"
			bolInCurrent = true
		Else 
			wscript.echo "Resource Group is not on preferred Node"
			bolInCurrent = false
		end if

		if bolInCurrent=false and strAction="B" then
			wscript.echo "Moving Group to " & strPrefNode
			resGroup.move intTimeOut, resgroup.PreferredOwnerNodes.item(1) ', strPrefNode
		End if
		
        if resGroup.OwnerNode.Name = strLocalServerName and strAction="D" then
		    wscript.echo "Moving Group " & resGroup.name 
            resGroup.move intTimeOut
        end if
        
        if strAction="K" then
		    wscript.echo "Moving Group " & resGroup.name 
            resGroup.move intTimeOut
        end if

        if resGroup.OwnerNode.Name <> strLocalServerName and strAction="P" then
		    wscript.echo "Moving Group " & resGroup.name 
            resGroup.move intTimeOut, TRIM(strLocalServerName)
        end if

        if resgroup.state <> 0 Then
			wscript.echo "!!WARNING!! Resource Group not in Online State"
		    intExitCode=100
		End if 
    Next
Next


'Check to make sure Drain or Balance is successful and double check resource state
For Each res in objCluster.Nodes
	For Each resGroup in res.ResourceGroups
		if resgroup.state <> 0 Then
			wscript.echo "!!WARNING!! Resource Group not in Online State"
			intExitCode=110
		Else 
			wscript.echo "State Check Completed Successfully"
		End if 

		if resGroup.OwnerNode.Name = strLocalServerName and strAction="D" then
			wscript.echo "!!WARNING!! Resource Group still on this server"
			intExitCode=200
		Elseif strAction="D" then
			wscript.echo "Drain Check completed successfully"
		end if
		
		if resGroup.OwnerNode.Name <> resgroup.PreferredOwnerNodes.item(1).name and strAction="B" then
			wscript.echo "!!WARNING!! Resource Group not on PreferredNode"
			intExitCode=300
		Elseif strAction="B" then
			wscript.echo "Balance Check completed successfully"
		end if
		        
        		
	Next
Next

'Cleanup
Set objCluster = nothing

' ---------------------------------------------------
'Check for Errors and Exit
' ---------------------------------------------------
if Err.Number <> 0 then
    wscript.echo ("Error # " & CStr(Err.Number) & " " & Err.Description)
    intExitCode=Err.Number
End if

wscript.echo "Exiting with code:" & intExitCode
wscript.quit(intExitCode)