Search code examples
batch-filewmic

comma separated wmic commands


i made this script that uses wmic to get info from remote computer

set /p "user=write UserName="
set /p "password=write Password="


for /f "tokens=*" %%a in (ip.txt) do ( 

    wmic /node:%%a /user:%user% /password:%password% computersystem get Name /format:table
    wmic /node:%%a /user:%user% /password:%password% computersystem get Model /format:table
    wmic /node:%%a /user:%user% /password:%password% computersystem get totalphysicalmemory /format:table
    wmic /node:%%a /user:%user% /password:%password% cpu get Name /format:table
    wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name /format:table
    wmic /node:%%a /user:%user% /password:%password% os get Caption /format:table
    wmic /node:%%a /user:%user% /password:%password% csproduct get identifyingnumber /format:table
    wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenheight /format:table
    wmic /node:%%a /user:%user% /password:%password% desktopmonitor get screenwidth /format:table

  )>>a.csv

but the output is a bit weird it seperates with "enter" any idea how to comma separate?


Solution

  • You could try the following code snippet:

    @echo off
    setlocal EnableDelayedExpansion
    set /P "user=write UserName="
    set /P "password=write Password="
    > "a.csv" (
        for /F "usebackq tokens=*" %%a in ("ip.txt") do (
            set "lineString="
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem             get Name                /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem             get Model               /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% computersystem             get totalphysicalmemory /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% cpu                        get Name                /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% path Win32_VideoController get Name                /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% os                         get Caption             /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% csproduct                  get identifyingnumber   /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor             get screenheight        /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            for /F "tokens=1,* delims==" %%A in ('wmic /node:%%a /user:%user% /password:%password% desktopmonitor             get screenwidth         /value') do for /F "delims=" %%Z in ("%%B") do set "lineString=!lineString!,%%Z"
            if defined lineString echo(!lineString:~1!
        )
    )
    endlocal
    exit /B
    

    The wmic output is stored into a ,-separated string in the variable lineString. For capturing the output of a command a for /F loop is perfect. However, the wmic command produces Unicode output, so two nested for /F loops are needed to do a proper Unicode/ANSI conversion; a single loop would leave orphaned trailing carriage-return characters in the strings.

    The output format of wmic has been changed from /format:table to /value to get an output like this: Caption=Microsoft Windows 7 Enterprise, so the for /F loop can take the = as a delimiter and get the desired value.

    Besides all this, I also changed the following:

    In your code, you redirected the output of every single loop iteration separately. To avoid this, () have been put around the (outer-most) for /F loop, so the whole data is redirected once into a.csv. Hence the redirection operator has been changed from append-type >> to (over-)write-type >.

    Delayed expansion has been enabled which is required when modifying and reading a variable (lineString) within for loops or blocks of code in between ().