James Yeoman James Yeoman - 9 months ago 91
Python Question

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

I am trying to make a Python script that will set my IP address to a static one instead of a dynamic one. I have searched up methods for this and the WMI implementation for Python seemed to be the best option. The stackoverflow question I got the information about is here.

I can get the IP address to be set to the static address but then I have to set the DNS server as well. This site here is where I got the basis for the DNS setting but it is causing problems.

Traceback from 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)>


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''
subnetmask = u''
gateway = u''
dns = u''

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


Please don't add solutions in comments as it makes it harder for other people to learn about how to fix the problem.


SetDNSServerSearchOrder is looking for an array of Strings

c = nic.SetDNSServerSearchOrder(dns)

should be

c = nic.SetDNSServerSearchOrder([dns])