Assign a Static IP Address

Sets the IP address of a computer to 192.168.1.141, and sets the IP gateway to 192.168.1.100.

 
 
 
 
 
5 Star
(1)
Add to favorites
Networking
8/10/2009
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Tests with recent Windows versions
    5 Posts | Last post December 18, 2012
    • Could somebody test this with recent Windows versions?
      It seems it 's not been validated for versions higher than XP...
    • With very little validation, it looks like on 2008 server (so I assume Windows 7 and probaby Vista too) I got this script to work by getting rid Array(xxx)and just setting the variables as text strings.  When I had it set to an array, it always returned code 90 "Parameter out of bounds"  
      
      It would be nice to get that verified, however.
    • OTOH, that doesn't work either.  If you take the Array out you get a type mismatch.  So, at this point there is no way to set the IP address on 2008, Vista or Windows 7.  Hey, Scripting Guys, do you have anything on this?
    • Is there a version of this script which works on Windows XP, Vista and 7? how to assigne DNS address?
    • 'run in elevated mode
      If WScript.Arguments.length =0 Then
        Set objShell = CreateObject("Shell.Application")
        'Pass a bogus argument with leading blank space, say [ uac]
        objShell.ShellExecute "wscript.exe", Chr(34) & _
        WScript.ScriptFullName & Chr(34) & " uac", "", "runas", 1
      Else
        'change IP address
      strComputer = "." 
      Set objWMIService = GetObject("winmgmts:" _ 
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
       
      Set colNetAdapters = objWMIService.ExecQuery _ 
          ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
      strIPAddress = Array("192.168.1.141") 
      strSubnetMask = Array("255.255.255.0") 
      strGateway = Array("192.168.1.100") 
      strGatewayMetric = Array(1) 
        
      For Each objNetAdapter in colNetAdapters 
          errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) 
          errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 
          If errEnable = 0 Then 
              WScript.Echo "The IP address has been changed." 
          Else 
              WScript.Echo "The IP address could not be changed." 
          End If 
      Next 
      End If
      
      
      adapted and Frankensteined from a couple scripts on here.   Works on Win7 X64
  • Is there a way to change what adapters this applies to?
    1 Posts | Last post December 18, 2012
    • Set colNetAdapters = objWMIService.ExecQuery _ 
          ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE") 
      .........
      For Each objNetAdapter in colNetAdapters 
          errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask) 
          errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric) 
      
      When the script is run it applies the all the IP information to the LAN, and it applies the default gateway to the Wireless.   I would like to set it to statically apply to the LAN