Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


I need help writing a VBS script that changes ONLY the subnet mask for 200 computers in my domain.

Posted on 2008-11-07
Medium Priority
Last Modified: 2012-05-05
My network consists of a Server 2003 servers, W2K, and XP clients. I have around 200 PCs where I need to change the subnet mask so I can move forward with configuring a trust on the Domain Controller. I have a script that is able to change everything: IP, Subnet, and Gateway. My issue is that I don't need to change the IP or gateway, just subnet mask. The script I'm using does not allow for only one of the three items to change. If I take away one of the options, it errors. Can someone help providing scripting syntax that leaves the static IP as what it is, but only changes the subnet mask? Here's what I have so far...

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
    errEnable = objNetAdapter.EnableStatic(strIPAddress, strSubnetMask)
    errGateways = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    If errEnable = 0 Then
        WScript.Echo "The IP address has been changed."
        WScript.Echo "The IP address could not be changed."
    End If
Question by:s1979
LVL 14

Accepted Solution

rejoinder earned 2000 total points
ID: 22909293
This will keep the IP settings and carry them over as it updates the subnet mask.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNetAdapters = objWMIService.ExecQuery _
    ("Select * from Win32_NetworkAdapterConfiguration where IPEnabled=TRUE")
strSubnetMask    = Array("")
strGateway       = Array("")
strGatewayMetric = Array(1)
For Each objNetAdapter in colNetAdapters
    arrIPAddress  = objNetAdapter.IPAddress
    'arrDNSSetting = objNetAdapter.DNSServerSearchOrder
    errEnable     = objNetAdapter.EnableStatic(arrIPAddress, strSubnetMask)
    errGateways   = objNetAdapter.SetGateways(strGateway, strGatewaymetric)
    'errDNSSetting = objNetAdapter.SetDNSServerSearchOrder(arrDNSSetting)
    Select Case errEnable
        Case 0
            wscript.echo "Successful completion, no reboot required."
        Case 1
            wscript.echo "Successful completion, reboot required."
        Case 64
            wscript.echo "Method not supported on this platform."
        Case 65
            wscript.echo "Unknown failure."
        Case 66
            wscript.echo "Invalid subnet mask."
        Case 67
            wscript.echo "An error occurred while processing an instance that was returned."
        Case 68
            wscript.echo "Invalid input parameter."
        Case 69
            wscript.echo "More than five gateways specified."
        Case 70
            wscript.echo "Invalid IP address."
        Case 71
            wscript.echo "Invalid gateway IP address."
        Case 72
            wscript.echo "An error occurred while accessing the registry for the requested information."
        Case 73
            wscript.echo "Invalid domain name."
        Case 74
            wscript.echo "Invalid host name."
        Case 75
            wscript.echo "No primary or secondary WINS server defined."
        Case 76
            wscript.echo "Invalid file."
        Case 77
            wscript.echo "Invalid system path."
        Case 78
            wscript.echo "File copy failed."
        Case 79
            wscript.echo "Invalid security parameter."
        Case 80
            wscript.echo "Unable to configure TCP/IP service."
        Case 81
            wscript.echo "Unable to configure DHCP service."
        Case 82
            wscript.echo "Unable to renew DHCP lease."
        Case 83
            wscript.echo "Unable to release DHCP lease."
        Case 84
            wscript.echo "IP not enabled on adapter."
        Case 85
            wscript.echo "IPX not enabled on adapter."
        Case 86
            wscript.echo "Frame or network number bounds error."
        Case 87
            wscript.echo "Invalid frame type."
        Case 88
            wscript.echo "Invalid network number."
        Case 89
            wscript.echo "Duplicate network number."
        Case 90
            wscript.echo "Parameter out of bounds."
        Case 91
            wscript.echo "Access denied."
        Case 92
            wscript.echo "Out of memory."
        Case 93
            wscript.echo "Already exists."
        Case 94
            wscript.echo "Path, file, or object not found."
        Case 95
            wscript.echo "Unable to notify service."
        Case 96
            wscript.echo "Unable to notify DNS service."
        Case 97
            wscript.echo "Interface not configurable."
        Case 98
            wscript.echo "Not all DHCP leases could be released or renewed."
        Case 100
            wscript.echo "DHCP not enabled on adapter."
    end select
wscript.echo "Done."

Open in new window


Author Closing Comment

ID: 31514542
Thank you very much! You've helped me save the day!!

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question