Jump to content

WMI apply change


Go to solution Solved by Nine,

Recommended Posts

Hello,

I want to disable energy saver for smart card devices.

So far I'm testing with this code, first I collect all devices name then I want to disable energy save for each one.

#include <Array.au3>
; Generated by AutoIt Scriptomatic

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $colItems = ""
Global $strComputer = "localhost"

Global $aDevices[1]

Global $strPowerManagementCapabilities

Global $Output=""
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{50DD5230-BA8A-11D1-BF5D-0000F805F530}'", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

Global $i = 0
$aDevices[0] = $i

If IsObj($colItems) then
   For $objItem In $colItems
       $i += 1
       $aDevices[0] = $i
       _ArrayAdd($aDevices,$objItem.DeviceID)
   Next
Endif

;_ArrayDisplay($aDevices)
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\WMI")
$colItems = $objWMIService.ExecQuery("SELECT * FROM MSPower_DeviceEnable WHERE InstanceName='USB\\VID_08E6&PID_3437\\CB066182_0'", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
       If $objItem.Enable == True Then
          $objItem.Enable = False ; <= won't apply
      Else
            $Output = "enable: " & $objItem.Enable
            if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      EndIf
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_USBHub" )
Endif

Enable is read/write

However this Powershell code work

Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | Where {$_.InstanceName -LIKE 'USB\VID_08E6&PID_3437\CB066182_0'}
$PowerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | Where {$_.InstanceName -LIKE 'USB\VID_08E6&PID_3437\CB066182_0'}
$PowerMgmt.Enable = $False
$PowerMgmt.psbase.Put()

Can you help me to save change in WMI ?

Link to comment
Share on other sites

Have you tried #RequireAdmin ?

BTW you should avoid using == operator to other variables other than Strings.  == operator is aimed at comparing Case-Sentive Strings (see help file).  When using boolean, you should simply state :

If $objItem.Enable Then
    ...
EndIf

 

Link to comment
Share on other sites

if you can see how it's implemented in powershell you might be able to convert it. nobody has access to powershell's souce code.

use powershell from your autoit script and don't try to reinvent the wheel.

Edited by Earthshine

My resources are limited. You must ask the right questions

 

Link to comment
Share on other sites

  • Solution

I've tried like this without success :

#RequireAdmin
#include <Array.au3>
; Generated by AutoIt Scriptomatic

Global $wbemFlagReturnImmediately = 0x10
Global $wbemFlagForwardOnly = 0x20
Global $wbemChangeFlagUpdateCompatible  = 0x0
Global $colItems = ""
Global $strComputer = "localhost"

Global $aDevices[1]

Global $strPowerManagementCapabilities

Global $Output=""
Global $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{50DD5230-BA8A-11D1-BF5D-0000F805F530}'", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

Global $i = 0
$aDevices[0] = $i

If IsObj($colItems) then
   For $objItem In $colItems
       $i += 1
       $aDevices[0] = $i
       _ArrayAdd($aDevices,$objItem.DeviceID)
   Next
Endif

;_ArrayDisplay($aDevices)
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\WMI")
$colItems = $objWMIService.ExecQuery("SELECT * FROM MSPower_DeviceEnable WHERE InstanceName='USB\\VID_08E6&PID_3437\\CB066182_0'", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
       If $objItem.Enable Then
          $objItem.Enable = False ; <= won't apply
          $objItem.Put($wbemChangeFlagUpdateCompatible)
      Else
            $Output = "enable: " & $objItem.Enable
            if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      EndIf
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_USBHub" )
Endif

 

Link to comment
Share on other sites

Thank you for your help it work 😀

Here the final script :

#RequireAdmin
#include <Array.au3>
#include <File.au3>

Global $LogFile = @ScriptDir & "\" & StringTrimRight(@ScriptName,4) & ".log"
Global $ret = SmartCardEnergySaver(False)
If @error Then _FileWriteLog($LogFile,$ret)

Func SmartCardEnergySaver($bPower = True)

    Local $wbemFlagReturnImmediately = 0x10
    Local $wbemFlagForwardOnly = 0x20
    Local $wbemChangeFlagUpdateCompatible  = 0x0
    Local $colItems = ""
    Local $aDevices[1]
    ;Local $strPowerManagementCapabilities
    Local $count = 0
    Local $errCount = 0
    Local $DevicePath = ""

    Local $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\ROOT\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE ClassGuid = '{50DD5230-BA8A-11D1-BF5D-0000F805F530}'", "WQL", _
                                              $wbemFlagReturnImmediately + $wbemFlagForwardOnly)


    $aDevices[0] = $count
    If IsObj($colItems) then
       For $objItem In $colItems
           $count += 1
           $aDevices[0] = $count
           _ArrayAdd($aDevices,$objItem.DeviceID)
       Next
    Else
        Return SetError(1,0,"No WMI Objects Found for class: " & "Win32_PnPEntity" )
    Endif

    If $count = 0 Then Return SetError(2,0,"No result found")
     ;_ArrayDisplay($aDevices)

    $objWMIService = ObjGet("winmgmts:\\" & @ComputerName & "\ROOT\WMI")

    For $i = 1 To $aDevices[0]
        $DevicePath = StringReplace($aDevices[$i],"\","\\") & "_0"
        $colItems = $objWMIService.ExecQuery("SELECT * FROM MSPower_DeviceEnable WHERE InstanceName='"&$DevicePath&"'", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

        If IsObj($colItems) then
            For $objItem In $colItems
                If $objItem.Enable And Not $bPower Then
                    $objItem.Enable = False
                    $objItem.Put_($wbemChangeFlagUpdateCompatible)
                ElseIf Not $objItem.Enable And $bPower Then
                    $objItem.Enable = True
                    $objItem.Put_($wbemChangeFlagUpdateCompatible)
              Else
                    ExitLoop
              EndIf
           Next
        Else
           $errCount += 1
        Endif
    Next

    Return(1)

EndFunc

 

Link to comment
Share on other sites

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...