<package>
<job id="main">
<comment>
------------------------------------------------------
ChangeSetting.wsf
Last modified: 7-22-2005


A Windows Scripting Host example to modify BIOS setting
information from the HP Client Management Interface.

This script relies on the HP CMI Password Control to 
retrieve the appropriate BIOS password from user input.
Be sure to register this control on any system where
the script will executed with the command:
   
   hppwdctl /install


Copyright (c) 2005, Hewlett-Packard Development Company
<reference object="hpPwdCtl.PasswordEdit" />
------------------------------------------------------
</comment>
<runtime>
  <description>
Description:

    This script example is designed to modify BIOS setting values
    from the HP Client Management Interface.
  </description>
  <named
    name = "setting"
    helpstring = "A specific setting to modify.  If a particular setting
           contains spaces, be sure to enclose the entire argument in
           quotation marks."
    type = "string"
    required = "true"
  />
  <named
    name = "value"
    helpstring = "The new setting value.  If a particular value contains
           spaces, be sure to enclose the entire argument in quotation
           marks."
    type = "string"
    required = "true"
  />
  <unnamed
    name = "computers"
    helpstring = "An optional list of computers to query specified by name
           or IP address, otherwise the local system is used.  The dot
           nomenclature can also be used to include the local computer in
           the computer list."
    many = "true"
    required = "false"
  />
  <example>
Example:

    ChangeSetting.wsf /setting:"Removable Media Boot" /value:Disable . remote1 remote2
  </example>
</runtime>

<script language="VBScript">

On Error Resume Next

' The following flags values useful in changing the behavior
' of WMI functions, such as instance enumeration.
Const wbemFlagReturnImmediately = 16
Const wbemFlagForwardOnly = 32

status = 99

' Check to ensure we have our parameters.
' If not, show the usage message.
If Not (WScript.Arguments.Named.Exists("setting") And _
        WScript.Arguments.Named.Exists("value")) Then
  WScript.Arguments.ShowUsage
  WScript.Quit(status)
End If

' Determine the list of computers to process
Dim argsUnnamed, strComputer
Set argsUnnamed = WScript.Arguments.Unnamed

If argsUnnamed.Count > 0 Then
  Set arrComputers = argsUnnamed

  For Each strComputer In argsUnnamed
    Call ChangeSetting(status, _
                       strComputer, _
                       WScript.Arguments.Named("setting"), _
                       WScript.Arguments.Named("value"))
  Next

Else
  Call ChangeSetting(status, _
                     ".", _
                     WScript.Arguments.Named("setting"), _
                     WScript.Arguments.Named("value"))
End If

WScript.Quit(status)

