Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I want to create a batch or vbs file that will put together a url and executed. Part of that url needs to be the actual ip address of the machine. How I am able to get that IP address in a variable to include it on the script?

EDIT 1:

I found out that the command below will give me the IP Address, but still don't know how to get that value into a variable to use it in a script.

c:\> wmic NICCONFIG WHERE IPEnabled=true GET IPAddress /format:csv

Node,IPAddress
IP-0AFB,{10.25.5.2}
share|improve this question

6 Answers 6

Here's some sample code I used in a previous script...

Dim myIPAddress : myIPAddress = ""
Dim objWMIService : Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim colAdapters : Set colAdapters = objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration Where IPEnabled = True")
Dim objAdapter
For Each objAdapter in colAdapters
  If Not IsNull(objAdapter.IPAddress) Then myIPAddress = trim(objAdapter.IPAddress(0))
  exit for
Next

Wscript.echo "My IPAddress is " & myIPAddress
share|improve this answer
    
Thanks! I am getting runtime error: Object Required 'idLocalIPAddress'. Then I added a line Dim idLocalIPAddress, and now I am getting runtime error: Object required: ''. Any comments? –  Geo Jul 31 '09 at 15:39
    
With Option Explict set you will need to ensure a Dim for every variable... for my example I just gave you the quick and dirty, leaving it up to you to add the Dim statements... To step through vbscript you can launch the script with WSCRIPT.EXE //X scriptname.vbs and it will launch the Just-In-Time debugger, allowing you to step through your code in Visual Studio. Hopefully stepping through your code you can pinpoint the cause of your failure. –  Dscoduc Jul 31 '09 at 15:53
    
See the updated code... –  Dscoduc Jul 31 '09 at 15:56

If you want to return the IP address into a variable, you could do the following:

Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled='TRUE'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
            strIPAddress = Join(objItem.IPAddress, ",")
            *yourFunctionName*(strIPAddress)
Next

This code is taken straight from the Scriptomatic v2.0 from Microsoft's TechNet Scriptcenter. Found here: http://technet.microsoft.com/en-us/scriptcenter/dd939957.aspx

share|improve this answer

Here is a way to extract the WMIC results into a variable in a cmd script:

@echo off
setlocal
set varcounter=0
set wmicmd="wmic NICCONFIG WHERE IPEnabled=true GET IPAddress"
for /f "tokens=1 delims={, skip=1" %%a in ('%wmicmd%') do call :SETVAR %%a
endlocal
goto :eof

:SETVAR
set /a varcounter=%varcounter% + 1
if not {%1}=={} (
    echo NIC %varcounter% address is {%1}
    set NIC%varcounter%=%1
)
goto :eof

Notice that since there may be multiple NICs, we have to loop through WMIC's output, which is accomplished by calling :SETVAR for each line of WMIC output (skipping the first one, though, and testing for any blank trailing lines. A variable is created for each enabled NIC found - the variables will be %NIC1%, %NIC2%, etc.

Line 12, starting with "echo NIC", can be removed once you have it working to your satisfaction.

share|improve this answer

You know, to make this REALLLLLY useful, I added /node:%1 (to get IP of remote computer) Saves some legwork for system admins & network admins vs. ping or nslookup. I'm sure the other thing I will add is 'error-checking' so that, IF the system happens to not be available, the script can "log that info" - like System %1 not available - and then move on to grab the ip of the next system.

Often, I make a 'bat' to 'call a bat' So, I would just create a batch file maybe like "getall.bat" below:

Then just run it like this: getall > getall-log.txt 2>&1

(Above will pipe out all the info to a text file, including any errors encountered)

getall.bat

rem getall.bat
call getnic.bat workstation1
call getnic.bat workstation2
call getnic.bat server1
call getnic.bat server2
rem end of getall.bat

getnic.bat

rem - getnic.bat
@echo off
setlocal
set varcounter=0
set wmicmd="wmic /node:%1 NICCONFIG WHERE IPEnabled=true GET IPAddress"
for /f "tokens=1 delims={, skip=1" %%a in ('%wmicmd%') do call :SETVAR %%a
endlocal
goto :eof

:SETVAR
set /a varcounter=%varcounter% + 1
if not {%1}=={} (
    echo NIC %varcounter% address is {%1}
    set NIC%varcounter%=%1
)
goto :eof
rem end of getnic.bat
share|improve this answer
    
Thanks 'jscott' for editing it. I was in the midst of editing it at same time as you. Much appreciated! –  Jeff Jun 1 '12 at 14:02

Converted for IPv6

@echo off setlocal set varcounter=0 set wmicmd="wmic NICCONFIG WHERE IPEnabled=true GET IPAddress" for /f "tokens=2 delims={, skip=1" %%a in ('%wmicmd%') do call :SETVAR %%a endlocal goto :eof

:SETVAR set /a varcounter=%varcounter% + 1 if not {%1}=={} ( echo NIC %varcounter% IPv6 address is {%1} set NIC%varcounter%=%1 ) goto :eof

share|improve this answer

I know you asked for a VBS or Batch style script but I thought I would add a PowerShell snippet as well if you wanted to try that route or for anyone else looking at this question...

[System.Net.Dns]::GetHostAddresses([System.Net.Dns]::GetHostName()) | foreach {write $_.IPAddressToString}

This will return all IP Addresses currently assigned to the machine.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.