How to write a CMD or VBS script to change the NIC settings on a group of PCs to DHCP

Posted on 2005-04-06
Last Modified: 2010-08-05
Hello experts,
I have a .txt file containing about 100pc hostnames. Unfortunatly we do not have AD set up where the PC's are in OU's. It would take as much time to manually add them to OU's as it would to use the netsh cmd file and push the settings.

I am wondering if somebody could help me write a script to read from a .txt file for the hostnames and then change the NIC settings to DHCP enabled?

Thanks much, i did post a question like this before, but i have not figured out how to pass the data from the text file to the script.

Thanks everybody : - )

I was thinking of this???(not sure, still new to VBScript)
Set Net = Wscript.CreateObject("Wscript.Network")
strComputer = ##read from txt file##

Question by:sqlagent007
    LVL 15

    Accepted Solution

    This is a variation of my script I used to move my network workstations from a Static to a Dynamic IP Address scheme.  I used computer groups but it could easily be done with your text script upon login/logoff.  I haven't tried it with the netsh util.

    <><>Begin Script<><>

    On Error Resume Next

    Set Net = Wscript.CreateObject("Wscript.Network")
    Set FSO = CreateObject("Scripting.FileSystemObject")
    strComputer = net.Computername
    Set ListFile = FSO.OpenTextFile("textfile")
    CompList = ListFile.readall

    if inStr(CompList, strComputer) then

          Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
          strQuery = "Select * from Win32_NetworkAdapterConfiguration " & "where IPEnabled=TRUE"

          Set colNetAdapters = objWMIService.ExecQuery(strQuery)
          For Each objNetAdapter In colNetAdapters
                errEnable = objNetAdapter.EnableDHCP()
                errDNS = objNetAdapter.SetDNSServerSearchOrder(null)
                errWINS = objNetAdapter.SetWINSServer("", "")
                RemoveGTW strComputer, objNetAdapter.SettingID
    end if

    Sub RemoveGTW(strComputer, lsnicsid)
          Const HKEY_LOCAL_MACHINE = &H80000002
          arrStringValues = Array()
          strEntryName = "DefaultGateway"

          lsStrWMI = "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv"
          Set objReg=GetObject(lsStrWMI)

          strKeyPath = "SYSTEM\CurrentControlSet\Services\" & lsNicSid & "\Parameters\Tcpip\"
          objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

          strKeyPath = "SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\" & lsNicSid
          objReg.SetMultiStringValue HKEY_LOCAL_MACHINE,strKeyPath, strEntryName,arrStringValues

    End Sub

    <><>End Script<><>
    LVL 15

    Expert Comment

    You might want to change two of the lines to ensure that the script runs as anticipated:

    strComputer = net.Computername -> strComputer = ucase(Net.ComputerName)

    add this

    CompList = ucase(CompList)  after CompList = ListFile.readall

    I am not sure if you can do this instead CompList = ucase(ListFile.readall).
    LVL 23

    Expert Comment

    Go sneek a peek at this URL  :

    Basically shows you how to programitcally change network card settings :)

    Kind regards

    LVL 1

    Author Comment

    SR75, can you tell me where in your script i would enter the computer hostname? Assuming i don't get it from a file.

    what if i just want to run this script 1 time on 1 PC? Where would the hostname go?


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    Suggested Solutions

    A short article about a problem I had getting the GPS LocationListener working.
    This is about my first experience with programming Arduino.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now