' ----------------------------------------------------
' This subroutine attempts to change the particular
' BIOS setting pointed to by strSettingName to the
' value specified by strSetting value for the given
' computer.
Sub ChangeSetting(ByRef returnedStatus, _
                  ByVal strComputerName, _
                  ByVal strSettingName, _
                  ByVal strSettingValue)

  On Error Resume Next
  
  returnedStatus = 98
  
  lFlags = 0
  bPasswordSet = 0
  strPassword = ""

  ' Set the WMI connection strings.
  strService = "winmgmts:{impersonationlevel=impersonate}//"
  strNamespace = "/root/HP/InstrumentedBIOS"

  WScript.Echo "Trying to connect to HP WMI BIOS... (" & Timer & ")"

  LoopCount = 0
  Do
    ' Connect to our namespace.
    Set objWMIService = GetObject(strService & _
                                  strComputerName & _
                                  strNamespace)
    
    If Err.Number = 0 Then
      Exit Do
    End If
  
    LoopCount = LoopCount + 1
    If LoopCount = 12 Then
      Exit Do
    End If

    ' If we did not connect to WMI, the service could still be initializing.
    ' Wait ten seconds and reattempt the connection.
    '
    ' This issue happens under Windows PE.
    WScript.Sleep(10000)

    WScript.Echo "    retry #" & LoopCount & "(" & Timer & ")"
  Loop

  If Err.Number <> 0 then
    WScript.Echo "    ***ERROR*** Failed to connect to HP InstrumentedBIOS!"
    Err.Clear
    Exit Sub
  End If

  WScript.Echo "    --> successfully connected! (" & Timer & ")"


  ' Create our query string for determining the current BIOS password
  ' state of the system.
  strPwdQuery = "SELECT * FROM HP_BIOSPassword WHERE " & _
                "Name=""Setup Password"""

  ' Determine if we need to get a password for this machine.
  WScript.Echo "Trying to get Setup Password setting... (" & Timer & ")"

  bPasswordSet = 0

  Set colItems = objWMIService.ExecQuery(strPwdQuery,,lFlags)

  If Err.Number <> 0 then
    WScript.Echo "    ***ERROR*** Failed to get status of setup password!"
    Err.Clear
    Exit Sub
  End If

  ' Note that colItems.Count cannot be referenced if lFlags is set to 
  ' wbemFlagReturnImmediately + wbemFlagForwardOnly 
  If colItems.Count <> 1 Then
    WScript.Echo "    ***ERROR*** No BIOS administration objects found!"
    Exit Sub
  End If

  For Each objItem In colItems
    bPasswordSet = objItem.IsSet
  Next
  
  ' If there is a password set, try to invoke the password control
  ' to collect the password string. If this fails, the HP CMI Password
  ' control has not been installed.  Run "hppwdctl /install"
  If (bPasswordSet > 0) Then
    WScript.Echo "    setup password is currently active --> cannot change BIOS settings! (" & Timer & ")"
    Exit Sub
  End If


  ' Create the query string to access the HP CMI method interface.
  strMethodQuery = "SELECT * FROM HP_BIOSSettingInterface"

  ' Obtain an instance of our method interface.
  WScript.Echo "Trying to get HP_BIOSSetting interface... (" & Timer & ")"

  Set colItems = objWMIService.ExecQuery(strMethodQuery,,lFlags)

  If Err.Number <> 0 then
    WScript.Echo "    ***ERROR*** Failed to get HP_BIOSSetting interface!"
    Err.Clear
    Exit Sub
  End If

  ' Note that colItems.Count cannot be referenced if lFlags is set to 
  ' wbemFlagReturnImmediately + wbemFlagForwardOnly 
  If colItems.Count <> 1 Then
    WScript.Echo "    ***ERROR*** HP CMI method interface not found!"
    Exit Sub
  End If

  For Each objItem In colItems
    WScript.Echo "Trying to change BIOS setting... (" & Timer & ")"

    objItem.SetBiosSetting retValue, _
                           strSettingName, _ 
                           strSettingValue, _ 
                           strPassword

    If Err.Number <> 0 then
      WScript.Echo "    ***ERROR*** Failed to change BIOS setting!"
      Err.Clear
      Exit Sub
    End If

    returnedStatus = retValue
  Next
  
  Select Case retValue
    Case 0 strReturn = "Success"
    Case 1 strReturn = "Not Supported"
    Case 2 strReturn = "Unspecified Error"
    Case 3 strReturn = "Timeout"
    Case 4 strReturn = "Failed"
    Case 5 strReturn = "Invalid Parameter"
    Case 6 strReturn = "Access Denied"
    Case Else strReturn = "..."
  End Select

  WScript.Echo "    SetBiosSetting() returned (" & retValue _
      & ") " & strReturn & " (" & Timer & ")"

End Sub
  

' ----------------------------------------------------
' This subroutine will display detailed information
' in the event WMI generates an error.
Sub CheckWMI()

  Dim errWMI
  Dim n

  If Err.Number <> 0 then

    errWMI = CreateObject("WbemScripting.SWbemLastError")
    
    If errWMI <> Nothing then

    n=MsgBox("An error has occurred while attempting to access WMI." & _  
        vbCr & vbCr & "Provider Name: " & vbTab & errWMI.ProviderName & _
        vbCr & "Error:         " & vbTab & "0x" & Hex(Err.Number) & _
        vbCr & "Description:   " & vbTab & errWMI.Description & _
        vbCr & "Operation:     " & vbTab & errWMI.Operation & _
        vbCr & "Parameters:    " & vbTab & errWMI.ParameterInfo, _
        vbExclamation, "HP Client Management Interface Example")
    Else
    
    n=MsgBox("An error has occurred while attempting to access WMI." & _  
        vbCr & "Error:         " & vbTab & "0x" & Hex(Err.Number) & _
        vbCr & "Description:   " & vbTab & err.Description & _
        vbExclamation, "HP Client Management Interface Example")
  
    End if

    Err.Clear
    WScript.Quit
  End if

End sub

</script>
</job>
</package>
