Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

How To Change Ip Address Using Vb Script


  • Please log in to reply
1 reply to this topic

#1 akh

akh

  • Members
  • 3 posts
  • OFFLINE
  •  
  • Local time:07:49 PM

Posted 10 September 2008 - 09:06 AM

How to change ip address in system which is having multiple network adapters( wireless , lan, bluetooth etc..) using vb script

I have code which changes for single adapter.

If I execute the same code then it reflects on all network adapter..I want it to update only on particular network adapter.

Any suggestion ...

On Error Resume Next

' DNS Section

sDNS1 = "192.168.6.1"
'sDNS2 = ""

' IP & Gateway Section
sIPAddress = "192.168.248.111"
sSubnetMask = "255.255.255.0"
sGateway = "192.168.248.253"

' WINS Section
' 0 : enable NetBIOS via DHCP
' 1 : enable NetBIOS
' 2 : disable NetBIOS

'iNetBios = 1

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

' DNS Section
For Each objNetAdapter in colNetAdapters
arrDNSServers = Array(sDNS1)
objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
Next


' WINS Section
'For Each objNetAdapter in colNetAdapters
'objNetAdapter.SetTCPIPNetBIOS(iNetBios)
'Next


arrIPAddress = Array(sIPAddress)
arrSubnetMask = Array(sSubnetMask)
arrGateway = Array(sGateway)
arrGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
errGateways = objNetAdapter.SetGateways(arrGateway, arrGatewaymetric)

If errEnable = 0 Then

sMsg = "IP address has changed to: " & sIPAddress & vbCrLf
sMsg = sMsg & "Subnet has changed to: " & sSubnetMask & vbCrLf
sMsg = sMsg & "Gateway has changed to: " & sGateway & vbCrLf & vbCrLf
sMsg = sMsg & "DNS 1 has changed to: " & sDNS1 & vbCrLf
'sMsg = sMsg & "DNS 2 has changed to: " & sDNS2 & vbCrLf

WScript.Echo sMsg
Else
WScript.Echo "The IP address could not be changed."
End If
Next


I got the code to change for particular network adapter but its not working (don't know how to use)

("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'realtek ")

http://www.microsoft.com/technet/scriptcenter/resources/qanda/mar06/hey0329.mspx

Any idea how to use....in the above script....

BC AdBot (Login to Remove)

 


#2 akh

akh
  • Topic Starter

  • Members
  • 3 posts
  • OFFLINE
  •  
  • Local time:07:49 PM

Posted 16 September 2008 - 04:05 AM

I got the final code done for changing IP address for multiple network adapter

this is wat we are supposed to do when multiple network adapter is present

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'Broadcom NetXtreme 57xx Gigabit Controller - Packet Scheduler Miniport'")


'Vb Script to change IP address for Single and Multiple Network Adapter

On Error Resume Next

' IP & Gateway Section
sIPAddress = "192.168.248.250"
sSubnetMask = "255.255.255.0"
sGateway = "192.168.248.253"

' DNS Section

sDNS1 = "192.168.6.1"

'if secondary DNS is present

'sDNS2 = ""



' WINS Section
' 0 : enable NetBIOS via DHCP
' 1 : enable NetBIOS
' 2 : disable NetBIOS

'iNetBios = 1

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'Enable below 2 lines only when 1 Network Adapter is present and comment Multiple Network Adapter

'Set colNicConfigs = objWMIService.ExecQuery _
' ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

' Mention Exact Network Adapter Description when Multiple Network Adapters are present

Set colNetAdapters = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration Where Description = " & _
"'Broadcom NetXtreme 57xx Gigabit Controller - Packet Scheduler Miniport'")


' DNS Section
For Each objNetAdapter in colNetAdapters

arrDNSServers = Array(sDNS1)

'if secondary DNS is present
'arrDNSServers = Array("sDNS1", "sDNS2")

objNetAdapter.SetDNSServerSearchOrder(arrDNSServers)
Next


' WINS Section
'For Each objNetAdapter in colNetAdapters
'objNetAdapter.SetTCPIPNetBIOS(iNetBios)
'Next


arrIPAddress = Array(sIPAddress)
arrSubnetMask = Array(sSubnetMask)
arrGateway = Array(sGateway)
arrGatewayMetric = Array(1)

For Each objNetAdapter in colNetAdapters
errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
errGateways = objNetAdapter.SetGateways(arrGateway, arrGatewaymetric)

If errEnable = 0 Then

sMsg = "IP address has changed to: " & sIPAddress & vbCrLf
sMsg = sMsg & "Subnet has changed to: " & sSubnetMask & vbCrLf
sMsg = sMsg & "Gateway has changed to: " & sGateway & vbCrLf & vbCrLf
sMsg = sMsg & "DNS 1 has changed to: " & sDNS1 & vbCrLf
'sMsg = sMsg & "DNS 2 has changed to: " & sDNS2 & vbCrLf

WScript.Echo sMsg
Else
WScript.Echo "The IP address could not be changed."
End If
Next




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users