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}
link|improve this question

40% accept rate
feedback

3 Answers

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
link|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
feedback

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

link|improve this answer
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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