Change this Logon script to quer a txt file with machine names and do the same task.

bsharath
bsharath used Ask the Experts™
on
Hi,

Change this Logon script to quer a txt file with machine names and do the same task.
The script gets all softwares are there in the machines into seperate txt files each for each software name and the machine name into it.

Regards
sharath
@echo off
set ServerLoc=\\inm\Logs\Software_Wise
if not exist %ServerLoc% exit /b
if exist temp1.txt del temp1.txt
for /f "tokens=*" %%A in ('reg query "hklm\software\microsoft\windows\currentversion\uninstall" ^| find /i "currentversion\uninstall\"') do (
    reg query "%%A" | find /i "parentkeyname">NUL
    if errorlevel 1 (
        reg query "%%A" | find /i "systemcomponent" | find /i "0x1">NUL
        if errorlevel 1 (
            for /f "tokens=2,*" %%B in ('reg query "%%A" ^| find /i " displayname"') do (
                echo %%C>>temp1.txt
            )
        )
    )
)
sort temp1.txt>temp2.txt
set LastLine=
for /f "tokens=*" %%A in (temp2.txt) do (
  if not "%%A"=="!LastLine!" (
    if exist "%ServerLoc%\%%A.txt" (
      findstr /i /b /e "%COMPUTERNAME%" "%ServerLoc%\%%A.txt">NUL
      if ERRORLEVEL 1 (
        echo %COMPUTERNAME%>>"%ServerLoc%\%%A.txt"
      )
    ) else (
      echo %COMPUTERNAME%>>"%ServerLoc%\%%A.txt"
    )
    set LastLine=%%a
  )
)
if exist temp1.txt del temp1.txt
if exist temp2.txt del temp2.txt

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
As far as I understood you don't want this as a login script (anymore), instead you want to run it from a central site. It doesn't need much of a modification then:
@echo off
set ServerLoc=\\inm\Logs\Software_Wise
if not exist %ServerLoc% exit /b

for /F "usebackq tokens=* delims=" %%a in ("%~dp0machins.txt") do call QueryPC %%a
exit /b

:QueryPC
if exist temp1.txt del temp1.txt
for /f "tokens=*" %%A in ('reg query "\\%0\hklm\software\microsoft\windows\currentversion\uninstall" ^| find /i "currentversion\uninstall\"') do (
    reg query "%%A" | find /i "parentkeyname">NUL
    if errorlevel 1 (
        reg query "%%A" | find /i "systemcomponent" | find /i "0x1">NUL
        if errorlevel 1 (
            for /f "tokens=2,*" %%B in ('reg query "%%A" ^| find /i " displayname"') do (
                echo %%C>>temp1.txt
            )
        )
    )
)
sort temp1.txt>temp2.txt
set LastLine=
for /f "tokens=*" %%A in (temp2.txt) do (
  if not "%%A"=="!LastLine!" (
    if exist "%ServerLoc%\%%A.txt" (
      findstr /i /b /e /c:"%0" "%ServerLoc%\%%A.txt">NUL
      if ERRORLEVEL 1 (
        echo %0>>"%ServerLoc%\%%A.txt"
      )
    ) else (
      echo %0>>"%ServerLoc%\%%A.txt"
    )
    set LastLine=%%a
  )
)
if exist temp1.txt del temp1.txt
if exist temp2.txt del temp2.txt
exit /b

Open in new window

Author

Commented:
I want to run it from a server b quering a txt file that has the machine names and save the output to the UNC as before
The onl change would be quering the machine whose names are in a txt file
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
That is exactly what I showed above. The file machins.txt (sorry for the typo) needs to be located in the same folder as the batch file. The logs are written as before.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Author

Commented:
I get this

D:\>for /F "usebackq tokens=* delims=" %a in ("D:\machines.txt") do call QueryPC
 %a

D:\>call QueryPC dev306
'QueryPC' is not recognized as an internal or external command,operable program or batch file.

D:\>exit /b
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Oops, small typo. Line 5 needs to be:
for /F "usebackq tokens=* delims=" %%a in ("%~dp0machins.txt") do call :QueryPC %%a

Author

Commented:
i dont get any output


D:\>Software_Wise_Machines.bat

D:\>set ServerLoc=\\insm\Logs\Software_Wise

D:\>if not exist \\insm\Logs\Software_Wise exit /b

D:\>for /F "usebackq tokens=* delims=" %a in ("D:\machines.txt") do call :QueryP
C %a

D:\>call :QueryPC dev306

D:\>if exist temp1.txt del temp1.txt

