Scripting Example: How to set the MPIO LB policy

The following scripting example shows how to set the MPIO Load Balancing policy via a script calling WMI:

Just to expand on the information below,

FOO Failover Only
RR Round Robin
RRwS Round Robin with Subset
LQD Least Queue Depth
WP Weighted Paths

'-----------------------------------------------------------------------------

'

' SET_LBP.vbs

'

' WScript which can be used to change MPIO Load Balance Policies on Windows 2008

'

' USAGE:

'

' SET_LBP.vbs <FOO | RR | RRwS | LQD | WP>

' Change to FOO: SET_LBP.vbs FOO

' Change to RR: SET_LBP.vbs RR

' Change to RRwS: SET_LBP.vbs RRwS

' Change to LQD: SET_LBP.vbs LQD

' Change to WP: SET_LBP.vbs WP

'

' Return value:

' Success: 0

' Failure: -1

'

'

'-----------------------------------------------------------------------------

'

' Function returning ALUA or nonALUA flag

'

Function aluaflag(theInstance)

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")

Set objShare = objWMIService.Get("DSM_QueryLBPolicy_V2.InstanceName=" & _

"'"&theInstance&"'")

For Each objDsmPath in objShare.LoadBalancePolicy.DSM_Paths

aluaflag = objDsmPath.ALUASupport

Exit for

Next

End Function

Dim Usage

Usage = "USAGE:" & vbCrLf & _

" SET_LBP.vbs <FOO | RR | RRwS | LQD | WP>" & vbCrLf

'

' Make sure we have the correct number of arguments.

'

If ( WScript.Arguments.Count <>1 ) Then

WScript.StdOut.Write Usage

WScript.Quit -1

End If

'

' Extract the arguments.

'

Dim bFlag, BOOL, LBP, LBPath, LBPolicy, foo, rr, rrws, lqd, wp

Dim fooValue, rrValue, rrwsValue, lqdValue, wpValue

LBP = WScript.Arguments(0): foo = "FOO" : rr = "RR" : rrws = "RRwS" : lqd = "LQD" : wp = "WP"

fooValue = StrComp(LBP, foo, 1)

rrValue = StrComp(LBP, rr, 1)

rrwsValue = StrComp(LBP, rrws, 1)

lqdValue = StrComp(LBP, lqd, 1)

wpValue = StrComp(LBP, wp, 1)

' ----------FOO---------

If ( 0 = fooValue) Then

LBPolicy = 1

End If

' ----------RR---------

If ( 0 = rrValue ) Then

LBPolicy = 2

End If

' ----------RRwS---------

If ( 0 = rrwsValue ) Then

LBPolicy = 3

End If

' ----------LQD---------

If ( 0 = lqdValue ) Then

LBPolicy = 4

End If

' ----------WP---------

If ( 0 = wpValue ) Then

LBPolicy = 5

End If

If ( Err.Number = 0 ) Then

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\WMI")

'

' Locate device

'

Set colSWbemObjectSet = objWMIService.InstancesOf("DSM_QueryLBPolicy_V2")

'

' Loop through all devices

'

For Each objSWbemObject In colSWbemObjectSet

'

' Set the boolean flag to FALSE

'

BOOL = 0

WScript.StdOut.Write "InstanceName= "& objSWbemObject.InstanceName & vbCrLf

'

' Flag for ALUA LUN

'

bFlag = aluaflag(objSWbemObject.InstanceName)

'

' If ALUA=True and LBPolicy=RoundRobin, don't apply the change

'

Do

if LBPolicy = 2 Then

if bFlag > 0 Then Exit Do:

End if

'

' Set boolean flag to TRUE if ALUA

'

if bFlag > 0 Then

BOOL = 1

End if

'

'Assign each WbemObject to LBPolicy object

'

Set objLBPolicy = objSWbemObject

'

' Obtain an instance of the the class using a key property value.

'

Set objShare = objWMIService.Get("DSM_LB_Operations.InstanceName=" & _

"'"&objSWbemObject.InstanceName&"'")

'

' Obtain an InParameters object specific to the method we are going to call.

'

Set objInParam = objShare.Methods_("DsmSetLoadBalancePolicyALUA").inParameters.SpawnInstance_()

'

' set to user-defined LB Policy

'

objLBPolicy.LoadBalancePolicy.LoadBalancePolicy = LBPolicy

Dim count

count = 0

'

' If LB Policy is set to FOO

'

' If it is the 1st path, Set to A/O

' If is is not the 1st Path, Set to A/U if ALUA or S/B if not ALUA

'

'

' else LB Policy is set to other than FOO

'

' If It is ALUA

' If TargetPortGroup State is A/O

' Set the associated paths to A/O

' If TargetPortGroup State is A/U

' Set the associated paths to A/U

'

' else It is not ALUA

' Set all paths to A/O

'

'

For Each objDSMPath In objLBPolicy.LoadBalancePolicy.DSM_Paths

if LBPolicy = 1 Then

if count = 0 Then

objDSMPath.PrimaryPath = 1

else

if BOOL = 1 Then

objDSMPath.OptimizedPath = 0

objDSMPath.PrimaryPath = 1

else

objDSMPath.OptimizedPath = 1

objDSMPath.PrimaryPath = 0

End if

End if

else

if BOOL = 1 Then

if objDSMPath.TargetPortGroup_State = 0 Then

objDSMPath.OptimizedPath = 1

objDSMPath.PrimaryPath = 1

else

objDSMPath.OptimizedPath = 0

objDSMPath.PrimaryPath = 1

End if

else

objDSMPath.PrimaryPath = 1

End if

End if

count = count + 1

Next

objInParam.Properties_.item("LoadBalancePolicy") = objLBPolicy.LoadBalancePolicy

WScript.StdOut.Write "DSM_LB_Operations.InstanceName=" & "'"& _

objSWbemObject.InstanceName & "'" & vbCrLf & vbCrLf

Set objOutParams = objWMIService.ExecMethod("DSM_LB_Operations.InstanceName=" & _

"'"&objSWbemObject.InstanceName&"'","DsmSetLoadBalancePolicyALUA", objInParam)

Loop Until True:

Next

If (LBPolicy = 5) Then

WScript.StdOut.Write "LB Policy has successfully changed to WP" & vbCrLf

End If

If (LBPolicy = 4) Then

WScript.StdOut.Write "LB Policy has successfully changed to LQD" & vbCrLf

End If

If (LBPolicy = 3) Then

WScript.StdOut.Write "LB Policy has successfully changed to RRwS" & vbCrLf

End If

If (LBPolicy = 2) Then

If(BOOL = 0) Then

WScript.StdOut.Write vbCrLf & "Found no applicable device for RR" & vbCrLf

else

WScript.StdOut.Write "LB Policy has successfully changed to RR" & vbCrLf

End If

End If

If (LBPolicy = 1) Then

WScript.StdOut.Write "LB Policy has successfully changed to FOO" & vbCrLf

End If

WScript.Quit 0

Else

'

' Something went wrong.

'

WScript.StdOut.Write "Failed to change LB Policy" & Err.Description & vbCrLf

WScript.Quit -1

End If

------------------------