TUTOS.EU

Configurer l'adresse Ip en VbScript

Commenter paramétrer l'adresse Ip d'une carte réseau en VbScript

Option Explicit
'7th july version
'Configure static ip parameters on the first Enable network card with vbscript whe can found

'http://msdn.microsoft.com/en-us/library/gg196652%28v=vs.85%29.aspx

Dim strComputer
Dim objWMIService, colItems
Dim NetworkId, CardGUID, objItem, colNetAdapters
Dim strIPAddress, strSubnetMask, strGateway, strGatewayMetric, strDNS1, strDNS2, strWINS1, strWINS2, objNetAdapter, arrDNSServers, DNSSuffixName, DNSSuffixSearchOrder, intSetWINS, intSetWINSServer
Dim errorEnable, errorGateway

'Configure here your ip parameters
strIPAddress = Array("192.168.1.54")
strSubnetMask= Array("255.255.255.0")
strGateway = Array("192.168.1.254")
strGatewayMetric = Array(1)
strDNS1 = "192.168.1.51"
strDNS2 = ""
strWINS1 = "192.168.1.51"
strWINS2 = ""
DNSSuffixName = "MyDomain.local"
DNSSuffixSearchOrder = Array(DNSSuffixName)

strComputer = "."


Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2") 
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter Where AdapterType = 'Ethernet 802.3'")
'Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter Where NetConnectionId = 'Interne'")

NetworkId = ""
CardGUID = ""
'We look for the firt available Ethernet network card GUID
For Each objItem in colItems 
	NetworkId = objItem.NetConnectionId
	CardGUID = objItem.GUID
	
	'Wscript.Echo "NetConnectionId: " & objItem.NetConnectionId
	'Wscript.Echo "Name : " & objItem.Name
	'Wscript.Echo "GUID : " & objItem.GUID
	'Wscript.Echo "Description : " & objItem.Description
	'Wscript.Echo "AdapterType : " & objItem.AdapterType
	'Wscript.Echo "NetEnabled : " & objItem.NetEnabled
	'objItem.Disable 'In order to disable the network card
	Exit for

Next
Set objWMIService = Nothing

'Now we will configure the first available Ethernet network card GUID
Set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
'Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where SettingID='" & CardGUID & "'")

For Each objNetAdapter in colNetAdapters
 
	errorEnable = objNetAdapter.EnableDHCP
	errorEnable = objNetAdapter.EnableStatic(strIPAddress,strSubnetMask)
	errorGateway= objNetAdapter.SetGateways(strGateway, strGatewayMetric)

	If errorEnable = 0 Then
		WScript.Echo "The IP Address has been changed successfully!!"
	Else
		WScript.Echo "Their was an error, the IP Address could not be changed!"
	End If

	If Len(strDNS2) > 0 Then
		arrDNSServers = Array(strDNS1, strDNS2)
	Else
		If Len(strDNS1) > 0 Then arrDNSServers = Array(strDNS1)
	End If
	
	objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)  
	intSetWINS = objNetAdapter.SetWINSServer(strWINS1, strWINS2)
	If intSetWINSServer = 0 Then
		WScript.Echo "Success! WINS & DNS servers configured."
	ElseIf intSetWINSServer = 1 Then
		WScript.Echo "WINS  & DNS servers configured, please reboot."
	Else
		WScript.Echo "Error!! Unable to configure WINS & DNS servers."
	End If
	
	objNetAdapter.SetDNSDomain(DNSSuffixName)
	'objNetAdapter.SetDNSServerSearchOrder(DNSSuffixSearchOrder)

Next

Set objWMIService = Nothing
Lien vers le fichier : cliquez ici Copier le code

Article(s) précédent(s)

2