Lenovo BladeCenter PowerOff Module

IBM.BladeCenter.PowerOff.Module (WriteActionModuleType)

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
OutputTypeSystem.CommandOutput

Member Modules:

ID Module Type TypeId RunAs 
WA WriteAction Microsoft.Windows.ScriptWriteAction Default

Source Code:

<WriteActionModuleType ID="IBM.BladeCenter.PowerOff.Module" Accessibility="Internal">
<Configuration>
<xsd:element name="ModuleType" type="xsd:string"/>
<xsd:element name="IPAddress" type="xsd:string"/>
<xsd:element name="BayNumber" type="xsd:string"/>
<xsd:element name="CommunityString" type="xsd:string"/>
</Configuration>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="WA" TypeID="Windows!Microsoft.Windows.ScriptWriteAction">
<ScriptName>IBMBladeCenterPowerOffModule.vbs</ScriptName>
<Arguments>"$Config/ModuleType$" "$Config/IPAddress$" "$Config/BayNumber$" "$Config/CommunityString$"</Arguments>
<ScriptBody><Script>
'----------------------------------------------------------------------------
'The constant should be equal to the input parameter of each refresh task on
'module.
'Debug Log Definition
Const HKEY_LOCAL_MACHINE = &amp;H80000002
Const DEBUG_INFO_KEY = "SOFTWARE\Lenovo\Lenovo SCOM MP\Debug"
Const DEBUG_LEVEL_NAME = "Level"
Const IBMHWMP_LICENSE_KEY = "Software\IBM\Systems Management Integrations\License"
Const LenovoHWMP_LICENSE_KEY = "Software\Lenovo\Systems Management Integrations\License"
Const IBMHWMP_LICENSE_SETUP_VERSION ="Version" 'suppose it is 3.0.6, the license tool's version, I saw the server side and endpoint is different, so this is useless.
Const IBMHWMP_LICENSE_TOKEN_NAME = "v1-0002.A"
Const ERROR_LEVEL = 1
Const WARNING_LEVEL = 2
Const WARNING_LEVEL_DEV = 3
Const INFO_LEVEL = 4
Const INFO_LEVEL_FUNCTION = 5
Const INFO_LEVEL_DETAIL = 6

Const CHASSIS_TYPE = "CHASSIS"
Const BLADE_TYPE = "BLADE"
Const COOLING_TYPE = "COOLING"
Const IO_TYPE = "IO"
Const MEDIA_TYPE = "MEDIA"
Const MM_TYPE = "MM"
Const POWER_TYPE = "POWER"
Const STORAGE_TYPE = "STORAGE"
'-----------------------------------------------------------------------------
SetLocale("en-us")
Dim SCOMServer, IPAddress, ModuleType, bayNumber,modBay, primaryBayNumber
Dim bladeChassis
Dim oReg, licenseLevel, licenseToken
Dim Regdebug
Regdebug = -1

SCOMServer = null
If (WScript.Arguments.Count &lt; 4) Then
WScript.Quit -1
End If
ModuleType = WScript.Arguments.Item(0)
IPAddress = WScript.Arguments.Item(1)
modBay = WScript.Arguments.Item(2)
bayNumber = GetModuleBayNumber(modBay)
CommunityString = WScript.Arguments.Item(3)
CommunityString = Chr2Unicode(fDecode(CommunityString))
Set oAPI = CreateObject("MOM.ScriptAPI")

Call GetDebugLevel()

WScript.Echo("ModuleType = " + ModuleType)
WScript.Echo("IPAddress = "+ IPAddress)
WScript.Echo("bayNumber = " &amp; bayNumber)

primaryBayNumber = split( bayNumber, ",")

Call LogDebugString("ModuleType = " + ModuleType, INFO_LEVEL_DETAIL)
Call LogDebugString("IPAddress = "+ IPAddress, INFO_LEVEL_DETAIL)
Call LogDebugString("bayNumber = "+ bayNumber + " primaryBayNumber = " + primaryBayNumber(0), INFO_LEVEL_DETAIL)

Set oReg = GetObject( "winmgmts://localhost/root/default:StdRegProv" )

bccInstalled = IsInstallBCCProject()
If bccInstalled = true Then

