@echo off
cls
echo HDS logfile collector 2.1 by Tassilo Senger
echo.
echo -------------------------------------------------------
echo Please close all software before executing this script.
echo -------------------------------------------------------
echo.
echo CAUTION: do not execute the batch file "As Administrator". If you want to use admin rights, open an administrative console and execute the batch file from there.
echo.
echo.
echo Press "1" and confirm with Enter for collecting HDS logs. This is the default option.
echo Press "2" and confirm with Enter for collecting HDS logs with RTC360 compatibility analysis. Do only, if instructed.
echo Press "3" and confirm with Enter for collecting HDS logs with BLK USB connection related issues. Do only, if instructed.
echo Press "4" and confirm with Enter for collecting HDS logs with PinPoint related information.

REM -------------- Selection of option BEGIN --------------
set /p choice=
   if %choice% == 1 call :hdslogsonly exit /b 0
   if %choice% == 2 call :hdslogswithRTC360CAT exit /b 0
   if %choice% == 3 call :hdslogswithBLK exit /b 0
   if %choice% == 4 call :hdslogswithPinPoint exit /b 0
exit /b 0


:hdslogsonly
call :hdslogs
call :archive
exit /b 0

:hdslogswithRTC360CAT
call :hdslogs
call :RTC360CAT
call :archive
exit /b 0

:hdslogswithportscanning
call :hdslogs
call :portscanning
call :archive
exit /b 0

:hdslogswithBLK
call :hdslogs
call :tee_BLK
call :archive
exit /b 0

:hdslogswithPinPoint
call :hdslogs
call :PinPointLogs
echo Prepare PinPointLogs
call :preparePinPointLogs
call :archive
exit /b 0

REM -------------- Selection of option END --------------

REM -------------- Function implementation BEGIN --------------
:PinPointLogs
REM remove existing log files
rmdir /s /q ".\PinPointLOGS"

mkdir PinPointLOGS
md .\PinPointLOGS\PP
md .\PinPointLOGS\Registry
md .\PinPointLOGS\CLM
md .\PinPointLOGS\SYS

REM Copy Extended Log File
set "source=.\healthcheck\licensing.cfg"
set "target=C:\ProgramData\Leica Geosystems\PinPoint\licensing.cfg"

copy "%source%" "%target%" /Y
echo.
echo Log file collection in progress. Do not close the window.
echo.
echo.

REM PinPoint starten um Logs zu schreiben
if exist "C:\Program Files\Leica Geosystems\PinPoint\PinPoint.exe" (
	start "" "C:\Program Files\Leica Geosystems\PinPoint\PinPoint.exe"
	timeout /t 10 >nul
	taskkill /IM PinPoint.exe /F
) else (
	echo Pinpoint not installed
)
pause
echo.

REM collect PinPoint logs

copy /y "C:\ProgramData\Leica Geosystems\PinPoint\Logs\*.*" ".\PinPointLOGS\PP\*.*"
copy /y "C:\ProgramData\Leica Geosystems\PinPoint\*.*" ".\PinPointLOGS\PP\*.*"

REM Read Pinpoint registry entries
reg export "HKEY_CURRENT_USER\SOFTWARE\Scasa" .\PinPointLOGS\Registry\Registry_HKEYScasa.txt /y
echo.
echo Log file collection in progress. Do not close the window.

echo.
exit /b 0

:hdslogs
echo version 2.1 > logversion.log
echo In progress. Do not close the window.
md bin
del hdsoutput.rar
echo In progress. Do not close the window.
dxdiag /t .\bin\dxdiag_out64bit.txt

copy /y "C:\Program Files (x86)\Common Files\Leica Geosystems\License-Server\svr_lgs.lic" .\bin\
copy /y "C:\Program Files (x86)\Common Files\Leica Geosystems\License-Server\lgs.opt" .\bin\
md CLM-logs11.14
md CLM-logs11.15
copy /y "%userprofile%\AppData\Local\Temp\CLM_Package_*.*" ".\CLM-logs11.14\"
copy /y "%userprofile%\AppData\Local\Temp\CLM_Package_*.*" ".\CLM-logs11.15\"
xcopy "C:\Leica Geosystems\CLM\LogFiles\*.*" ".\CLM-logs11.14\*.*" /exclude:.\healthcheck\exclude.txt /y
xcopy "C:\Leica Geosystems\CLM\LogFiles\clmnext_log.db" ".\CLM-logs11.14\" /y
xcopy "C:\Leica Geosystems\CLM\LogFiles\*.*" ".\CLM-logs11.14\*.*" /y
xcopy "c:\Users\Public\Documents\CLM-LogFiles\*.*" ".\CLM-logs11.15\*.*" /exclude:.\healthcheck\exclude.txt /y
xcopy "c:\Users\Public\Documents\CLM-LogFiles\clmnext_log.db" ".\CLM-logs11.15\" /y

