?
Solved

Script Help

Posted on 2009-04-15
4
Medium Priority
?
359 Views
Last Modified: 2013-11-10
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.
0
Comment
Question by:ISWSIMBX
  • 2
  • 2
4 Comments
 
LVL 31

Expert Comment

by:merowinger
ID: 24155771
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
 
LVL 31

Accepted Solution

by:
merowinger earned 2000 total points
ID: 24155814
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
 
LVL 3

Author Comment

by:ISWSIMBX
ID: 24157609
Thanks merowinger.  I'll give that a shot and let you know how it works.
0
 
LVL 3

Author Closing Comment

by:ISWSIMBX
ID: 31570696
Script works perfectly.  I really appreciate it.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This tutorial will show how to configure a new Backup Exec 2012 server and move an existing database to that server with the use of the BEUtility. Install Backup Exec 2012 on the new server and apply all of the latest hotfixes and service packs. The…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question