If (Isnull(IPAddress)) then
Call LogDebugString("Error: Bad or missing ipaddress!", WARNING_LEVEL)
Else
Set bladeChassis = WScript.CreateObject("Lenovo.SystemsManagement.MMClassLibrary.BladeChassis")
Dim port
port = 0
Dim init
init = -1
' Don't forget to set SNMP in MM Web console, and "access type" should be "Set" value
init = bladeChassis.InitializeSocketConnection(IPAddress, CommunityString, port,"V1")
If init = 0 Then
Select Case ModuleType
case BLADE_TYPE
Call LogDebugString("Lenovo BladeCenter Blade Power Off. Blade: " + primaryBayNumber(0), INFO_LEVEL_DETAIL)
init = bladeChassis.PowerOffBlade(primaryBayNumber(0))
If init &lt;&gt; 0 Then
Call LogDebugString("Cannot Power Off Blade!", WARNING_LEVEL_DEV)
WScript.Echo("----------------------------------------" &amp; vbcrlf &amp; _
"--------------- Task failed ------------" &amp; vbcrlf &amp; _
"----------------------------------------" &amp; vbcrlf &amp; _
"Cannot Power Off Blade! " &amp; vbcrlf &amp; _
"Return code from MMClassLibrary.BladeChassis.PowerOnBlade: " &amp; init &amp; vbcrlf &amp; _
"----------------------------------------" )
End If
End Select
End If
bladeChassis.CloseSocketConnection()
End If
End If

Function GetModuleBayNumber(tempModBay)
Dim n
n = InStr(1,tempModBay," Bay ",1)
If n &gt; 0 then
GetModuleBayNumber = Right(tempModBay, Len(tempModBay) - n - 4)
Else
GetModuleBayNumber = 0
End If
End Function

Function Chr2Unicode(byval str)
Dim st, t, i
For i = 1 To Len(str) Step 2
t = Mid(str, i, 1)
st = st + t
Next
Chr2Unicode = st
End Function

Function fDecode(sStringToDecode)
'This function will decode a Base64 encoded string and returns the decoded string.
Const CharList = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
Dim iDataLength, sOutputString, iGroupInitialCharacter
sStringToDecode = Replace(Replace(Replace(sStringToDecode, vbCrLf, ""), vbTab, ""), " ", "")
iDataLength = Len(sStringToDecode)
If iDataLength Mod 4 &lt;&gt; 0 Then
fDecode = "Bad string passed to fDecode() function."
Exit Function
End If
For iGroupInitialCharacter = 1 To iDataLength Step 4
Dim iDataByteCount, iCharacterCounter, sCharacter, iData, iGroup, sPreliminaryOutString
iDataByteCount = 3
iGroup = 0
For iCharacterCounter = 0 To 3
sCharacter = Mid(sStringToDecode, iGroupInitialCharacter + iCharacterCounter, 1)
If sCharacter = "=" Then
iDataByteCount = iDataByteCount - 1
iData = 0
Else
iData = InStr(1, CharList, sCharacter, 0) - 1
If iData = -1 Then
fDecode = "Bad string passed to fDecode() function."
Exit Function
End If
End If
iGroup = 64 * iGroup + iData
Next
iGroup = Hex(iGroup)
iGroup = String(6 - Len(iGroup), "0") &amp; iGroup
sPreliminaryOutString = Chr(CByte("&amp;H" &amp; Mid(iGroup, 1, 2))) &amp; Chr(CByte("&amp;H" &amp; Mid(iGroup, 3, 2))) &amp; Chr(CByte("&amp;H" &amp; Mid(iGroup, 5, 2)))
sOutputString = sOutputString &amp; Left(sPreliminaryOutString, iDataByteCount)
Next
fDecode = sOutputString
End Function

Sub GetDebugLevel()
Dim oReg,RC_Code
Set oReg = GetObject("winmgmts://localhost/root/default:StdRegProv")
If (not(oReg is nothing)) Then
RC_Code= oReg.GetStringValue(HKEY_LOCAL_MACHINE, DEBUG_INFO_KEY, DEBUG_LEVEL_NAME, Regdebug)
If ( RC_Code&lt;&gt;0) Then
Regdebug = -1
End If
End If
End Sub

'-----------------------------------------------------------------------------
'start of IsInstallBCCProject()
'This Function judges whether the target machine installed or not
'IBM Assemblies For Systems Management Support.
'-----------------------------------------------------------------------------
Function IsInstallBCCProject()
Dim IsInstalled
IsInstalled = true
Dim obj
Set obj = WScript.CreateObject("WScript.Shell")
Dim regpath
regpath = "HKEY_LOCAL_MACHINE\SOFTWARE\Lenovo\Systems Management Integrations\Lenovo HW Mgmt Pack for OpsMgr\Path"
Dim InstallPath
InstallPath = obj.RegRead(regpath)
If isNull(InstallPath) then
regpath = "HKEY_LOCAL_MACHINE\SOFTWARE\IBM\Systems Management Integrations\IBM HW Mgmt Pack for OpsMgr\Path"
InstallPath = obj.RegRead(regpath)
End If
If not isNull(InstallPath) then
IsInstalled = true
Else
IsInstalled = false
WScript.Echo("!!!!!!!!!!Error: BCC Not Installed!!!!!!!!!!!!")
End If
IsInstallBCCProject = IsInstalled
'WScript.Echo(IsInstalled)
End Function
'-------------------------------------------------------------------------------
'end of IsInstallBCCProject()
'-------------------------------------------------------------------------------

