Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

1 Answer 1

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)
   Next
End If

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

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


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

   Next
End If


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



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


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

  Else
     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
next
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

 
discard

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.