WMI USB 启用和禁用

发布于 2024-12-26 03:49:06 字数 740 浏览 0 评论 0原文

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

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

Hi I am using WMI to change the remote registry value for USBSTOR. I want to change the value of start attribute to 4 or 3 for enabling and disabling.
But the datatype for Start attribute in registry is DWORD, if i can the datatype to size it does not work .
I need to keep the Datatype to DWORD. Can someone please tell me how to setDWORDValue using WMI, following is the piece of code that i tried, it worked succesfully but still the value of start field is unchanged in registry.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ら栖息 2025-01-02 03:49:06

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

Simple solution using 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))
一个人的旅程 2025-01-02 03:49:06

是的,这是可以做到的。这是代码,引用此 Microsoft 链接这个。将 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

Yes it can be done. Here's the code, referencing this Microsoft link and this one. Replace 3389 with the new value you want to use, and change the key as needed:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文