Function GetLicenseLevel(ByRef sLicLevel, ByRef sLicTok)


'-------------------------------------------------------------------------------
'add for Trial License start
sLicLevel = null
sLicTok = null

Dim trialresult
trialresult = false
Set objtr=WScript.CreateObject("Lenovo.SystemsManagement.MMClassLibrary.LicenseControl")

trialresult=objtr.TrialLicenseOK()
If trialresult Then
sLicLevel="255"
sLicTok="My4Y"

Exit Function
Else
sLicLevel=""
Call LogDebugString(objtr.GetTrialLicenseInfomation(), INFO_LEVEL_DETAIL)
End If

'add for Trial License end
'-------------------------------------------------------------------------------
sLicLevel = null
sLicTok = null
If IsEmpty(oReg) Or IsNull(oReg) Or (oReg is Nothing) Then
Exit Function
End If

Dim oLicValidator, aLicLevelERs, oBestLevelER
Set oLicValidator = new LicManager
Call oLicValidator.LoadFromRegistry()
aLicLevelERs = oLicValidator.LevelERs
If IsArray(aLicLevelERs) Then
sLicTok = oLicValidator.LicenseToken
Call oLicValidator.FindBestFeatLevel(oBestLevelER)
If Not IsNull(oBestLevelER) Then
sLicLevel = oBestLevelER.ToString()
Else
Call LogDebugString("The best matched feature level could " + _
"not be obtained on this Lenovo License Server", WARNING_LEVEL)
End If
Else
Call LogDebugString("No feature levels activated on this Lenovo License Server", WARNING_LEVEL)
End If

End Function

'-------------------------------------------------------------------------------
'start of LogDebugString()
'-------------------------------------------------------------------------------
Sub LogDebugString(debugString, debugLevel)
If (Int(Regdebug) &gt;= Int(debugLevel)) Then
Call oAPI.LogScriptEvent("IBMBladeCenterPowerOffModule.vbs", 210, DebugLevelToLogLevel(debugLevel), debugString)
End If
End Sub
Function DebugLevelToLogLevel(debugLevel)
DebugLevelToLogLevel = 4
If debugLevel &lt;= 1 then
DebugLevelToLogLevel = 1
ElseIF debugLevel &lt;= 3 then
DebugLevelToLogLevel = 2
Else
DebugLevelToLogLevel = 4
End If
End Function

class LicManager

private m_aLevelERs
public property get LevelERs
LevelERs = m_aLevelERs
end property

private m_sLicToken
public property get LicenseToken
LicenseToken = m_sLicToken
end property

private sub Class_Initialize()
m_aLevelERs = null
m_sLicToken = ""
end sub

public sub LoadFromRegistry()

dim nLevelsLoaded
nLevelsLoaded = 0
m_aLevelERs = null
m_sLicToken = ""
licenseFlag = 1

dim aKeyEntryNames, aKeyEntryTypes
Call LogDebugString(".. LicManager: loading from registry key '" + _
LenovoHWMP_LICENSE_KEY + "'", INFO_LEVEL_FUNCTION)
Call oReg.EnumValues( HKEY_LOCAL_MACHINE, LenovoHWMP_LICENSE_KEY, _
aKeyEntryNames, aKeyEntryTypes )

if isNull( aKeyEntryNames ) or not(isArray( aKeyEntryNames )) then
Call LogDebugString(".. LicManager: loading from registry key '" + _
IBMHWMP_LICENSE_KEY + "'", INFO_LEVEL_FUNCTION)
Call oReg.EnumValues( HKEY_LOCAL_MACHINE, IBMHWMP_LICENSE_KEY, _
aKeyEntryNames, aKeyEntryTypes )
licenseFlag = 0
end if
if isNull( aKeyEntryNames ) or not(isArray( aKeyEntryNames )) then
exit sub
end if

Call LogDebugString(".. LicManager: # of registry value entries = " + _
CStr(UBound(aKeyEntryNames)+1), INFO_LEVEL_DETAIL)



