Jump to content

Convert VBScript WMI query to AutoIT


jayinoz
 Share

Recommended Posts

Hi folks,

I've converted quite a few PowerShell scripts to AutoIT, but I'm struggling with this one, and I've got a VBScript alternative that seems equally a challenge.

Here is the PowerShell command and output

 

(gwmi –class Lenovo_GetBiosSelections –namespace root\wmi).GetBiosSelections("WakeOnLAN") | Format-List Selections

Output:

Selections : Disable,ACOnly,ACandBattery,Enable

Here is a very similar VBScript 

On Error Resume Next
Dim colItems

strComputer = "LOCALHOST"     ' Change as needed.
strOptions
Set objWMIService = GetObject("WinMgmts:" _
    &"{ImpersonationLevel=Impersonate}!\\" & strComputer & "\root\wmi")
Set colItems = objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")

For Each objItem in colItems
    If Len(objItem.CurrentSetting) > 0 Then
        Setting = ObjItem.CurrentSetting
        StrItem = Left(ObjItem.CurrentSetting, InStr(ObjItem.CurrentSetting, ",") - 1)
        StrValue = Mid(ObjItem.CurrentSetting, InStr(ObjItem.CurrentSetting, ",") + 1, 256)
        If StrItem = "WakeOnLAN" Then
        	Set selItems = objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
        	For Each objItem2 in selItems
            	objItem2.GetBiosSelections StrItem + ";", strOptions
        	Next
        
            WScript.Echo StrItem
        	WScript.Echo "  current setting  = " + StrValue
        	WScript.Echo "  possible settings = " + strOptions
        	WScript.Echo
    	End If
    End If
Next

Output: 

WakeOnLAN
  current setting  = ACOnly
  possible settings = Disable,ACOnly,ACandBattery,Enable

Could somebody help me with the AutoIT equivalent please?

Thanks

Edited by jayinoz
Link to comment
Share on other sites

$strComputer = "LOCALHOST"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")


