此Python WMI脚本中的错误原因是什么? (Windows 8.1)(网络适配器配置)(Python 2.7)

时间:2016-09-28 19:33:54

标签: python windows wmi

我正在尝试制作一个Python脚本,将我的IP地址设置为静态IP地址而不是动态地址。我已经搜索了这方法,而Python的WMI实现似乎是最好的选择。我获得的有关stackoverflow问题的信息是here

我可以将IP地址设置为静态地址,但我也必须设置DNS服务器。 This site here是我获得DNS设置基础的地方,但它导致了问题。

来自IDLE的回溯

Traceback (most recent call last):
File "C:\Users\james_000\Desktop\SetIP.py", line 18, in <module>
    c = nic.SetDNSServerSearchOrder(dns)
File "C:\Python27\lib\site-packages\wmi.py", line 431, in __call__
    handle_com_error ()
File "C:\Python27\lib\site-packages\wmi.py", line 241, in handle_com_error
    raise klass (com_error=err)
x_wmi: <x_wmi: Unexpected COM Error (-2147352567, 'Exception occurred.', (0,
u'SWbemProperty', u'Type mismatch ', None, 0, -2147217403), None)>

SetIP.py

import wmi

nic_configs = wmi.WMI('').Win32_NetworkAdapterConfiguration(IPEnabled=True)

# First network adaptor
nic = nic_configs[0]

# IP address, subnetmask and gateway values should be unicode objects
ip = u'192.168.0.151'
subnetmask = u'255.255.255.0'
gateway = u'192.168.0.1'
dns = u'192.168.0.1'

# Set IP address, subnetmask and default gateway
# Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
a = nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
b = nic.SetGateways(DefaultIPGateway=[gateway])
c = nic.SetDNSServerSearchOrder(dns)
d = nic.SetDynamicDNSRegistration(true)

print(a)
print(b)
print(c)
print(d)

请不要在评论中添加解决方案,因为这会让其他人更难了解如何解决问题。

1 个答案:

答案 0 :(得分:1)

SetDNSServerSearchOrder正在寻找一个字符串数组

c = nic.SetDNSServerSearchOrder(dns)

应该是

c = nic.SetDNSServerSearchOrder([dns])