2012-01-12 70 views
0

嗨我正在使用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); 

回答

0

使用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)) 
0

是的,它可以做到。這裏是代碼,引用這個微軟的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