Script is looping without an end

Need help with this script. I am trying to get the os, version and patches installed on a server. When i run the script the loop is not stopping and it seems to be running forever until i intervene and stop the script. Please help me in understanding where i am going wrong.

@echo off
setlocal

rem Define output file location
set outputfile=%computername%.csv

rem Overwrite output file and add header line
echo Caption,Version,Node,Description,Type,HotFixID,InstalledBy,InstalledOn>>%outputfile%

rem Gather all data desired
for /f "skip=2 tokens=2 delims=," %%a in ('wmic os get caption /format:csv') do (
  for /f "skip=2 tokens=2 delims=," %%b in ('wmic os get version /format:csv') do (
    for /f "skip=2 tokens=2 delims=," %%c in ('wmic qfe get csname /format:csv') do (
      for /f "skip=2 tokens=2 delims=," %%d in ('wmic qfe get caption /format:csv') do (
        for /f "skip=2 tokens=2 delims=," %%e in ('wmic qfe get description /format:csv') do (
          for /f "skip=2 tokens=2 delims=," %%f in ('wmic qfe get hotfixid /format:csv') do (
            for /f "skip=2 tokens=2 delims=," %%g in ('wmic qfe get installedby /format:csv') do (
              for /f "skip=2 tokens=2 delims=," %%h in ('wmic qfe get installedon /format:csv') do (
              rem Write merged results to output file
                  echo "%%~a","%%~b","%%~c","%%~d","%%~e","%%~f","%%~g","%%~h">>%outputfile%
            )
          )
        )
      )
    )
   )
 )
)
Raj ShivaramSenior ArchitectAsked:
Who is Participating?
 
Bill PrewCommented:
Give this a try, should work better...

You want to gather all the fields you want from each class on a single WMIC execution, not do a separate WMIC for each field.  So you only needed two WMIC executions.

Also, when doing CSV output, the Node will always be added as the first column, and then the rest of the columns will appear in sorted name order.  So you need to account for that, I always place them on the WMIC execution in that order just to keep it straight.

Also, WMIC adds a trailing carriage return that often messes up usage of the last column, so if possible I add the last available column to the list even though I don't want that value.  It resolves the format of the rest of the fields.  

@echo off
setlocal

rem Define output file location
set outputfile=%computername%.csv

rem Overwrite output file and add header line
echo Caption,Version,Node,Description,Type,HotFixID,InstalledBy,InstalledOn>>%outputfile%

rem Gather all data desired
for /f "skip=2 tokens=2-3 delims=," %%a in ('wmic os get caption^,version^,WindowsDirectory /format:csv') do (
  for /f "skip=2 tokens=2-7 delims=," %%c in ('wmic qfe get caption^,csname^,description^,hotfixid^,installedby^,installedon^,status /format:csv') do (
    rem Write merged results to output file
    echo "%%~a","%%~b","%%~d","%%~c","%%~e","%%~f","%%~g","%%~h">>%outputfile%
  )
)

Open in new window


»bp
1
 
Raj ShivaramSenior ArchitectAuthor Commented:
Thank you so much Bill for helping me fix the looping issue and also correcting the script. Also thanks for the tip for adding additional column, I will use it in future.
0
 
Bill PrewCommented:
Welcome, glad that was helpful.


»bp
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.