Solved

Editing Network Adapters using VBScript

Posted on 2008-10-28
9
617 Views
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 " & _

                      strConnection

      else

         WScript.Echo "There was an error configuring IP for " & _

                      strconnection & ": " & intRC & " and " & intRC2

      end if

   next

next

Open in new window

0
Comment
Question by:corphealth
  • 4
  • 4
9 Comments
 

Author Comment

by:corphealth
Comment Utility
make that line 18 =)
change.ip.vbs.error.jpg
0
 
LVL 3

Expert Comment

by:Angelized
Comment Utility
This script is just working fine for me. At least no errors a line 18
What version of server are you running ?
0
 

Author Comment

by:corphealth
Comment Utility
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?

change.ip.vbs.error2.jpg
0
 

Author Comment

by:corphealth
Comment Utility
Im running it from an XP machine
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Expert Comment

by:jimmyb0004
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:jimmyb0004
Comment Utility
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.
0
 
LVL 1

Expert Comment

by:jimmyb0004
Comment Utility
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.
0
 

Accepted Solution

by:
corphealth earned 0 total points
Comment Utility
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

0
 
LVL 1

Expert Comment

by:jimmyb0004
Comment Utility
So it was looking for a collection.. that explains it. Nice work. Congrats :)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…

744 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

14 Experts available now in Live!

Get 1:1 Help Now