Batch Script shows no output

前端 未结 4 1571
情深已故
情深已故 2021-01-28 14:08

With reference to my previous question and responses received (which can be found here), I am seeking a help regarding a Batch Script. Based on the responses received to above m

相关标签:
4条回答
  • 2021-01-28 14:51

    Here is a debugging build. You can see that the variables are ending up nul.

    @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
    
    0 讨论(0)
  • 2021-01-28 14:52
    @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
    )))
    
    0 讨论(0)
  • 2021-01-28 14:58

    Rebuild your FOR lines to the following syntax:

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

    (and obviously remove the echo when the output satisfys you)

    0 讨论(0)
  • 2021-01-28 15:09

    1 - Retrieve as much information you can from each call to wmic

    2 - Filter the output of wmic. In this case, i'm using find to search the line containing the indicated node.

    3 - If possible, to avoid having to remove the aditional CR at the end of the line, retrieve an aditional field that you will not read.

    4 - Output from wmic is prefixed with the node name and fields ordered alphabetically. Define tokens accounting for this.

    @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
    
    0 讨论(0)
提交回复
热议问题