0

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?

Michelle
  • 913
  • 5
  • 20
  • 30
  • 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 Answers1

0
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
user9517
  • 114,104
  • 20
  • 206
  • 289
  • 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