Assign a static IP address to multiple computers with 1 script

All Pc's are Windows XP SP2
Can I assign static Ip address to 20 computers with 1 script? The script that I found to change 1 computer Ip address is below:
-------------------------------------------------------------------------------------------------------
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
--------------------------------------------------------------------------------------------------------------
To use this for 20 computers, can I just use the same text over again after the last statement in this script with different computer name and so on and so on?
teamsjAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RobSampsonConnect With a Mentor Commented:
Hi, while your latest script *would* work, it's not the easiest way...what I would do is create an array to loop through, like:

arrComputers = Array( _
   "PC1;192.168.1.141", _
   "PC2;192.168.1.142" _
   )

and then iterate through that:

For Each strEntry in arrComputers
   strComputer = Split(strEntry, ";")(0)
   strIPAddress = Split(strEntry, ";")(1)
  ' now assign the ipaddress, where you'll also have
   strIPAddress = Array(strIPAddress)
   ' etc, etc
Next


Regards,

Rob.
0
 
bradleys40Commented:
you need a different ip address aswell
0
 
teamsjAuthor Commented:
For example would this work for 2 computers:
---------------------------------------------------------------------------------------
strComputer = "C01"
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
strComputer = "C02"
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.142")
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
-----------------------------------------------------------------------------------------------------------
0
 
teamsjAuthor Commented:
When I run this script on the computer it turns on the firewall after it changes the IP.  I don't see what in the script is turning on the firewall.  Is there something i can take out of this script to turn off the firewall, or do I need to append another script to turn off firewall?
--------------------------------------------------------------------------------------------------------------
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
0
 
RobSampsonCommented:
Hmmm, I have no idea why that's turning on the firewall.....perhaps Windows detects that as a new NIC configuration and just automatically firewalls it.  In any case, after this line:
        WScript.Echo "The IP address has been changed."

add these three lines:
        Set objFirewall = CreateObject("HNetCfg.FwMgr")
        Set objPolicy = objFirewall.LocalPolicy.CurrentProfile
        objPolicy.FirewallEnabled = False


Regards,

Rob.
0
All Courses

From novice to tech pro — start learning today.