Editing Network Adapters using VBScript

Posted on 2008-10-28
Last Modified: 2013-11-10
I am fairly new to vbs.  I am trying to write a script for my servers, so that I can edit thier network adapter easier on builds and rebuilds.  I have pieced together the following script from snippets on the web, and edited it so that I would prompt for input instead of statically assigning the variables, however it now gives me a type mismatch on line 26...Any ideas?  Also I would like to modify it so that it edits and prompts for the primary and secondary dns.

strConnection = "Local Area Connection"
strComputer = InputBox( "What is the name of the Server?" ) 
strIP = InputBox( "What is the new IP?:" )
strMask = InputBox( "What is the Subnet Mask for this IP?" )
strGatewayIP = InputBox( "What is the default gateway?" )
MsgBox("You entered Server" & " " & strComputer & " "  & "and the IP" & " " & strIP)
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set colNA = objWMI.ExecQuery("select * " & _
                            " from Win32_NetworkAdapter " & _
                            " where NetConnectionID = '" & strConnection & "'" ) 
for each objNA in colNA
   set colNAConfig = objWMI.ExecQuery _
      ("ASSOCIATORS OF {Win32_NetworkAdapter.DeviceID='" & _
        objNA.DeviceID & "'} " & _
      " WHERE resultClass = win32_NetworkAdapterConfiguration ")
   for each objNAConfig in colNAConfig
      intRC = objNAConfig.EnableStatic(strIP,strMask)
      intRC2 = objNAConfig.SetGateways(strGatewayIP)
      if intRC = 0 and intRC2 = 0 then
         WScript.Echo "IP address configured for " & strConnection
      elseif intRC = 1 or intRC2 = 1 then
         WScript.Echo "You must reboot for the changes to take effect for " & _
         WScript.Echo "There was an error configuring IP for " & _
                      strconnection & ": " & intRC & " and " & intRC2
      end if

Open in new window

Question by:corphealth
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4

Author Comment

ID: 22823332
make that line 18 =)

Expert Comment

ID: 22829062
This script is just working fine for me. At least no errors a line 18
What version of server are you running ?

Author Comment

ID: 22832740
i changed the script now im getting the error in the screen shot below... Are you saying you used this script and it actually changed the network details for the selected box?

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.


Author Comment

ID: 22832754
Im running it from an XP machine

Expert Comment

ID: 22833843
Usually a type mismatch is because your are trying to input or test a using a string or another type against an int or something to that effect. Are you sure you don't have to parse out the ip address or something so that it is an int and not a string? I will look more closely when I get a chance, but try looking over your code to make sure that everything should be a string against string check/enter. If that's not it, maybe look at your strings and check into the escape characters for your "path\path\". sometimes you have to add an extra escape character so that it reads the path line properly, and that can cause issues as well. I will look at your code closer when I get off work and can check it out.

Expert Comment

ID: 22833900
Sorry, I also noticed on your line 26. You are trying to make an int variable = to your enable static with string input params. Are you at any point changing the input to int type? Maybe try accepting the ip addy as an int instead and just parse or convert the input coming in from the input box from string to int.

Expert Comment

ID: 22833986
One last thing for now. I also noticed that you are inside your foreach statement while doing some of this. I think that inside there, and much as your code says with the objNAconfig..etc, you are dealing with objects and trying to assign and object to an int. I know, at least in some cases, you have to parse or convert the object from an object type to an int if you are trying to assign it to an int variable. Again, i'll check back later. Sorry for all the posts. I'll stop now for awhile. Hope all this helps.

Accepted Solution

corphealth earned 0 total points
ID: 22834543
changed lines 26 and 27 to the below and it now works.

intRC = objNAConfig.EnableStatic(Array(strIP), Array(strMask))   
      intRC2 = objNAConfig.SetGateways(Array(strGatewayIP))

Open in new window


Expert Comment

ID: 22834701
So it was looking for a collection.. that explains it. Nice work. Congrats :)

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

740 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