If IsObj($colItems) then
    For $objItem In $colItems
        $StrOptions = ''
        If StringLen($objItem.CurrentSetting) > 0 Then
            $Setting = $objItem.CurrentSetting
            $StrItem = StringLeft($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") - 1)
            $StrValue = StringMid($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") + 1, 256)
            If $StrItem = "WakeOnLAN" Then
                $selItems =$objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
                For $objItem2 in $selItems
                        ;Not sure about this line, I suppose it's a string concatenation
;~                      $objItem2.GetBiosSelections StrItem + ";", strOptions
                        $StrOptions &= $objItem2.GetBiosSelections & ','
                Next
                ConsoleWrite($StrItem & @CRLF)
                ConsoleWrite('Current setting: ' & $StrValue & @CRLF)
                ConsoleWrite('Possible settings: ' & $StrOptions & @CRLF)
            EndIf
        EndIf
    Next
Endif

 

When the words fail... music speaks.

Link to comment
Share on other sites

Are you running the AutoIt script as an EXE?  When using ConsoleWrite and running as an EXE you need to add the directive #AutoIt3Wrapper_Change2CUI=Y...if you are expecting the output to be shown in the command prompt console.

Link to comment
Share on other sites

I'm using ConsoleWrite at the moment, the code has not been compiled.

I'm using other similar queries with no issue, e.g. 

Func GetAllBIOSSettingsAndValues()
    Local $objWMI = ObjGet('winmgmts:root\wmi')
    Local $objClass = $objWMI.ExecQuery('SELECT * FROM Lenovo_BiosSetting')

    Local $ResultString = 'All BIOS settings...' & @CRLF
    Dim $ResultsArray[0][3]

    For $objItem In $objClass
        If StringInStr($objItem.CurrentSetting, ",") Then
            ReDim $ResultsArray[UBound($ResultsArray)+1][3]
            Local $anObject = StringSplit($objItem.CurrentSetting, ",")
            If $Debug Then ConsoleWrite ($objItem.CurrentSetting & " split value in to " & $anObject[0] & "." & $anObject[1] & ":" & $anObject[2] & @CRLF)
            If $anObject[0] >= 1 Then $ResultsArray[UBound($ResultsArray) - 1][0] = $anObject[1]
            If $anObject[0] >= 2 Then $ResultsArray[UBound($ResultsArray) - 1][1] = $anObject[2]
            $ResultsArray[UBound($ResultsArray) - 1][2] =  StringReplace($objItem.InstanceName,"\","\\")
        EndIf
    Next


    Return $ResultsArray


EndFunc

The problem is in here somewhere...

                For $objItem2 in $selItems
;VBScript               $objItem2.GetBiosSelections StrItem + ";", strOptions
                        $StrOptions &= $objItem2.GetBiosSelections & ','
                Next

 

 

Link to comment
Share on other sites

2 hours ago, jayinoz said:

The problem is in here somewhere...

                For $objItem2 in $selItems
;VBScript               $objItem2.GetBiosSelections StrItem + ";", strOptions
                        $StrOptions &= $objItem2.GetBiosSelections & ','
                Next

I think GetBiosSelection method has a parameter that it's the setting name so it should be something like this. Let me know if it works.

$strComputer = "LOCALHOST"

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")


If IsObj($colItems) then
    For $objItem In $colItems
        $StrOptions = ''
        If StringLen($objItem.CurrentSetting) > 0 Then
            $Setting = $objItem.CurrentSetting
            $StrItem = StringLeft($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") - 1)
            $StrValue = StringMid($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") + 1, 256)
            If $StrItem = "WakeOnLAN" Then
                $selItems =$objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
                For $objItem2 in $selItems
                        $StrOptions &= $objItem2.GetBiosSelections($StrItem) & ','
                Next
                ConsoleWrite($StrItem & @CRLF)
                ConsoleWrite('Current setting: ' & $StrValue & @CRLF)
                ConsoleWrite('Possible settings: ' & StringTrimRight($StrOptions, 1) & @CRLF)
            EndIf
        EndIf
    Next
Endif

 

When the words fail... music speaks.

Link to comment
Share on other sites

Thanks for the suggestion & I think you're right, but it's not getting that far...

$strComputer = "LOCALHOST"
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
$colItems = $objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")


If IsObj($colItems) then
    ConsoleWrite("Step 1" & @CRLF)
    For $objItem In $colItems
        ConsoleWrite("Step 2" & @CRLF)
        $StrOptions = ''
        ;If StringLen($objItem.CurrentSetting) > 0 Then
            ConsoleWrite("Step 3" & @CRLF)
            $Setting = $objItem.CurrentSetting
            $StrItem = StringLeft($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") - 1)
            $StrValue = StringMid($objItem.CurrentSetting, StringInStr($objItem.CurrentSetting, ",") + 1, 256)
            If $StrItem = "WakeOnLAN" Then
                $selItems =$objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
                For $objItem2 in $selItems
                        $StrOptions &= $objItem2.GetBiosSelections($StrItem) & ','
                Next
                ConsoleWrite($StrItem & @CRLF)
                ConsoleWrite('Current setting: ' & $StrValue & @CRLF)
                ConsoleWrite('Possible settings: ' & StringTrimRight($StrOptions, 1) & @CRLF)
            EndIf
        ;EndIf
    Next
Endif

This results in Step 1 being output, so it's not getting in to the For $objItem In $colItems loop

 

 

Edited by jayinoz
Link to comment
Share on other sites

Run this and show us what do you get in console:

Lenovo_BiosSetting()

Func Lenovo_BiosSetting($strComputer = 'localhost')
    Local $objWMIService, $colItems, $selItems
    Local $Options, $Setting, $SettingName, $SettingValue
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    If IsObj($colItems) then
       For $objItem In $colItems
            ConsoleWrite($objItem.Manufacturer & ' / ' & $objItem.Model & @CRLF)
       Next
    EndIf
    $colItems = $objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")
    If IsObj($colItems) then
        ConsoleWrite("Lenovo_BiosSetting: " & (IsObj($colItems) ? True : False) & @CRLF)
        For $objItem In $colItems
            $SettingName = ''
            $SettingValue = ''
            $Setting = $objItem.CurrentSetting
            ConsoleWrite("Current setting: " & $objItem.CurrentSetting& @CRLF)
            $aSetting = StringSplit($Setting, ',')
            If IsArray($aSetting) Then
                If $aSetting[0] >= 2 Then
                    $SettingName = $aSetting[1]
                    $SettingValue = $aSetting[2]
                EndIf
            EndIf
            If $SettingName = "WakeOnLAN" Then
                $Options = ''
                $selItems =$objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
                ConsoleWrite("Lenovo_GetBiosSelections: " & (IsObj($selItems) ? True : False) & @CRLF)
                For $selItem in $selItems
                        $Options &= $selItem.GetBiosSelections($SettingName) & ','
                Next
                ConsoleWrite($SettingName & @CRLF)
                ConsoleWrite('Current setting value: ' & $SettingValue & @CRLF)
                ConsoleWrite('Possible settings: ' & StringTrimRight($Options, 1) & @CRLF)
            EndIf
        Next
    Endif
EndFunc

 

When the words fail... music speaks.

Link to comment
Share on other sites

This might help.

This code works perfectly

#include <AutoItConstants.au3>
#include <Array.au3>
#include <Math.au3>

$Debug = False
$AllBIOSSettings = GetAllBIOSSettingsAndValues()
ShowBIOSSettingAndValues($AllBIOSSettings, 300)

Func GetAllBIOSSettingsAndValues()
    Local $objWMI = ObjGet('winmgmts:root\wmi')
    Local $objClass = $objWMI.ExecQuery('SELECT * FROM Lenovo_BiosSetting')

    Local $ResultString = 'All BIOS settings...' & @CRLF
    Dim $ResultsArray[0][3]

    For $objItem In $objClass
        If StringInStr($objItem.CurrentSetting, ",") Then
            ReDim $ResultsArray[UBound($ResultsArray)+1][3]
            Local $anObject = StringSplit($objItem.CurrentSetting, ",")
            If $Debug Then ConsoleWrite ($objItem.CurrentSetting & " split value in to " & $anObject[0] & "." & $anObject[1] & ":" & $anObject[2] & @CRLF)
            If $anObject[0] >= 1 Then $ResultsArray[UBound($ResultsArray) - 1][0] = $anObject[1]
            If $anObject[0] >= 2 Then $ResultsArray[UBound($ResultsArray) - 1][1] = $anObject[2]
            $ResultsArray[UBound($ResultsArray) - 1][2] =  StringReplace($objItem.InstanceName,"\","\\")
        EndIf
    Next

    Return $ResultsArray

EndFunc

Func ShowBIOSSettingAndValues ($AllData, $topcount)

    ConsoleWrite(StringFormat("%-25.20s", "Reference")  )
    ConsoleWrite(StringFormat("%-45.40s", "Setting")); // left-justification with spaces    ;ConsoleWrite(StringFormat("%-15.10s",   $TheString[$EachLine][1]) & @CRLF); // left-justification with spaces
    ConsoleWrite("Value" & @CRLF ); // left-justification with spaces
    For $EachLine = 0 to _min((UBound($AllData) - 1), ($topcount - 1))
        ConsoleWrite(StringFormat("%-25.20s", $AllData[$EachLine][2])); // left-justification with spaces
        ConsoleWrite(StringFormat("%-45.40s", $AllData[$EachLine][0])); // left-justification with spaces    ;ConsoleWrite(StringFormat("%-15.10s",   $TheString[$EachLine][1]) & @CRLF); // left-justification with spaces
        ConsoleWrite( $AllData[$EachLine][1] & @CRLF ); // left-justification with spaces
    Next

EndFunc

This outputs this...

Reference                Setting                                      Value
ACPI\\PNP0C14\\1_0       WakeOnLAN                                    ACOnly
ACPI\\PNP0C14\\1_1       WakeOnLANDock                                Disable
ACPI\\PNP0C14\\1_2       EthernetLANOptionROM                         Enable
ACPI\\PNP0C14\\1_3       IPv4NetworkStack                             Disable
ACPI\\PNP0C14\\1_4       IPv6NetworkStack                             Disable

Etc

 

Link to comment
Share on other sites

19 hours ago, Andreik said:

Run this and show us what do you get in console:

Lenovo_BiosSetting()

Func Lenovo_BiosSetting($strComputer = 'localhost')
    Local $objWMIService, $colItems, $selItems
    Local $Options, $Setting, $SettingName, $SettingValue
    $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem")
    If IsObj($colItems) then
       For $objItem In $colItems
            ConsoleWrite($objItem.Manufacturer & ' / ' & $objItem.Model & @CRLF)
       Next
    EndIf
    $colItems = $objWMIService.ExecQuery("Select * from Lenovo_BiosSetting")
    If IsObj($colItems) then
        ConsoleWrite("Lenovo_BiosSetting: " & (IsObj($colItems) ? True : False) & @CRLF)
        For $objItem In $colItems
            $SettingName = ''
            $SettingValue = ''
            $Setting = $objItem.CurrentSetting
            ConsoleWrite("Current setting: " & $objItem.CurrentSetting& @CRLF)
            $aSetting = StringSplit($Setting, ',')
            If IsArray($aSetting) Then
                If $aSetting[0] >= 2 Then
                    $SettingName = $aSetting[1]
                    $SettingValue = $aSetting[2]
                EndIf
            EndIf
            If $SettingName = "WakeOnLAN" Then
                $Options = ''
                $selItems =$objWMIService.ExecQuery("Select * from Lenovo_GetBiosSelections")
                ConsoleWrite("Lenovo_GetBiosSelections: " & (IsObj($selItems) ? True : False) & @CRLF)
                For $selItem in $selItems
                        $Options &= $selItem.GetBiosSelections($SettingName) & ','
                Next
                ConsoleWrite($SettingName & @CRLF)
                ConsoleWrite('Current setting value: ' & $SettingValue & @CRLF)
                ConsoleWrite('Possible settings: ' & StringTrimRight($Options, 1) & @CRLF)
            EndIf
        Next
    Endif
EndFunc

 

I get this...

 

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
LENOVO / 20UCS0P40E
Lenovo_BiosSetting: True
+>07:40:11 AutoIt3.exe ended.rc:0

Thanks

Link to comment
Share on other sites

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...