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

What is the cause of the error in this Python WMI script? (Windows 8.1) (Network adapter configuration) (Python 2.7)

我正在尝试制作一个 Python 脚本,将我的 IP 地址设置为静态地址而不是动态地址。我为此搜索了一些方法,Python 的 WMI 实现似乎是最佳选择。我得到的关于Whosebug的问题是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)

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

SetDNSServerSearchOrder 正在寻找字符串数组

c = nic.SetDNSServerSearchOrder(dns)

应该是

c = nic.SetDNSServerSearchOrder([dns])