Python 从WMI com对象调用Win32_NetworkAdapterConfiguration例程

Python 从WMI com对象调用Win32_NetworkAdapterConfiguration例程,python,Python,提前谢谢你的帮助。我正在尝试使用Win32 COM模块设置网络接口的ip地址,但无法执行此操作。我尝试了很多次搜索,但都没有找到问题的答案。以下是我正在运行的代码: import win32com.client obj = win32com.client.Dispatch("WbemScripting.SWbemLocator") wmobj = obj.ConnectServer("localhost","root\cimv2"

提前谢谢你的帮助。我正在尝试使用Win32 COM模块设置网络接口的ip地址,但无法执行此操作。我尝试了很多次搜索,但都没有找到问题的答案。以下是我正在运行的代码:

import win32com.client 
obj  = win32com.client.Dispatch("WbemScripting.SWbemLocator") 
wmobj = obj.ConnectServer("localhost","root\cimv2")
nobj = wmobj.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
for n in nobj:
    print n.Caption
    n.SetMTU('9000')
 
当我运行此代码时,会出现以下错误:

Traceback (most recent call last):
  File "<pyshell#18>", line 3, in <module>
    n.SetMTU('9000')
  File "C:\Python27\lib\site-packages\win32com\client\dynamic.py", line 505, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
com_error: (-2147352567, 'Exception occurred.', (0, u'SWbemObjectEx', u'Invalid method ', None, 0, -2147217362), None)
回溯(最近一次呼叫最后一次):
文件“”,第3行,在
n、 SetMTU('9000')
文件“C:\Python27\lib\site packages\win32com\client\dynamic.py”,第505行,在\uu getattr中__
ret=self.\u oleobj\u.Invoke(retEntry.dispid,0,Invoke\u类型,1)
com_错误:(-2147352567,‘发生异常’,(0,u'SWbemObjectEx',u'Invalid method',None,0,-2147217362),None)

我做了更多的调试,发现我可以访问
Win32Networking
类的任何变量,但每当我尝试调用该类的任何方法时,它都会返回相同的错误。

我没有太多使用
win32com
的经验,但可能没有实现
SetMTU
方法。根据的MSDN文档,该方法“不受支持”。我在XP中失败了

请注意,对于Win32 COM,仅访问属性即可调用它:

>>> import win32com.client
>>> wmobj = obj.ConnectServer("localhost","root\cimv2")
>>> nobj = wmobj.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
>>> n = nobj[10]  #my wireless interface
>>> n.ReleaseDHCPLease  #invoked
0
>>> n.RenewDHCPLease
0
尝试正常调用它将导致尝试调用整数返回值,这将引发
TypeError
。但是,您可以首先包装这样一个方法,使其成为一个普通的Python调用:

>>> n._FlagAsMethod('ReleaseDHCPLease')
>>> n._FlagAsMethod('RenewDHCPLease')
>>> n.ReleaseDHCPLease()
0
>>> n.RenewDHCPLease()
0

编辑:

在上面链接的页面的用户贡献区域中,搜索必须从类访问的静态方法列表,包括
SetMTU
。以下是如何获得课程:

>>> NetAdapterConfig = wmobj.Get("Win32_NetworkAdapterConfiguration")
>>> NetAdapterConfig._FlagAsMethod('SetMTU')
有关返回值的含义,请参见。虽然我并不真正理解这个方法在静态上下文中的作用


下面是一个使用标准库的
winreg
更新注册表的示例:

import winreg

nid = n.SettingID
MTU = 1500

path = r'SYSTEM\CurrentControlSet\Services\TCPIP\Parameters\Interfaces\\'+ nid 
sam = winreg.KEY_ALL_ACCESS
adapter = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, path, 0, sam)
winreg.SetValueEx(adapter, 'MTU', None, winreg.REG_DWORD, MTU)

嗯,我刚刚尝试了
FlagAsMethod

正如所建议的,对于
Win32\u进程
类,例如:

proc10 = GetObject(pos).ExecQuery("Select * from Win32_Process")[10]
proc10._FlagAsMethod('GetOwner')
proc10.GetOwner()
# >> 0
然而,我期望
domain\user
等。此外,我还能够编写

proc10.GetOwner(10,20,30)
但效果是一样的


我发布了一个在我的案例中起作用的方法。我只是想知道
SetMTU
是否使用
\u FlagAsMethod
技巧收到了正确的值。

我遇到了类似的问题,并找到了调用Win32\u NetworkAdapterConfiguration对象的这些方法的方法。在这里,我没有直接使用这些方法,而是将所有输入参数包装到一个实例中。这似乎是有线的,但工作

import win32com.client
objLocator = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objService = objLocator.ConnectServer(".","root\cimv2")
nobj = objService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
obj = nobj[0]

## here is the strange part. 
obj_method = obj.Methods_("SetWINSServer")
obj_in_param = obj_method.inParameters.SpawnInstance_()
obj_in_param.WINSPrimaryServer = "127.0.0.1"
obj_in_param.WINSSecondaryServer = "127.0.0.2"
#invoke the SetWINServer method, and it worked.
obj.ExecMethod_("SetWINSServer", obj_in_param)
几乎所有这些Win32_NetworkAdapter配置方法都可以这样使用。 然而,“SetMTU”不能,可能是因为“这个方法不受支持”,我在Windows2008R2中尝试了它,但得到了相同的错误。 它说这里不支持SetMTU。

我也尝试了Tim golden WMI模块,但最终还是出现了相同的错误。可能缺少一些基本的共同点。以下是我尝试使用WMI模块的代码。导入wmi netobj=wmi.wmi().Win32_NetworkAdapterConfiguration(IPEnabled=True)以在netobj中为a:print a.caption print a.MACAddress print a.mtu print a.ipaddress a.setipaddress(“”)仍然失败,错误如下:>>>>n._FlagAsMethod('EnableStatic')>>n.EnableStatic('10.193.184.215'、'255.255.255.0')回溯(最后一次调用):文件“”,第1行,在n.EnableStatic('10.193.184.215'、'255.255.255.0')文件“”中,第2行,在EnableStatic com中,错误:(-2147352567,'发生异常',(0,u'SWbemObjectEx',u'Type失配',无,0,-2147217403),无)@Rohit:
EnableStatic
需要数组。您可以使用元组或列表。请尝试
n.EnableStatic(['10.193.184.215'],['255.255.255.0'])
。SetMTU仍然失败,方法错误无效。我正在Windows 2008R2上运行,它是受支持的。是否可以从com对象中列出所有方法错误:>>>n._FlagAsMethod('SetMTU'))>>>n.SetMTU(2000)回溯(最近一次调用):文件“”,第1行,在n.SetMTU(2000)文件“”中,第2行,在SetMTU com_错误:(-2147352567,“发生异常”。(0,u'SWbemObjectEx',u'Invalid method',无,0,-2147217362),无)这是可行的,但问题是它为系统中的所有现有网卡设置。我需要搜索一个特定的适配器,然后更改参数。这变得太繁忙了,请您建议一些简单的方法,使用Python在Windows机器上配置网络适配器。