dim ii
for ii=0 to UBound(aKeyEntryNames)
if 1 = aKeyEntryTypes( ii ) then ' 1 = REG_SZ
if IBMHWMP_LICENSE_TOKEN_NAME = aKeyEntryNames(ii) then
dim sLevelERsEncoded, sLevelERsDecoded, asSplitERs
if licenseFlag = 1 then
call oReg.GetStringValue( HKEY_LOCAL_MACHINE, LenovoHWMP_LICENSE_KEY, _
aKeyEntryNames(ii), sLevelERsEncoded )
else
call oReg.GetStringValue( HKEY_LOCAL_MACHINE, IBMHWMP_LICENSE_KEY, _
aKeyEntryNames(ii), sLevelERsEncoded )
end if

if IsNull(sLevelERsEncoded) Or IsEmpty(sLevelERsEncoded) then
call LogDebugString("!! LicManager: Registry value " + _
aKeyEntryNames(ii) + " null or empty", ERROR_LEVEL)
else
call LogDebugString(".. LicManager: " + aKeyEntryNames(ii) + _
"='" + sLevelERsEncoded + "'", INFO_LEVEL_DETAIL)
m_sLicToken = sLevelERsEncoded
sLevelERsDecoded = fDecode(sLevelERsEncoded)
call LogDebugString(".. LicManager: " + aKeyEntryNames(ii) + _
"='" + sLevelERsDecoded + "' [Decoded]", INFO_LEVEL_DETAIL)
asSplitERs = split(sLevelERsDecoded, vbTab)
if UBound(asSplitERs) &gt;= 0 then
dim jj
for jj=0 to UBound(asSplitERs)
call LogDebugString(".. LicManager: Retrieved '" + _
asSplitERs(jj) + "'", INFO_LEVEL_DETAIL)
dim oLevelER
set oLevelER = new FeatLevelER
if 0 &lt;= oLevelER.ParseLevelER( asSplitERs(jj) ) then
if 0 = nLevelsLoaded then
m_aLevelERs = array( 0 ) ' create a 1-element array of '0'
else
redim preserve m_aLevelERs( nLevelsLoaded )
end if
set m_aLevelERs( nLevelsLoaded ) = oLevelER
nLevelsLoaded = nLevelsLoaded + 1
call LogDebugString("&gt;&gt; LicManager: Parsed " + _
oLevelER.ToString(), INFO_LEVEL)
end if
next
end if
end if
end if
end if
next

end sub ' LoadFromRegistry

public sub FindBestFeatLevel( byRef oBestLevelER )
oBestLevelER = null
if not( isArray( m_aLevelERs ) ) then
exit sub
end if
if UBound(m_aLevelERs) &gt;= 0 then
dim ii, oLevelER
set oBestLevelER = m_aLevelERs(0)
for ii=1 to UBound(m_aLevelERs)
set oLevelER = m_aLevelERs(ii)
if (oLevelER.LevelMajor &gt; oBestLevelER.LevelMajor) Or _
(oLevelER.LevelMajor = oBestLevelER.LevelMajor And _
oLevelER.LevelMinor &gt; oBestLevelER.LevelMinor) then
set oBestLevelER = oLevelER
end if
next
end if
end sub

end class

class FeatLevelER

private m_levelMajor
private m_levelMinor

public property get LevelMajor
LevelMajor = m_levelMajor
end property
public property get LevelMinor
LevelMinor = m_levelMinor
end property

private sub Class_Initialize()
end sub

public function ParseLevelER( byVal sEntryValue )
dim retCode
retCode = 0
on error resume next
dim asSplit
asSplit = split( sEntryValue, "." )
if UBound(asSplit) &lt;&gt; 1 then
call LogDebugString("!! FeatLevelER.ParseLevelER: unexpected number of items in " + _
"feature level record (" + UBound(asSplit) + ")", ERROR_LEVEL)
retCode = -1
else
m_levelMajor = CInt(asSplit(0))
m_levelMinor = CInt(asSplit(1))
call LogDebugString(".. FeatLevelER.ParseLevelER: MajorVersion = " + _
CStr(m_levelMajor) + ", MinorVersion = " + CStr(m_levelMinor), INFO_LEVEL_DETAIL)
end if
on error goto 0
ParseLevelER = retCode
end function

public function ToString()
ToString = CStr(m_levelMajor) + "." + CStr(m_levelMinor)
end function

end class
</Script></ScriptBody>
<TimeoutSeconds>300</TimeoutSeconds>
<EventPolicy>
<Severity>2</Severity>
<StdOutMatches Operator="MatchesRegularExpression">Task failed</StdOutMatches>
<StdErrMatches/>
<ExitCodeMatches/>
</EventPolicy>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="WA"/>
</Composition>
</Composite>
</ModuleImplementation>
<OutputType>System!System.CommandOutput</OutputType>
<InputType>System!System.BaseData</InputType>
</WriteActionModuleType>