coco minion
coco minion

Reputation: 123

wmic batch file for network scan

I want to get the computer name, IP address and version (windows 10) by using a batch file that scans the network.

I have the following:

Set compInfo= "wmic /NODE:192.168.1.%%s OS GET Version,Caption"
Set ipAdd= "wmic /NODE:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET    
IPAddress,DNSHostName"

(FOR /L %%s IN (1,1,254) DO (echo %compInfo%,%ipAdd% >>    
C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt)) 

I'm trying to have the results in the same Line and possibly have it in CSV format with /Format:

It's not working for me, is it because the variables are outside the for loop?

EDIT: Note: run command inside for loop

(FOR /L %%s IN (1,1,254) DO wmic /node:192.168.1.%%s OS GET Version,Caption /format:htable & wmic /node:192.168.1.%%s NICCONFIG WHERE IPEnabled=true GET IPAddress,DNSHostName /format:htable) > results.html

When I do this I get the TWO lines as a result instead of ONE. if i can make this as one line for every ip address it would work great :)

Upvotes: 1

Views: 2146

Answers (1)

Stephan
Stephan

Reputation: 56154

@echo off
setlocal enabledelayedexpansion
(
  FOR /L %%s IN (1,1,254) DO (
    set "compInfo=N/A"
    set "IpAdd=N/A"
    for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s os get version^,caption /all^|find "."') do set compInfo=%%a
    for /f "delims=" %%a in ('wmic /NODE:192.168.1.%%s NICCONFIG WHERE "IPEnabled=true" GET IPAddress^,DNSHostName /all^|find "."') do set IpAdd=%%a
    echo !compInfo:~0,-1!,!IpAdd:~0,-1!
  )
)>"C:\Users\xxxx\Desktop\NsLooKup\TestEcho.txt"

Upvotes: 1

Related Questions