md CLM-option
copy /y "C:\ProgramData\Leica Geosystems\CLM\*.*" ".\CLM-option\*.*"

md JetStream
copy /y "C:\ProgramData\Leica Geosystems\Logs\*.*" ".\JetStream\*.*"
copy /y "C:\ProgramData\Leica Geosystems\JetStream\ServerDb.db" ".\JetStream\"
copy /y "C:\ProgramData\Leica Geosystems\JetStream\JetStreamConfig.ini" ".\JetStream\"

echo Register360
md Register360
copy /y "C:\ProgramData\Leica Geosystems\Cyclone REGISTER 360\ServerDb.db" "Register360"
copy /y "C:\ProgramData\Leica Geosystems\Cyclone REGISTER 360\Register360Config.ini" "Register360"
md Register360-BLK
copy /y "C:\ProgramData\Leica Geosystems\Cyclone REGISTER 360 (BLK Edition)\ServerDb.db" "Register360-BLK"
copy /y "C:\ProgramData\Leica Geosystems\Cyclone REGISTER 360 (BLK Edition)\Register360Config.ini" "Register360-BLK"

echo Cyclone Defect Report
md CycloneDefectReport
for /f "tokens=*" %%a in ('reg query "HKCU\Software\Leica Geosystems\Cyclone" /v LogFolder') do set CYLOG=%%a
set CYLOG=%CYLOG:LogFolder    REG_SZ    =%
copy /y "%CYLOG%\*.*" ".\CycloneDefectReport"

REM echo Infinity
REM md Infinity
REM copy /y "%userprofile%\AppData\Local\Leica Geosystems\Infinity\logs\*.*" "Infinity"

echo Software versions
del .\bin\version.log
del .\bin\version2.log

wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Common Files\\Macrovision Shared\\FlexNet Publisher\\FNPLicensingService.exe' get caption, version > .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Common Files\\Macrovision Shared\\FlexNet Publisher\\FNPLicensingService64.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Common Files\\Leica Geosystems\\License-Server\\LGS.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Common Files\\Leica Geosystems\\License-Server\\lmgrd.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Common Files\\Leica Geosystems\\License-Server\\FNPCommsSoap.dll' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Leica Geosystems\\CLM-Administration\\clmadmin.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Leica Geosystems\\CLM-Administration\\clmadminsvr.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Leica Geosystems\\CLM-Administration\\clmrdsvr.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files (x86)\\Leica Geosystems\\CLM\\CLM.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone\\Cyclone.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\JetStream\\JetStream.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\JetStreamAdmin\\JetStreamAdmin.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\JetStreamViewer\\JetStreamViewer.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone REGISTER 360\\Register360.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone REGISTER 360 (BLK Edition)\\Register360-BLK.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone 3DR\\3DR.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\BLK360 Data Manager\\Blk360DataManager.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone ENTERPRISE\\JetStreamSrv.exe' get caption, version >> .\bin\version.log
wmic Path CIM_DataFile where Name='C:\\Program Files\\Leica Geosystems\\Cyclone ENTERPRISE\\node.exe' get caption, version >> .\bin\version.log

powershell -NoProfile -ExecutionPolicy Bypass -File ".\healthcheck\version.ps1"

dir "C:\Program Files (x86)\Leica Geosystems\CLM\CLM.exe" > .\bin\lmver.log

REM Harddiskinfo
wmic logicaldisk list brief > .\bin\disks.log
powershell -NoProfile -ExecutionPolicy Bypass -File ".\healthcheck\disks.ps1"

del .\bin\queryserv.log

echo JetStreamService > .\bin\queryserv.log
sc query JetStreamService >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo FLEXnet License Server >> .\bin\queryserv.log
sc query "FLEXnet License Server" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo FlexNet Licensing Service >> .\bin\queryserv.log
sc query "FlexNet Licensing Service" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo FLEXnet Licensing Service 64 >> .\bin\queryserv.log
sc query "FLEXnet Licensing Service 64" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo CycloneLicenseServer >> .\bin\queryserv.log
sc query "CycloneLicenseServer" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo lgs-clmrdsvr >> .\bin\queryserv.log
sc query lgs-clmrdsvr >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo Leica HDS Server >> .\bin\queryserv.log
sc query "Leica HDS Server" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo cyclonepceservice.Service >> .\bin\queryserv.log
sc query cyclonepceservice.Service >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo Leica TruView Importer >> .\bin\queryserv.log
sc query "Leica TruView Importer" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo Leica TruView Server >> .\bin\queryserv.log
sc query "Leica TruView Server" >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo Redis >> .\bin\queryserv.log
sc query Redis >> .\bin\queryserv.log
echo. >> .\bin\queryserv.log
echo MongoDB >> .\bin\queryserv.log
sc query MongoDB >> .\bin\queryserv.log

del .\bin\lmgrd.log

