Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a (WSH) script that clients can execute on their machines themselves and with a minimum of hassle and without me requiring any special privileges. The script should take the existing IP address, subnet mask and default gateway of the ethernet adapter and set these as static parameters. I also need a script to be able to undo this. I have figured out how to set the parameters manually on the command line using the netsh command i.e.

netsh.exe interface ip set address {Interface} static {IP} {SUB} {DEF} 1

However I suspect that this could be done automatically with a WSH script. The clients are either XP/Vista/Windows 7. What would be the best way to accomplish this?

share|improve this question
Does the script need to poll the workstation for the address information (i.e. make a WMI query), then use this to set the static value? Are you trying to move from DHCP to static? How are you handling the DHCP pool once the address is migrated? – newmanth Feb 24 '11 at 16:11
The script needs to be executed by our home customers to whose machines we have no access to. We intend to distribute it via a downloads page on our website. Our application requires that the customer switches to static networking before running the app and then switching back afterward. – Michelle Feb 24 '11 at 16:25
up vote 0 down vote accepted
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\"& strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

n = 1

For Each objAdapter in colAdapters 
If (objAdapter.DHCPEnabled) Then
    If Not IsNull(objAdapter.IPAddress) Then
   For i = 0 To UBound(objAdapter.IPAddress)
    IPAddress = objAdapter.IPAddress(i)
End If

If Not IsNull(objAdapter.IPSubnet) Then
   For i = 0 To UBound(objAdapter.IPSubnet)
    SubNet = objAdapter.IPSubnet(i)
End If

If Not IsNull(objAdapter.DefaultIPGateway) Then
   For i = 0 To UBound(objAdapter.DefaultIPGateway)
      GateWay = objAdapter.DefaultIPGateway(i)
End If

If Not IsNull(objAdapter.DNSServerSearchOrder) Then
   For i = 0 To UBound(objAdapter.DNSServerSearchOrder)

End If

If Not IsNull(objAdapter.DNSDomainSuffixSearchOrder) Then
   For i = 0 To UBound(objAdapter.DNSDomainSuffixSearchOrder)
      WScript.Echo "    DNS suffix search list: " & _
End If

objAdapter.DHCPEnabled = false
  If Not IsNull(objAdapter.DHCPLeaseObtained) Then
     utcLeaseObtained = objAdapter.DHCPLeaseObtained
     strLeaseObtained = ""
  End If

  If Not IsNull(objAdapter.DHCPLeaseExpires) Then
     utcLeaseExpires = objAdapter.DHCPLeaseExpires

     strLeaseExpires = ""
  End If
  WScript.Echo "    DHCP lease expires:  " & strLeaseExpires

n = n + 1

'netsh interface ip set address "Local Area Connection" static objAdapter.IPAddress(i) objAdapter.IPSubnet(i) objAdapter.DefaultIPGateway(i) 1

Dim WshShell, oExec, A
Set WshShell = CreateObject("WScript.Shell")

WScript.Echo "  IP address:             " & IPAddress
WScript.Echo "  Subnet:                 " & Subnet
WScript.Echo "  Default gateway:        " & Gateway

A = "netsh interface ip set address " & Chr(34) & "Local Area Connection" & Chr(34) & " static " & IPAddress & " " & Subnet & " " & Gateway & " 1"
wscript.echo A
Set oExec = WshShell.Exec(A)

  WScript.Echo "before runnig this script on the network take out all wscripts enjoy LCpl Worsnop"
End If
share|improve this answer
This script doesn't work. It only detects an ipv6 address since it just records the last ip in the objAdapter.IPAddress array. Also why does it need to know the DNS search order, DNS suffix search order, DHCP lease times? – Michelle Mar 15 '11 at 11:20

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.