WMI USB启用和禁用

问题描述:

嗨我正在使用WMI来更改USBSTOR的远程注册表值。我想将启动属性的值更改为4或3以启用和禁用。 但在注册表中的Start属性的数据类型是DWORD,如果我可以通过数据类型来设置它的大小不起作用。 我需要保持数据类型为DWORD。有人可以告诉我如何使用WMI设置DWORD值,以下是我试过的一段代码,它工作成功,但仍然在注册表中保持启动字段的值不变。WMI USB启用和禁用

const uint HKEY_LOCAL_MACHINE = 0x80000002; 

ManagementBaseObject methodParams = registryTask.GetMethodParameters(typeOfValue); 

methodParams["hDefKey"] = HKEY_LOCAL_MACHINE;// BaseKey; 
methodParams["sSubKeyName"] = @"SYSTEM\\CurrentControlSet\\Servic\\USBSTOR"; 
methodParams["sValueName"] = "Start"; 

try 
{ 
    methodParams["sValue"] = "3"; 
} 
catch 
{ 
    methodParams["uValue"] = (UInt32)Convert.ToInt32("3"); 
} 

ManagementBaseObject exitValue = registryTask.InvokeMethod(typeOfValue, methodParams, null); 

使用python的简单解决方案。

import wmi 
import win32api,_winreg 

c = wmi.WMI() 

# To get the binary value of particular subkey 
# Please note that 0x80000002 represents HKEY_LOCAL_MACHINE 
ReturnValue, uValue = c.StdRegProv.GetBinaryValue(0x80000002,"AFD","SYSTEM\CurrentControlSet\Services") 

# To get the list of all the subkeys available in particular key 
ret, subKeys = c.StdRegProv.EnumKey (0x80000002, "SYSTEM\CurrentControlSet\Services") 
print ret 
for key in subKeys: 
    print key 

ReturnValue=c.StdRegProv.SetDWORDValue(0x80000002,"Type","SYSTEM\CurrentControlSet\Services\USBSTOR",0x4) 

#HKEY_CLASSES_ROOT (2147483648 (0x80000000)) 
#HKEY_CURRENT_USER (2147483649 (0x80000001)) 
#HKEY_LOCAL_MACHINE (2147483650 (0x80000002)) 
#HKEY_USERS (2147483651 (0x80000003)) 
#HKEY_CURRENT_CONFIG (2147483653 (0x80000005)) 
#HKEY_DYN_DATA (2147483654 (0x80000006)) 

是的,它可以做到。这里是代码,引用这个微软的linkthis之一。将3389替换为您要使用的新值,并根据需要更改密钥:

const HKEY_LOCAL_MACHINE = &H80000002 
strComputer = "." 
'Set StdOut = WScript.StdOut 
Set oReg=GetObject(_ 
    "winmgmts:{impersonationLevel=impersonate}!\\" &_ 
    strComputer & "\root\default:StdRegProv") 
strKeyPath = "SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" 
strValueName = "PortNumber" 

' Display old value 
oReg.GetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue 
WScript.Echo "Old RDP value=" & dwValue 

' Set new value 
dwValue= 3389 
oReg.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue 
If Err = 0 Then 
    oReg.GetDWORDValue _ 
     HKEY_LOCAL_MACHINE,strKeyPath,strValueName,dwValue 
    WScript.Echo "New RDP Value =" & dwValue 
Else 
    WScript.Echo "Error in creating key" & _ 
     " and DWORD value = " & Err.Number 
End If