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("192.168.1.202")
strSubnetMask= Array("255.255.255.0")
strGateway = Array("192.168.1.100")
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!!"
Else
WScript.Echo "Their was an error, the IP Address could not be changed!"
End If
Next
Hope this helps :)

0 comments:

Post a Comment