Output into a single line

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
geelsu
Posts: 3
Joined: 16 Feb 2015 09:20

Output into a single line

#1 Post by geelsu » 20 Oct 2016 07:43

Greetings all,
This is kind of a segue from a post I did earlier at http://www.dostips.com/forum/viewtopic.php?f=3&t=6266.

This time they want me to gather information on system RAM in order to determine which systems need to be beefed up. So off to the batch scripting I went knowing the wmic command could help me.

I have the script working fine, but the output is not on one line making it difficult to import into an Excel spreadsheet for easy sorting. I am hoping some of you more experienced batch experts have dealt with getting things on one line before. So here is the code:

Code: Select all

@echo off
SETLOCAL EnableDelayedExpansion
   SET output=C:\temp\DevNet_RAM.txt
   echo Output file will be in %output%
   pause
   
   REM Delete any old output file if it exist.
   IF EXIST "%output% DEL "%output%"
   
   REM Ping all the IP Addresses listed in the IP file.
   for /F %%G IN (C:\temp\IPList.txt) Do (
   Call :ping %%G
   )
   
   GOTO :EOF
   
   :ping
   REM Ping an IP to see if it responds
   ping -n 2 -w 50 %1

        REM If the IP responds, errorlevel will be zero ... start collecting the RAM information
        if "errorlevel%"=="0" (
echo Start ******************  >>"%output%"
        REM echo the IP, System Name, System type, and RAM info to the Output file.
        echo "%1" >>"%output%"
        nslookup %1 | find "Name:" >>"%output%"
        wmic /node:"%1" csproduct get name >>"%output%"
        ping -n 2 -w 50 %1 | find "Approximate round trip " && (
        wmic /node:"%1" memorychip get capacity, devicelocator >>"%output%"
        )
echo End ****************** >>"%output%"

echo. >>"%output%"
)


The output is thus like this:

Code: Select all

Start ********************
"10.1.15.30"
Name:      c5d5r1ff@noc.dev.net

Optiplex 9010

Capacity         DeviceLocator
4294967296     DIMM1
4294967296     DIMM2
End **********************

Start ********************
"10.1.15.32"
Name:      e4g2t2g1@noc.dev.net

Precision 5610

Capacity         DeviceLocator
2147483648     DIMM1_CPU1
2147483648     DIMM2_CPU1
2147483648     DIMM1_CPU2
2147483648     DIMM2_CPU2
End **********************


This output is acceptable in a text file and for reading it on the screen, but as said previously not very useful for import into Excel.

If the output was something like the following:

Code: Select all

10.1.15.30 | c5d5r1f.noc.dev.net | Optiplex 9010 | 4294967296 | DIMM1 | 4294967296 | DIMM2
10.1.15.31 | e4g2t2g@noc.dev.net | Precision 5610 | 2147483648 | DIMM1_CPU1 | 2147483648 | DIMM2_CPU1 | 2147483648 | DIMM1_CPU2 | 2147483648 | DIMM2_CPU2

Then we would really be cooking with fire.

I realize the coding may not be the way experienced scriptors would do it and I am sorry for its clumsiness. Feel free to "beautify" or rewrite it as you please. Believe me, I won't complain. I'm thinking that second ping is unnecessary since the errorlevel has proven the system is alive and reachable. Also adding any comments to explain what is happening would also be greatly appreciated.

Any ideas or help in getting the output for each IP to a single line would be beyond greatly appreciated.

Kind regards to all.

Squashman
Expert
Posts: 4465
Joined: 23 Dec 2011 13:59

Re: Output into a single line

#2 Post by Squashman » 20 Oct 2016 08:01

Use a FOR /F command to capture the output of the WMIC commands and assign it to an environmental variable. Then output the variables to your log file.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Output into a single line

#3 Post by foxidrive » 21 Oct 2016 09:40

I thought it'd be a piece of cake Sqaushman and went to write a few lines to play with, and then that stoopid wmic bug stepped in with it's non-ansi text and screwy line endings.

This seems to work in my limited testing

Code: Select all

@echo off
SET output=C:\temp\DevNet_RAM.txt
echo Output file will be in %output%
REM Delete any old output file if it exist.
IF EXIST "%output%" DEL "%output%"
REM Ping all the IP Addresses listed in the IP file.
for /F %%a IN (IPList.txt) Do (
     REM Ping an IP to see if it responds and call the routine
 ping -n 2 -w 50 %%a | find /i "TTL=" >nul && call :go %%a
)
GOTO :EOF

:go
 set pc=
 set name=
 set ram=
 REM echo the IP, System Name, System type, and RAM info to the Output file.
 for /f "tokens=2" %%a in (' nslookup %1 ^| find "Name:" ') do set "PC=%%a"
 for /f "delims=" %%a in  (' wmic /node:"%1" csproduct get name
 ') do for /f %%b in ("%%a") do set "NAME=%%b"
  setlocal enabledelayedexpansion
 for /f "delims=" %%a in ('
   wmic /node:"%1" memorychip get capacity^, devicelocator /format:csv
 ') do for /f "tokens=1,2,3 delims=," %%b in ("%%a") do if /i not "%%b"=="node" set "ram=!ram!|%%c|%%d"
 >>"%output%" echo %1^|%PC%^|%NAME%!ram!

Post Reply