We help IT Professionals succeed at work.

Batch File IP to variable for logging

I need to log make a log using a batch file(in this situation its better to use .bat then .vbs), the code is what I have so far, I used netsh because it seams to be a better fit for a mixed XP/Win7 environment.

I want the log to look like:

DELL-123456      Mon 06/14/2010 13:34:10.38       192.168.1.1

Thanks ahead of time, google really isn't turning up much for me on this one.
@Net use Q: "\\server\d$" /USER:

netsh interface ip show addresses | find "IP Address" | find /v "127.0.0.1" | find /v "0.0.0.0"

@echo %computername%	%date% %time% 	 %IP% >> Q:\Image_log.txt

@Net use Q: /delete /Y

Open in new window

Comment
Watch Question

Author

Commented:
This will be run during the image process then log to a server. so everything used must be native to XP / win7 (netsh)
Justin OwensITIL Problem Manager

Commented:
If it just HAS to be batch file, I would use the TYPE command with a double >> (which appends, opposed to a > which overwrites).  For example, see the code below...
Since you are doing this at logon anyway, is there a reason you don't want to do it in VBS?
Justin

@echo off
@Net use Q: "\\server\d$" /USER:
netsh interface ip show addresses | find "IP Address" | find /v "127.0.0.1" | find /v "0.0.0.0"
type %computername%	%date% %time% 	 %IP% >> Q:\Image_log.txt
@Net use Q: /delete /Y

Open in new window

Author

Commented:
Its not a logon script. %IP% is not a real variable.
Commented:
You could do it like this.


@echo off
setlocal

set ipcmd=netsh interface ip show addresses
set include=findstr /c:"IP Address"
set exclude=findstr /v /c:"127.0.0.1" /c:"0.0.0.0"
set log=\\server\d$\Image_log.txt

for /f "tokens=3" %%G in ('%ipcmd% ^| %include% ^| %exclude%') do set ip=%%G
echo %computername%	%date% %time% 	 %IP% > "%log%"

Open in new window

Justin OwensITIL Problem Manager

Commented:
I like Shift-3's code, but would use a >> (append) instead of a > (overwrite) in line 10.  You could also use TYPE rather than ECHO.  In this case, they pretty much do the same thing.

Author

Commented:
Finished! Quick one, thanks!
@echo off
@Net use Q: "\\Server\d$" /USER:
setlocal

set ipcmd=netsh interface ip show addresses
set include=findstr /c:"IP Address"
set exclude=findstr /v /c:"127.0.0.1" /c:"0.0.0.0"
set log=Q:\Image_log.txt

for /f "tokens=3" %%G in ('%ipcmd% ^| %include% ^| %exclude%') do set ip=%%G
echo %computername%	%date% %time% 	 %IP% >> "%log%"

@Net use Q: /delete /Y

Open in new window