【问题标题】:WMI USB Enable and DisableWMI USB 启用和禁用
【发布时间】:2012-01-12 11:47:17
【问题描述】:

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

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);

【问题讨论】:

    标签: wmi


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      是的,它可以做到。这是代码,引用了这个 Microsoft 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多