Friday, December 26, 2008

How to assign a static IP address using VBScript?

This type of scripts are always in demand by the network proffessionals. Here is a handy script that assigns a static IP address to a machine:

strComputer = "."
Set objWMIService = GetObject ("winmgmts:" & "{impersonationLevel = impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strIPAddress = Array("")
strSubnetMask= Array("")
strGateway = Array("")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
errorEnable = objNetAdapter.EnableStatic(strIPAddress,strSubnetMask)
errorGateway= objNetAdapter.SetGateways(strGateway, strGatewayMetric)
If errorEnable = 0 Then
WScript.Echo "The IP Address has been changed successfully!!"
WScript.Echo "Their was an error, the IP Address could not be changed!"
End If
Hope this helps :)


Post a Comment