Stormgrade Posted August 26, 2021 Share Posted August 26, 2021 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. expandcollapse popup#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 More sharing options...
Nine Posted August 26, 2021 Share Posted August 26, 2021 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 “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Stormgrade Posted August 26, 2021 Author Share Posted August 26, 2021 Thank you for the tips on operator ==. Yes I've tried with #RequireAdmin, I've also compiled and run with elevated privilege without sucess. How to convert ? $PowerMgmt.psbase.Put() Link to comment Share on other sites More sharing options...
Stormgrade Posted August 31, 2021 Author Share Posted August 31, 2021 Hello, Someone could help me please ? Link to comment Share on other sites More sharing options...
Earthshine Posted August 31, 2021 Share Posted August 31, 2021 (edited) 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 August 31, 2021 by Earthshine My resources are limited. You must ask the right questions Link to comment Share on other sites More sharing options...
Stormgrade Posted August 31, 2021 Author Share Posted August 31, 2021 With AutoIt you can query WMI datas but it's not possible to update values ? Are you sure about that ? Perhaps there is another method without convert this powershell code ? $PowerMgmt.psbase.Put() Link to comment Share on other sites More sharing options...
Solution Nine Posted August 31, 2021 Solution Share Posted August 31, 2021 Ok found it : https://docs.microsoft.com/en-us/windows/win32/wmisdk/swbemobject-put- Set only the first parameter to the update flag. Use #RequireAdmin I got it working under Win7. Enjoy ! “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Stormgrade Posted August 31, 2021 Author Share Posted August 31, 2021 I've tried like this without success : expandcollapse popup#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 More sharing options...
Nine Posted August 31, 2021 Share Posted August 31, 2021 There is an underscore after Put_ method (see now ?) “They did not know it was impossible, so they did it” ― Mark Twain Spoiler Block all input without UAC Save/Retrieve Images to/from Text Monitor Management (VCP commands) Tool to search in text (au3) files Date Range Picker Virtual Desktop Manager Sudoku Game 2020 Overlapped Named Pipe IPC HotString 2.0 - Hot keys with string x64 Bitwise Operations Multi-keyboards HotKeySet Recursive Array Display Fast and simple WCD IPC Multiple Folders Selector Printer Manager GIF Animation (cached) Screen Scraping Multi-Threading Made Easy Link to comment Share on other sites More sharing options...
Stormgrade Posted September 1, 2021 Author Share Posted September 1, 2021 Thank you for your help it work 😀 Here the final script : expandcollapse popup#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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now