tasklist /svc /fi "imagename eq lmgrd.exe" > .\bin\lmgrd.log
tasklist /svc | sort > .\bin\processes.log
sc query > .\bin\allservices.log

del .\bin\licservers.log

REM Check license servers
echo In progress. Do not cancel.
.\healthcheck\lmstat -a > .\bin\licservers.log

REM Read registry entries
md Registry
reg export HKCU\SOFTWARE\Leica .\Registry\Registry_HKCU_jetstream.txt /y
reg export "HKEY_CURRENT_USER\SOFTWARE\Leica Geosystems" .\Registry\Registry_HKCU_LeicaGeosystems.txt /y
reg export "HKLM\SOFTWARE\Leica Geosystems" .\Registry\Registry_HKLM_LeicaGeosystems.txt /y
reg export "HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Leica Geosystems" .\Registry\Registry_HKLM_WOW6432_LeicaGeosystems.txt /y
reg export "HKEY_CURRENT_USER\Software\Technodigit" .\Registry\Registry_HKCU_Software_Technodigit.txt /y


REM Eventviewer
wevtutil epl System .\bin\system.evtx /ow:true
wevtutil epl Application .\bin\application.evtx /ow:true

REM Licenses
.\healthcheck\serveractutil -view > .\bin\licenses.log

REM Hyperthreading
wmic CPU Get NumberOfCores,NumberOfLogicalProcessors /Format:List > .\bin\hyperthreading.log

REM TruView logs
rmdir /q /s TruViewLogs
md TruViewLogs
copy /y "C:\ProgramData\Leica Geosystems\TruView\data\log*" ".\TruViewLogs\"

REM CloudWorx logs
md CloudWorx
xcopy /y /e "C:\Users\%username%\AppData\Local\Leica Geosystems\CloudWorx" ".\CloudWorx\"


REM Flexera list
dir C:\ProgramData\FLEXnet /ogne /s > .\bin\flist.txt

REM Cyclone3DR
md Cyclone3DR
copy /y "C:\Users\%username%\AppData\Local\Temp\Cyclone 3DR\Cyclone 3DR.log" ".\Cyclone3DR"

REM Ports
netstat -na -p tcp > .\bin\tcpports.txt

REM CLM logs
rmdir /s /q .\send_for_investigation
call .\healthcheck\basic_health_check.bat

exit /b 0

:RTC360CAT
REM OpenCL Compatibility Analysis Tool
echo.
echo.
echo !!! CAUTION !!!
echo If user interaction is required DO NOT CLOSE THE WINDOW. Just press any key to continue.
echo.
echo.
.\healthcheck\CompatibilityAnalysisTool.exe
echo.
echo.
exit /b 0

:portscanning
REM Scans ports
echo.
echo.
color 17
echo ..................................................................
echo Doing port scanning. This can take some minutes. Do not interrupt.
echo ..................................................................
echo.
echo.
.\healthcheck\tcpvcon.exe -a -c > .\bin\TCPout.csv
echo Port scanning finished.
color 7
exit /b 0

:tee_BLK
REM BLK related logs
rmdir /q /s BLKlogs
mkdir BLKlogs
wmic path CIM_LogicalDevice where "Description like 'USB%%'" get /value > .\BLKlogs\usb.txt
wmic nic get AdapterType, Name, Installed, MACAddress, PowerManagementSupported, Speed > .\BLKlogs\net.txt
driverquery /V > .\BLKlogs\drivers.txt
ipconfig /all > .\BLKlogs\ipconfig.txt
systeminfo > .\BLKlogs\system.txt
dxdiag /t .\BLKlogs\dxdiag.txt
exit /b 0

REM -------------- Function implementation END --------------

REM -------------- PinPoint logs preparation BEGIN --------------
:preparePinPointLogs

copy /y bin\application.evtx .\PinPointLOGS\SYS
copy /y bin\system.evtx .\PinPointLOGS\SYS
copy /y .\bin\dxdiag_out64bit.txt .\PinPointLOGS\SYS
xcopy .\send_for_investigation\*.* .\PinPointLOGS\CLM\*.* /y /e
copy .\PinPointLOGS\CLM\clm_diagnosis.txt .\PinPointLOGS\CLM\clm_diagnosis.txt
REM System Info
REM exit /b 0
REM -------------- PinPoint logs preparation END --------------

:archive
REM Pack all informations
.\healthcheck\rar.exe a -xrar.exe -xFnpCommsSoap.dll -xlmstat.exe -xhdslogs.bat -xhdslogs.exe -xserveractutil.exe -xserveractutil_libFNP.dll -xscanner32 -xscanner64 -xbasic_health_check.bat -xlmutil.exe -xCompatibilityAnalysisTool.exe -xRTC360CompatibilityAnalysisTool.bat -xhealthcheck -r HDSOUTPUT.rar *.*

echo ......................................
echo Return HDSOUTPUT.rar to Leica support.
echo ......................................
pause
exit /b 0