Script Help

I have zero scripting abilities other than I can look at one and understand what it's doing.  What I need is a script that will read all of the NIC's in a machine, assign an IP/Subnet Mask/Gateway to the first NIC, and then attempt to ping the gateway.  If it is unable to ping the gateway, it will wipe the IP of the first NIC and apply it to the second NIC, etc until it finds the NIC that has network connectivity.

As a back ground, this will be part of a Windows Server 2008 unattended build.  We have run into issues with our 2003 unattended that it pics the wrong NIC during the WinPE session.  Any help would be greatly appreciated.
LVL 3
ISWSIMBXAsked:
Who is Participating?
 
merowingerConnect With a Mentor Commented:
OK now it will set NICs with no connection to dhcp and then performs the next one
strComputer = "."
strPC2Validate="192.168.0.254"
bolConnection = False
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE AND Description LIKE '%VMWare%'")
 
strIPAddress = Array("192.168.0.1")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.0.254")
strGatewayMetric = Array(1)
 
For Each objNetAdapter in colNetAdapters
	If bolConnection = False Then
    	errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    	errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    	If errEnable = 0 Then
    	    WScript.Echo "The IP address was successfully changed on NIC "  &objNetAdapter.Description
        	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select Replysize from Win32_PingStatus where address = '" & strPC2Validate & "'") 
 
			For Each objStatus in objPing 
				If  IsNull(objStatus.ReplySize) Then 
					bolConnection = False
					WScript.Echo "No connection with NIC " &objNetAdapter.Description
					
					objNetAdapter.EnableDHCP()
					WScript.Echo "Enabled DHCP on " &objNetAdapter.Description
				Else 
					bolConnection = True
					WScript.Echo "Connection with NIC " &objNetAdapter.Description
				End If 
			Next 
 
			Set objPing=Nothing 
			Set objStatus=Nothing 
    	Else
        	WScript.Echo "The IP address could not be changed."  &objNetAdapter.Description
        	bolConnection = False
    	End If
   	Else
		WScript.Echo "Connection available"
		Exit For
	End If
Next

Open in new window

0
 
merowingerCommented:
Give the below listed script a try
strComputer = "."
strPC2Validate="192.168.0.254"
bolConnection = False
 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colNetAdapters = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE AND Description LIKE '%VMWare%'")
 
strIPAddress = Array("192.168.0.1")
strSubnetMask = Array("255.255.255.0")
strGateway = Array("192.168.0.254")
strGatewayMetric = Array(1)
 
For Each objNetAdapter in colNetAdapters
	If bolConnection = False Then
    	errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    	errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    	If errEnable = 0 Then
    	    WScript.Echo "The IP address was successfully changed on NIC "  &objNetAdapter.Description
        	Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery("select Replysize from Win32_PingStatus where address = '" & strPC2Validate & "'") 
 
			For Each objStatus in objPing 
				If  IsNull(objStatus.ReplySize) Then 
					bolConnection = False
					WScript.Echo "No connection with NIC " &objNetAdapter.Description
				Else 
					bolConnection = True
					WScript.Echo "Connection with NIC " &objNetAdapter.Description
				End If 
			Next 
 
			Set objPing=Nothing 
			Set objStatus=Nothing 
    	Else
        	WScript.Echo "The IP address could not be changed."  &objNetAdapter.Description
        	bolConnection = False
    	End If
   	Else
		WScript.Echo "Connection available"
		Exit For
	End If
Next

Open in new window

0
 
ISWSIMBXAuthor Commented:
Thanks merowinger.  I'll give that a shot and let you know how it works.
0
 
ISWSIMBXAuthor Commented:
Script works perfectly.  I really appreciate it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.