Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to change Network Properties

Status
Not open for further replies.

Jagger15

MIS
May 26, 2006
20
0
0
CA
Hi,

I am bad with VBScript and I need a script so that I can change the network properties on each workstation. I need to change the DNS properties and WINS servers, and I don't want to go around and manually change 150 PC's

Thanks in advance
 
Are you using DHCP? If so then you just need to change these options ont he DHCP server. If you are not then I suggest configuring DHCP and set the workstations accordingly.

You can do what you are asking using NETSH.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
i use 2 subs to do this, setstatic then setIPaddress, the oNIC class is simply

Class clsNIC

Public MACAddress
Public DNSServers
Public Gateway
Public SubnetMask
Public Disable
Public IPAddress

End Class

the clsNIC is created by reading an xml which contains device ipaddress information

Sub SetIP(ByRef objWMIService, ByVal oNIC)

Dim strMACAddress, colNetAdapters, objNetAdapter
Dim errEnable, arrIPAddress, arrSubnetMask, arrGateway, arrDNSServerSearchOrder
Dim strGatewayMetric
strGatewayMetric = Array(1)
strMACAddress = Replace(oNIC.MACAddress, "-", ":")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each objNetAdapter in colNetAdapters
If UCase(objNetAdapter.MACAddress) = UCase(strMACAddress) Then

arrDNSServerSearchOrder = Split(oNIC.DNSServers, ",")

If oNIC.IPAddress <> "" Then
arrIPAddress = Array(oNIC.IPAddress)
If oNIC.SubnetMask <> "" Then
arrSubnetMask = Array(oNIC.SubnetMask)
Else
arrSubnetMask = Array("255.255.0.0.")
End If
errEnable = objNetAdapter.EnableStatic(arrIPAddress, arrSubnetMask)
End If

If oNIC.Gateway <> "" Then
arrGateway = Array(oNIC.GateWay)
If IsArray(arrGateway) Then
errEnable = objNetAdapter.SetGateways(arrGateway, strGatewaymetric)
End If
Else
objNetAdapter.SetGateways(arrGateway)
End If

If oNIC.DNSServers <> "" Then
arrDNSServerSearchOrder = Split(oNIC.DNSServers, ",")
If IsArray(arrDNSServerSearchOrder) Then
errEnable = objNetAdapter.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
End If
Else
errEnable = objNetAdapter.SetDNSServerSearchOrder(arrDNSServerSearchOrder)
End If

'errWINS = objNetAdapter.SetWINSServer(strWINSPrimaryServer, strWINSSecondaryServer)

If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Else
Wscript.Echo "no match " & UCase(objNetAdapter.MACAddress) & "<>" & UCase(strMACAddress)
End If
Next

Set colNetAdapters = Nothing

End Sub



Sub setStatic(ByVal strMACAddress, ByRef objWMIService)
Dim colNetAdapters, objNetAdapter
Dim errEnable, errGateways, errdns1, errWINS
Dim strIPAddress, strSubnetMask, strGateway, strGatewayMetric, strDNSServerSearchOrder
Dim strWINSPrimaryServer, strWINSSecondaryServer
Dim i

strGatewayMetric = Array(1)
strMACAddress = Replace(strMACAddress, "-", ":")
Set colNetAdapters = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")

For Each objNetAdapter in colNetAdapters
If UCase(objNetAdapter.MACAddress) = UCase(strMACAddress) Then
strIPAddress = ""
strSubnetMask = ""
strGateway = ""
strDNSServerSearchOrder = ""
strWINSPrimaryServer = objNetAdapter.WINSPrimaryServer
strWINSSecondaryServer = objNetAdapter.WINSSecondaryServer
If Not IsNull(objNetAdapter.IPAddress) Then
strIPAddress = objNetAdapter.IPAddress
For i = 0 To UBound(objNetAdapter.IPAddress)
WScript.Echo " IP address:" & objNetAdapter.IPAddress(i)
'strIPAddress = Array(objNetAdapter.IPAddress(i))
Next
End If

If Not IsNull(objNetAdapter.IPSubnet) Then
strSubnetMask = objNetAdapter.IPSubnet
For i = 0 To UBound(objNetAdapter.IPSubnet)
WScript.Echo " Subnet:" & objNetAdapter.IPSubnet(i)
'strSubnetMask = Array(objNetAdapter.IPSubnet(i))
Next
End If

If Not IsNull(objNetAdapter.DefaultIPGateway) Then
strGateway = objNetAdapter.DefaultIPGateway
For i = 0 To UBound(objNetAdapter.DefaultIPGateway)
WScript.Echo " Default gateway:" & objNetAdapter.DefaultIPGateway(i)
'strGateway = Array(objNetAdapter.DefaultIPGateway(i))
Next
End If


If Not IsNull(objNetAdapter.DNSServerSearchOrder) Then
strDNSServerSearchOrder = objNetAdapter.DNSServerSearchOrder
For i = 0 To UBound(objNetAdapter.DNSServerSearchOrder)
WScript.Echo " DNSServer:" & objNetAdapter.DNSServerSearchOrder(i)
'strGateway = Array(objNetAdapter.DefaultIPGateway(i))
Next
End If

errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
If IsArray(strGateway) Then
errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
End If

If IsArray(strDNSServerSearchOrder) Then
errdns1 = objNetAdapter.SetDNSServerSearchOrder(strDNSServerSearchOrder)
End If

errWINS = objNetAdapter.SetWINSServer(strWINSPrimaryServer, strWINSSecondaryServer)

If errEnable = 0 Then
WScript.Echo "The IP address has been changed."
Else
WScript.Echo "The IP address could not be changed."
End If
Else
Wscript.Echo "no match " & UCase(objNetAdapter.MACAddress) & "<>" & UCase(strMACAddress)
End If
Next

Set colNetAdapters = Nothing



End Sub
 
I am using Static IPS. Can NETSH change all PC's with one command?

Thanks Mark U have been so helpful
 
nopre you will need to execute NetSh locally on the machine
 
Are you using AD? If so, you can do this with a logon script.



Thanks,
Andrew
 
I am using AD. Do you have any sample code for the logon script??
 
I have the computer names and IP's in a text file. How can I loop psexec through the names to make the changes?? Anybody now the command?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top