用于网络扫描的wmic批处理文件

时间:2016-02-03 18:08:41

标签: batch-file

我希望使用扫描网络的批处理文件来获取计算机名称,IP地址和版本(Windows 10)。

我有以下内容:

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)) 

我正在尝试将结果放在同一行中,并且可能使用/格式的CSV格式:

这对我不起作用,是因为变量在for循环之外吗?

编辑: 注意:在for循环中运行命令

(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

当我这样做时,我得到两行而不是一行。如果我可以将它作为每个IP地址的一行,那么它将非常有效:)

1 个答案:

答案 0 :(得分:1)

@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"