D:\>for /F "tokens=*" %A in ('reg query "\\:QueryPC\hklm\software\microsoft\wind
ows\currentversion\uninstall" | find /i "currentversion\uninstall\"') do (
reg query "%A"   | find /i "parentkeyname" 1>NUL
 if errorlevel 1 (
reg query "%A"   | find /i "systemcomponent"   | find /i "0x1" 1>NUL
 if errorlevel 1 (for /F "tokens=2,*" %B in ('reg query "%A" | find /i " display
name"') do (echo %C 1>>temp1.txt ) )
)
)

Error:  The network path was not found.

D:\>sort temp1.txt 1>temp2.txt
temp1.txtThe system cannot find the file specified.

D:\>set LastLine=

D:\>for /F "tokens=*" %A in (temp2.txt) do (if not "%A" == "!LastLine!" (
if exist "\\insm\Logs\Software_Wise\%A.txt" (
findstr /i /b /e /c:":QueryPC" "\\insm\Logs\Software_Wise\%A.txt" 1>NUL
 if ERRORLEVEL 1 (echo :QueryPC 1>>"\\insm\Logs\Software_Wise\%A.txt" )
)  else (echo :QueryPC 1>>"\\insm\Logs\Software_Wise\%A.txt" )
 set LastLine=%a
) )

D:\>if exist temp1.txt del temp1.txt

D:\>if exist temp2.txt del temp2.txt
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
I have to apologize for that. Stupid me, I used the wrong variable (%0 instead of %1). Next try:
@echo off
set ServerLoc=\\inm\Logs\Software_Wise
if not exist %ServerLoc% exit /b

for /F "usebackq tokens=* delims=" %%a in ("%~dp0machins.txt") do call :QueryPC %%a
exit /b

:QueryPC
if exist temp1.txt del temp1.txt
for /f "tokens=*" %%A in ('reg query "\\%1\hklm\software\microsoft\windows\currentversion\uninstall" ^| find /i "currentversion\uninstall\"') do (
    reg query "%%A" | find /i "parentkeyname">NUL
    if errorlevel 1 (
        reg query "%%A" | find /i "systemcomponent" | find /i "0x1">NUL
        if errorlevel 1 (
            for /f "tokens=2,*" %%B in ('reg query "%%A" ^| find /i " displayname"') do (
                echo %%C>>temp1.txt
            )
        )
    )
)
sort temp1.txt>temp2.txt
set LastLine=
for /f "tokens=*" %%A in (temp2.txt) do (
  if not "%%A"=="!LastLine!" (
    if exist "%ServerLoc%\%%A.txt" (
      findstr /i /b /e /c:"%1" "%ServerLoc%\%%A.txt">NUL
      if ERRORLEVEL 1 (
        echo %1>>"%ServerLoc%\%%A.txt"
      )
    ) else (
      echo %1>>"%ServerLoc%\%%A.txt"
    )
    set LastLine=%%a
  )
)
if exist temp1.txt del temp1.txt
if exist temp2.txt del temp2.txt
exit /b

Open in new window

Author

Commented:
I get this


Error:  The system was unable to find the specified registry key or value

Error:  The system was unable to find the specified registry key or value

Error:  The system was unable to find the specified registry key or value
"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Commented:
Well, this one is tested ;-). I could not get it to work against W7 x64, but XP and Vista x86 are ok.
@echo off
set ServerLoc=\\inm\Logs\Software_Wise
if not exist %ServerLoc% exit /b

for /F "usebackq tokens=* delims=" %%a in ("%~dp0machins.txt") do call :QueryPC %%a
exit /b

:QueryPC
set pc=%1
if exist temp1.txt del temp1.txt
for /f "tokens=*" %%A in ('reg query "\\%pc%\hklm\software\microsoft\windows\currentversion\uninstall" ^| find /i 

"currentversion\uninstall\"') do (
@echo on
    reg query "\\%pc%\%%A" | find /i "parentkeyname">NUL
    if errorlevel 1 (
        reg query "\\%pc%\%%A" | find /i "systemcomponent" | find /i "0x1">NUL
        if errorlevel 1 (
            for /f "tokens=2,*" %%B in ('reg query "\\%pc%\%%A" ^| find /i " displayname"') do (
                echo %%C>>temp1.txt
            )
        )
    )
)
sort temp1.txt>temp2.txt
set LastLine=
for /f "tokens=*" %%A in (temp2.txt) do (
  if not "%%A"=="!LastLine!" (
    if exist "%ServerLoc%\%%A.txt" (
      findstr /i /b /e /c:"%pc%" "%ServerLoc%\%%A.txt">NUL
      if ERRORLEVEL 1 (
        echo %pc%>>"%ServerLoc%\%%A.txt"
      )
    ) else (
      echo %pc%>>"%ServerLoc%\%%A.txt"
    )
    set LastLine=%%a
  )
)
if exist temp1.txt del temp1.txt
if exist temp2.txt del temp2.txt
exit /b

Open in new window

Author

Commented:
Thanks a lot.
I have asked a related post please have a look
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

Commented:
Sorry, there are two (unimportant) changes you should do for cleanup. It is working that way, but it is echoing too much.
* Line 11 to 13 should be a single line
* Line 14 should be removed (the @echo on). It was used for test puproses only.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial