savindrasingh
Board Regular
- Joined
- Sep 10, 2009
- Messages
- 183
Hello Experts,
I am sharing this VB Script code to change IP address to Static or dynamic if anyone requires this:
Set to Static IP Adddress:
And to set IP to Dynamic:
Please feel free to ask if you have any question.
I am sharing this VB Script code to change IP address to Static or dynamic if anyone requires this:
Set to Static IP Adddress:
Code:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oNetwork = CreateObject("WScript.Network")
strCurrentComputerName = oNetwork.ComputerName
'Setup IP's you want to implament, you can add logic for MSGBOX etc.
strComputer = "." 'Default computer is current computer
IPAddress = "10.11.12.13"
dGateway = "10.11.12.11"
arrIPAddress = Array(IPAddress)
arrSubnetMask = Array("255.255.255.128")
arrGateway = Array(dGateway)
arrDNSServers = Array("12.107.200.146")
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnableStatic = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
errGateways = objNetAdapter.SetGateways(arrGateway)
intSetDNSServers = objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
Next
If errEnableStatic = 0 Then
MSGBOX("Server IPs set to: IPAddress = " & IPAddress)
Else
Select Case errEnableStatic
Case 0 : MsgBox ("Successful completion, no reboot required.")
Case 1 : MsgBox ("Successful completion, reboot required.")
Case 64 : MsgBox ("Method not supported on this platform.")
Case 65 : MsgBox ("Unknown failure.")
Case 66 : MsgBox ("Invalid subnet mask.")
Case 67 : MsgBox ("An error occurred while processing an instance that was returned.")
Case 68 : MsgBox ("Invalid input parameter.")
Case 69 : MsgBox ("More than five gateways specified.")
Case 70 : MsgBox ("Invalid IP address.")
Case 71 : MsgBox ("Invalid gateway IP address.")
Case 72 : MsgBox ("An error occurred while accessing the registry for the requested information.")
Case 73 : MsgBox ("Invalid domain name.")
Case 74 : MsgBox ("Invalid host name.")
Case 75 : MsgBox ("No primary or secondary WINS server defined.")
Case 76 : MsgBox ("Invalid file.")
Case 77 : MsgBox ("Invalid system path.")
Case 78 : MsgBox ("File copy failed.")
Case 79 : MsgBox ("Invalid security parameter.")
Case 80 : MsgBox ("Unable to configure TCP/IP service.")
Case 81 : MsgBox ("Unable to configure DHCP service.")
Case 82 : MsgBox ("Unable to renew DHCP lease.")
Case 83 : MsgBox ("Unable to release DHCP lease.")
Case 84 : MsgBox ("IP not enabled on adapter.")
Case 85 : MsgBox ("IPX not enabled on adapter.")
Case 86 : MsgBox ("Frame or network number bounds error.")
Case 87 : MsgBox ("Invalid frame type.")
Case 88 : MsgBox ("Invalid network number.")
Case 89 : MsgBox ("Duplicate network number.")
Case 90 : MsgBox ("Parameter out of bounds.")
Case 91 : MsgBox ("Access denied.")
Case 92 : MsgBox ("Out of memory.")
Case 93 : MsgBox ("Already exists.")
Case 94 : MsgBox ("Path, file, or object not found.")
Case 95 : MsgBox ("Unable to notify service.")
Case 96 : MsgBox ("Unable to notify DNS service.")
Case 97 : MsgBox ("Interface not configurable.")
Case 98 : MsgBox ("Not all DHCP leases could be released or renewed.")
Case 100 : MsgBox ("DHCP not enabled on adapter.")
Case Else: MsgBox ("Unknown error occurred while Setting IP Address.")
End Select
End If
If intSetDNSServers = 0 Then
MSGBOX("Server DNS search order is set to static value.")
Else
Select Case intSetDNSServers
Case 0 : MsgBox ("Successful completion, no reboot required.")
Case 1 : MsgBox ("Successful completion, reboot required.")
Case 64 : MsgBox ("Method not supported on this platform.")
Case 65 : MsgBox ("Unknown failure.")
Case 66 : MsgBox ("Invalid subnet mask.")
Case 67 : MsgBox ("An error occurred while processing an instance that was returned.")
Case 68 : MsgBox ("Invalid input parameter.")
Case 69 : MsgBox ("More than five gateways specified.")
Case 70 : MsgBox ("Invalid IP address.")
Case 71 : MsgBox ("Invalid gateway IP address.")
Case 72 : MsgBox ("An error occurred while accessing the registry for the requested information.")
Case 73 : MsgBox ("Invalid domain name.")
Case 74 : MsgBox ("Invalid host name.")
Case 75 : MsgBox ("No primary or secondary WINS server defined.")
Case 76 : MsgBox ("Invalid file.")
Case 77 : MsgBox ("Invalid system path.")
Case 78 : MsgBox ("File copy failed.")
Case 79 : MsgBox ("Invalid security parameter.")
Case 80 : MsgBox ("Unable to configure TCP/IP service.")
Case 81 : MsgBox ("Unable to configure DHCP service.")
Case 82 : MsgBox ("Unable to renew DHCP lease.")
Case 83 : MsgBox ("Unable to release DHCP lease.")
Case 84 : MsgBox ("IP not enabled on adapter.")
Case 85 : MsgBox ("IPX not enabled on adapter.")
Case 86 : MsgBox ("Frame or network number bounds error.")
Case 87 : MsgBox ("Invalid frame type.")
Case 88 : MsgBox ("Invalid network number.")
Case 89 : MsgBox ("Duplicate network number.")
Case 90 : MsgBox ("Parameter out of bounds.")
Case 91 : MsgBox ("Access denied.")
Case 92 : MsgBox ("Out of memory.")
Case 93 : MsgBox ("Already exists.")
Case 94 : MsgBox ("Path, file, or object not found.")
Case 95 : MsgBox ("Unable to notify service.")
Case 96 : MsgBox ("Unable to notify DNS service.")
Case 97 : MsgBox ("Interface not configurable.")
Case 98 : MsgBox ("Not all DHCP leases could be released or renewed.")
Case 100 : MsgBox ("DHCP not enabled on adapter.")
Case Else: MsgBox ("Unknown error occurred while Setting IP Address.")
End Select
End If
If errGateways = 0 Then
MSGBOX("Server Gateway IP is set to static value.")
Else
Select Case errGateways
Case 0 : MsgBox ("Successful completion, no reboot required.")
Case 1 : MsgBox ("Successful completion, reboot required.")
Case 64 : MsgBox ("Method not supported on this platform.")
Case 65 : MsgBox ("Unknown failure.")
Case 66 : MsgBox ("Invalid subnet mask.")
Case 67 : MsgBox ("An error occurred while processing an instance that was returned.")
Case 68 : MsgBox ("Invalid input parameter.")
Case 69 : MsgBox ("More than five gateways specified.")
Case 70 : MsgBox ("Invalid IP address.")
Case 71 : MsgBox ("Invalid gateway IP address.")
Case 72 : MsgBox ("An error occurred while accessing the registry for the requested information.")
Case 73 : MsgBox ("Invalid domain name.")
Case 74 : MsgBox ("Invalid host name.")
Case 75 : MsgBox ("No primary or secondary WINS server defined.")
Case 76 : MsgBox ("Invalid file.")
Case 77 : MsgBox ("Invalid system path.")
Case 78 : MsgBox ("File copy failed.")
Case 79 : MsgBox ("Invalid security parameter.")
Case 80 : MsgBox ("Unable to configure TCP/IP service.")
Case 81 : MsgBox ("Unable to configure DHCP service.")
Case 82 : MsgBox ("Unable to renew DHCP lease.")
Case 83 : MsgBox ("Unable to release DHCP lease.")
Case 84 : MsgBox ("IP not enabled on adapter.")
Case 85 : MsgBox ("IPX not enabled on adapter.")
Case 86 : MsgBox ("Frame or network number bounds error.")
Case 87 : MsgBox ("Invalid frame type.")
Case 88 : MsgBox ("Invalid network number.")
Case 89 : MsgBox ("Duplicate network number.")
Case 90 : MsgBox ("Parameter out of bounds.")
Case 91 : MsgBox ("Access denied.")
Case 92 : MsgBox ("Out of memory.")
Case 93 : MsgBox ("Already exists.")
Case 94 : MsgBox ("Path, file, or object not found.")
Case 95 : MsgBox ("Unable to notify service.")
Case 96 : MsgBox ("Unable to notify DNS service.")
Case 97 : MsgBox ("Interface not configurable.")
Case 98 : MsgBox ("Not all DHCP leases could be released or renewed.")
Case 100 : MsgBox ("DHCP not enabled on adapter.")
Case Else: MsgBox ("Unknown error occurred while Setting IP Address.")
End Select
End If
And to set IP to Dynamic:
Code:
Set WshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set oNetwork = CreateObject("WScript.Network")
strCurrentComputerName = oNetwork.ComputerName
'Setup IP's you want to implament, you can add logic for MSGBOX etc.
strComputer = "." 'Default computer is current computer
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
For Each objNetAdapter in colNetAdapters
errEnableDynamic = objNetAdapter.EnableDHCP()
Next
If errEnableDynamic = 0 Then
MSGBOX("Server IPs set to Dynamic")
Else
Select Case errEnableDynamic
Case 0 : MsgBox ("Successful completion, no reboot required.")
Case 1 : MsgBox ("Successful completion, reboot required.")
Case 64 : MsgBox ("Method not supported on this platform.")
Case 65 : MsgBox ("Unknown failure.")
Case 66 : MsgBox ("Invalid subnet mask.")
Case 67 : MsgBox ("An error occurred while processing an instance that was returned.")
Case 68 : MsgBox ("Invalid input parameter.")
Case 69 : MsgBox ("More than five gateways specified.")
Case 70 : MsgBox ("Invalid IP address.")
Case 71 : MsgBox ("Invalid gateway IP address.")
Case 72 : MsgBox ("An error occurred while accessing the registry for the requested information.")
Case 73 : MsgBox ("Invalid domain name.")
Case 74 : MsgBox ("Invalid host name.")
Case 75 : MsgBox ("No primary or secondary WINS server defined.")
Case 76 : MsgBox ("Invalid file.")
Case 77 : MsgBox ("Invalid system path.")
Case 78 : MsgBox ("File copy failed.")
Case 79 : MsgBox ("Invalid security parameter.")
Case 80 : MsgBox ("Unable to configure TCP/IP service.")
Case 81 : MsgBox ("Unable to configure DHCP service.")
Case 82 : MsgBox ("Unable to renew DHCP lease.")
Case 83 : MsgBox ("Unable to release DHCP lease.")
Case 84 : MsgBox ("IP not enabled on adapter.")
Case 85 : MsgBox ("IPX not enabled on adapter.")
Case 86 : MsgBox ("Frame or network number bounds error.")
Case 87 : MsgBox ("Invalid frame type.")
Case 88 : MsgBox ("Invalid network number.")
Case 89 : MsgBox ("Duplicate network number.")
Case 90 : MsgBox ("Parameter out of bounds.")
Case 91 : MsgBox ("Access denied.")
Case 92 : MsgBox ("Out of memory.")
Case 93 : MsgBox ("Already exists.")
Case 94 : MsgBox ("Path, file, or object not found.")
Case 95 : MsgBox ("Unable to notify service.")
Case 96 : MsgBox ("Unable to notify DNS service.")
Case 97 : MsgBox ("Interface not configurable.")
Case 98 : MsgBox ("Not all DHCP leases could be released or renewed.")
Case 100 : MsgBox ("DHCP not enabled on adapter.")
Case Else: MsgBox ("Unknown error occurred while Setting IP Address.")
End Select
End If
Please feel free to ask if you have any question.