批处理脚本显示无输出

时间:2014-04-09 05:30:39

标签: windows batch-file csv

参考我之前的问题和收到的回复(可以找到here),我正在寻求关于批处理脚本的帮助。根据收到的上述问题的答复,我创建了一个Batch脚本,如下所示:

@echo off

setlocal enabledelayedexpansion

ping -n 1 %1 | find "TTL=" > NUL
IF NOT ERRORLEVEL 1 (

FOR /F %%i IN ('wmic /node:%1 computersystem get Name') DO (SET A=%%i)

FOR /F %%i IN ('wmic /node:%1 computersystem get Domain') DO (SET B=%%i)

FOR /F %%i IN ('wmic /node:%1 computersystem get UserName') DO (SET C=%%i)

FOR /F %%i IN ('wmic /node:%1 computersystem get Manufacturer') DO (SET D=%%i)

FOR /F "delims=" %%i IN ('wmic /node:%1 computersystem get Model') DO (SET E=%%i)
FOR %%a in (%E%) DO (SET E=%%a)

FOR /F %%i IN ('wmic /node:%1 computersystem get SystemType') DO (SET F=%%i)

FOR /F %%i IN ('wmic /node:%1 bios get SerialNumber') DO (SET G=%%i)

FOR /F "delims=|" %%i IN ('wmic /node:%1 os get Name') DO (SET H=%%i)
FOR %%a in (%H%) DO (SET H=%%a)

FOR /F %%i IN ('wmic /node:%1 os get TotalVisibleMemorySize') DO (SET J=%%i)

FOR /F "delims=" %%i IN ('wmic /node:%1 cpu get Name') DO (SET K=%%i)
FOR %%a in (%K%) DO (SET K=%%a)


echo !A!,!B!,!C!,!D!,!E!,!F!,!G!,!H!,!J!,!K! >> output.csv

)

问题在于,当使用有效参数执行上述脚本时,它既不会显示任何错误消息,也不会在生成的CSV文件中显示任何数据。我无法理解脚本的错误。

4 个答案:

答案 0 :(得分:2)

1 - 从每次调用wmic

中检索尽可能多的信息

2 - 过滤wmic的输出。在这种情况下,我使用find来搜索包含指定节点的行。

3 - 如果可能,为避免必须删除该行末尾的附加CR,请检索您不会阅读的附加字段。

4 - wmic的输出以节点名称为前缀,字段按字母顺序排列。定义tokens对此进行说明。

@echo off

    setlocal enableextensions disabledelayedexpansion

    set "node=%~1"

    ping -n 1 %node% | find "TTL=" > NUL
    if errorlevel 1 goto endProcess

    for /f "tokens=2-7 delims=," %%a in (
        'wmic /node:"%node%" computersystem get domain^,manufacturer^,model^,name^,systemtype^,username^,wakeuptype /format:csv ^| find /i "%node%"'
    ) do (
        set "_domain=%%a"
        set "_manufacturer=%%b"
        set "_model=%%c"
        set "_name=%%d"
        set "_systemType=%%e"
        set "_userName=%%f"
    )

    for /f "tokens=2 delims=," %%a in (
        'wmic /node:"%node%" bios get serialNumber^,version /format:csv ^| find /i "%node%"'
    ) do (
        set "_serialNumber=%%a"
    )

    for /f "tokens=2-3 delims=," %%a in (
        'wmic /node:"%node%" os get description^,totalvisiblememorysize^,version /format:csv ^| find /i "%node%"'
    ) do (
        set "_osName=%%a"
        set "_memory=%%b"
    )

    for /f "tokens=2 delims=," %%a in (
        'wmic /node:"%node%" cpu get name^,version /format:csv ^| find /i "%node%"'
    ) do (
        set "_cpu=%%a"
    )

    echo %_name%,%_domain%,%_userName%,%_manufacturer%,%_model%,%_systemType%,%_serialNumber%,%_osName%,%_memory%,%_cpu%

:endProcess
    endlocal

答案 1 :(得分:0)

这是一个调试版本。你可以看到变量最终结束了。

@echo off

setlocal enabledelayedexpansion

ping -n 1 %1 | find "TTL=" > NUL
IF NOT ERRORLEVEL 1 (
echo running loop
FOR /F %%i IN ('wmic /node:%1 computersystem get Name') DO echo (set A=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get Domain') DO echo (set B=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get UserName') DO echo (set C=%%i)
FOR /F %%i IN ('wmic /node:%1 computersystem get Manufacturer') DO echo (set D=%%i)

FOR /F "delims=" %%i IN ('wmic /node:%1 computersystem get Model') DO echo (set E=%%i)
FOR %%a in (!E!) DO echo (set E=%%a)

FOR /F %%i IN ('wmic /node:%1 computersystem get SystemType') DO echo (set F=%%i)
FOR /F %%i IN ('wmic /node:%1 bios get SerialNumber') DO echo (set G=%%i)

FOR /F "delims=|" %%i IN ('wmic /node:%1 os get Name') DO echo (set H=%%i)
FOR %%a in (!H!) DO echo (set H=%%a)

FOR /F %%i IN ('wmic /node:%1 os get TotalVisibleMemorySize') DO echo (set J=%%i)
FOR /F "delims=" %%i IN ('wmic /node:%1 cpu get Name') DO echo (set K=%%i)
FOR %%a in (!K!) DO echo (set K=%%a)

echo !A!,!B!,!C!,!D!,!E!,!F!,!G!,!H!,!J!,!K! >> output.csv

)
pause

答案 2 :(得分:0)

FOR行重建为以下语法:

FOR /F "tokens=2 delims==" %%i IN ('wmic /node:%1 computersystem get Name /value') DO echo (set A=%%i)

(显然在输出满足时删除echo

答案 3 :(得分:0)

@echo off

setlocal enableextensions enabledelayedexpansion

for %%y in (73,74) do (
for /L %%z in (1,1,254) do (

set "node=172.22.%%y.%%z"

ping -n 1 %node% | find "TTL=" > NUL
if not errorlevel 1 (

for /f "tokens=2-7 delims=," %%a in (
    'wmic /node:"%node%" computersystem get domain^,manufacturer^,model^,name^,systemtype^,username^,wakeuptype /format:csv ^| find /i "%node%"'
) do (
    set "_domain=%%a"
    set "_manufacturer=%%b"
    set "_model=%%c"
    set "_name=%%d"
    set "_systemType=%%e"
    set "_userName=%%f"
)

for /f "tokens=2 delims=," %%a in (
    'wmic /node:"%node%" bios get serialNumber^,version /format:csv ^| find /i "%node%"'
) do (
    set "_serialNumber=%%a"
)

for /f "tokens=2-3 delims=," %%a in (
    'wmic /node:"%node%" os get description^,totalvisiblememorysize^,version /format:csv ^| find /i "%node%"'
) do (
    set "_osName=%%a"
    set "_memory=%%b"
)

for /f "tokens=2 delims=," %%a in (
    'wmic /node:"%node%" cpu get name^,version /format:csv ^| find /i "%node%"'
) do (
    set "_cpu=%%a"
)

echo !_name!,!_domain!,!_userName!,!_manufacturer!,!_model!,!_systemType!,!_serialNumber!,!_osName!,!_memory!,!_cpu! >> output.csv
)))