Script to audit hardware and software

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
BlinkyBlinky
Posts: 3
Joined: 21 Feb 2018 08:23

Script to audit hardware and software

#1 Post by BlinkyBlinky » 21 Feb 2018 08:44

Hello Dos Coders.

I am trying to create a batch file that will create a simple text report on a machine, that lists both hardware and software installed. I have attached the code that does the hardware report but there is a loop that I think might be coming from how I am trying to report on the freediskspace. If you run the below code it appears to run 3 times and I think this is due to the code trying to report on all local hard drives on my test machine.

Once this problem is fixed I need to add to the report a list of installed software I tried different methods and find a report on why different methods don't always capture all installed programs. The work around is a VBS file but I am not sure how to call the VBS file from the batch file. If someone has a better method then please let me know.

Thank you to all that can help


Code: Select all

@echo off & setlocal

if %os%==Windows_NT goto WINNT
goto NOCON

:WINNT
echo .Using a Windows NT based system


REM set variables
set system=
set manufacturer=
set model=
set serialnumber=
set osname=
set sp=
setlocal ENABLEDELAYEDEXPANSION
set totalMem=
set availableMem=
set usedMem=
Set IP=

echo Getting data [Computer: %computername%]
echo Please Wait....

REM Get Computer Name
FOR /F "tokens=2 delims='='" %%A in ('wmic OS Get csname /value') do SET system=%%A

REM Get Computer Manufacturer
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Manufacturer /value') do SET manufacturer=%%A

REM Get Computer Model
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

REM Get Computer Serial Number
FOR /F "tokens=2 delims='='" %%A in ('wmic Bios Get SerialNumber /value') do SET serialnumber=%%A

REM Get Computer OS
FOR /F "tokens=2 delims='='" %%A in ('wmic os get Name /value') do SET osname=%%A
FOR /F "tokens=1 delims='|'" %%A in ("%osname%") do SET osname=%%A

REM Get Computer OS SP
FOR /F "tokens=2 delims='='" %%A in ('wmic os get ServicePackMajorVersion /value') do SET sp=%%A

REM Get Memory
FOR /F "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)
set totalMem=%totalMem:,=%
set availableMem=%availableMem:,=%
set /a usedMem=totalMem-availableMem

REM Get IP Address
FOR /f "tokens=2 delims=:" %%a in ('ipconfig^|find "IPv4 Address"') do (
set IP=%%a
)

:REM Get driveInfo method from 'viewtopic.php?t=5555#p34130'
set "freeB=" & set "sizeB="
for /f "usebackq skip=1 tokens=1,2" %%X in (`
    wmic logicaldisk where "DeviceID='C:'" get freespace^,size
  `) do if not defined freeB (set "freeB=%%X" & set "sizeB=%%Y")
set/a freeMB = %freeB:~0,-6% & set/a sizeMB = %sizeB:~0,-6%
set/a freePCT = (100 * freeMB + sizeMB / 2) / sizeMB
call :mb2gib freeMB freeGiB & call :mb2gib sizeMB sizeGiB
goto :Report

:mb2gib
@rem double 1000/1024 mb->mib correction
set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128
@rem 1/1024 mib->gib conversion
set/a %2 = (%2 + 512) / 1024


echo Summary of report

:Report

echo --------------------------------------------
echo System Name: %system%
echo Report Run by: %userdomain%\%username%
echo System IP Address: %IP%
echo Manufacturer: %manufacturer%
echo Model: %model%
echo Serial Number: %serialnumber%
echo Operating System: %osname%
echo Total Memory: %totalMem%
echo Used  Memory: %usedMem%
echo Computer Processor: %processor_architecture%
echo Service Pack: %sp%
echo Drive C:  %freeGiB% GB free / %sizeGiB% GB size = %freePCT%%% available  (= %freeB% / %sizeB%)
echo --------------------------------------------

REM Generate Report file
SET file="%~dp0%computername%.txt"
echo -------------------------------------------- >> %file%
echo Details For: %system% >> %file%
echo Report Run by: %userdomain%\%username% >> %file%
echo IP Address: %IP% >> %file%
echo Manufacturer: %manufacturer% >> %file%
echo Model: %model% >> %file%
echo Serial Number: %serialnumber% >> %file%
echo Operating System: %osname% >> %file%
echo Total Memory: %totalMem% >> %file%
echo Used  Memory: %usedMem% >> %file%
echo Computer Processor: %processor_architecture% >> %file%
echo Service Pack: %sp% >> %file%
echo Drive C:  %freeGiB% GB free / %sizeGiB% GB size = %freePCT%%% available  (= %freeB% / %sizeB%) >> %file%
echo -------------------------------------------- >> %file%
echo wmic /output:C:\InstallList.txt product get name,version >> %file%

REM request user to push any key to continue
pause
goto END

:NOCON
echo Error...Invalid Operating System...
echo Error...No actions were made...
goto END

:END


VBS code for getting a list of installed programs from a windows based machine

Code: Select all

taken from stackoverflow.com/questions/673233/wmi-installed-query-different-from-add-remove-programs-list
Const HKLM = &H80000002
Set objReg = GetObject("winmgmts://" & "." & "/root/default:StdRegProv")
Set objFSO = CreateObject("Scripting.FileSystemObject")

outFile="programms.txt"

Set objFile = objFSO.CreateTextFile(outFile,True)
writeList "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\", objReg, objFile
writeList "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", objReg, objFile
objFile.Close 

Function writeList(strBaseKey, objReg, objFile) 
objReg.EnumKey HKLM, strBaseKey, arrSubKeys 
    For Each strSubKey In arrSubKeys
        intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "DisplayName", strValue)
        If intRet <> 0 Then
            intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "QuietDisplayName", strValue)
        End If
        objReg.GetStringValue HKLM, strBaseKey & strSubKey, "DisplayVersion", version
        objReg.GetStringValue HKLM, strBaseKey & strSubKey, "InstallDate", insDate 
        If (strValue <> "") and (intRet = 0) Then
            objFile.Write strValue & "," & version & "," & insDate & vbCrLf
        End If
    Next
End Function

BlinkyBlinky
Posts: 3
Joined: 21 Feb 2018 08:23

Re: Script to audit hardware and software

#2 Post by BlinkyBlinky » 22 Feb 2018 10:34

Updated VBscript code used to get software (This needs to be called from or embed in the original batch file) Once I have found what is causing the loop


Code: Select all



'constants
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Const HKLM = &H80000002

Set objReg = GetObject("winmgmts://" & "." & "/root/default:StdRegProv")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WshNetwork = WScript.CreateObject("WScript.Network")
strCompName = WshNetwork.Computername


'get computer name as file name
outFile=strCompName & ".txt"

'Open it forAppending and write found registry values

Set objFile = objFSO.OpenTextFile(outFile, ForAppending, True)
writeList "SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\", objReg, objFile
writeList "SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\", objReg, objFile

'clean up objects
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
set objReg = Nothing


'Function for grabing software and versions from the registry
Function writeList(strBaseKey, objReg, objFile) 
objReg.EnumKey HKLM, strBaseKey, arrSubKeys 
    For Each strSubKey In arrSubKeys
        intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "DisplayName", strValue)
        If intRet <> 0 Then
            intRet = objReg.GetStringValue(HKLM, strBaseKey & strSubKey, "QuietDisplayName", strValue)
        End If
        objReg.GetStringValue HKLM, strBaseKey & strSubKey, "DisplayVersion", version
        objReg.GetStringValue HKLM, strBaseKey & strSubKey, "InstallDate", insDate 
        If (strValue <> "") and (intRet = 0) Then
            objFile.Write strValue & "," & version & "," & insDate & vbCrLf
        End If
    Next
End Function



Hackoo
Posts: 75
Joined: 15 Apr 2014 17:59

Re: Script to audit hardware and software

#3 Post by Hackoo » 22 Feb 2018 14:23

You can add this command in one line with powershell :

Code: Select all

@echo off
Powershell /command "Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-List"
Pause
And to use it on your main batch file :

Code: Select all

@echo off & setlocal
Color 0A
Title Get Report on %computername%
if %os%==Windows_NT goto WINNT
goto NOCON

:WINNT
echo .Using a Windows NT based system
REM set variables
set system=
set manufacturer=
set model=
set serialnumber=
set osname=
set sp=
setlocal ENABLEDELAYEDEXPANSION
set totalMem=
set availableMem=
set usedMem=
Set IP=

echo Getting data [Computer: %computername%]
echo Please Wait....

REM Get Computer Name
FOR /F "tokens=2 delims='='" %%A in ('wmic OS Get csname /value') do SET system=%%A

REM Get Computer Manufacturer
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Manufacturer /value') do SET manufacturer=%%A

REM Get Computer Model
FOR /F "tokens=2 delims='='" %%A in ('wmic ComputerSystem Get Model /value') do SET model=%%A

REM Get Computer Serial Number
FOR /F "tokens=2 delims='='" %%A in ('wmic Bios Get SerialNumber /value') do SET serialnumber=%%A

REM Get Computer OS
FOR /F "tokens=2 delims='='" %%A in ('wmic os get Name /value') do SET osname=%%A
FOR /F "tokens=1 delims='|'" %%A in ("%osname%") do SET osname=%%A

REM Get Computer OS SP
FOR /F "tokens=2 delims='='" %%A in ('wmic os get ServicePackMajorVersion /value') do SET sp=%%A

REM Get Memory
FOR /F "tokens=4" %%a in ('systeminfo ^| findstr Physical') do if defined totalMem (set availableMem=%%a) else (set totalMem=%%a)
set totalMem=%totalMem:,=%
set availableMem=%availableMem:,=%
set /a usedMem=totalMem-availableMem

@for /f "delims=[] tokens=2" %%a in ('ping -4 -n 1 %ComputerName% ^| findstr [') do (
   set "LAN_IP=%%a"
)
 
for /f "tokens=2 delims=: " %%A in (
  'nslookup myip.opendns.com. resolver1.opendns.com 2^>NUL^|find "Address:"'
) Do set ExtIP=%%A
 
 
@For /f %%a in ('getmac /NH /FO Table') do  (
   @For /f %%b in ('echo %%a') do (
       If /I NOT "%%b"=="N/A" (
            Set "MY_MAC=%%b"
        )
    )
)

REM Get driveInfo method from 'viewtopic.php?t=5555#p34130'
set "freeB=" & set "sizeB="
for /f "usebackq skip=1 tokens=1,2" %%X in (`
    wmic logicaldisk where "DeviceID='C:'" get freespace^,size
  `) do if not defined freeB (set "freeB=%%X" & set "sizeB=%%Y")
set/a freeMB = %freeB:~0,-6% & set/a sizeMB = %sizeB:~0,-6%
set/a freePCT = (100 * freeMB + sizeMB / 2) / sizeMB
call :mb2gib freeMB freeGiB & call :mb2gib sizeMB sizeGiB
goto :Report
::******************************************************
:mb2gib
@rem double 1000/1024 mb->mib correction
set/a %2 = (125 * ((125 * %1 + 64) / 128) + 64) / 128
@rem 1/1024 mib->gib conversion
set/a %2 = (%2 + 512) / 1024
Exit /b
::******************************************************


echo Summary of report

:Report
echo --------------------------------------------------------------------------------------------
echo System Name             : %system%
echo Report Run by           : %userdomain%\%username%
echo My Private LAN IP       : %LAN_IP%
echo My External Public IP   : %ExtIP%
echo MAC Address             : %MY_MAC%
echo Manufacturer            : %manufacturer%
echo Model                   : %model%
echo Serial Number           : %serialnumber%
echo Operating System        : %osname%
echo Total Memory            : %totalMem%
echo Used  Memory            : %usedMem%
echo Computer Processor      : %processor_architecture%
echo Service Pack            : %sp%
echo Drive C:  %freeGiB% GB free / %sizeGiB% GB size = %freePCT%%% available  (= %freeB% / %sizeB%^)
echo --------------------------------------------------------------------------------------------

REM Generate Report file
SET file="%~dp0%computername%.txt"
(
echo --------------------------------------------------------------------------------------------
echo System Name             : %system%
echo Report Run by           : %userdomain%\%username%
echo My Private LAN IP       : %LAN_IP%
echo My External Public IP   : %ExtIP%
echo MAC Address             : %MY_MAC%
echo Manufacturer            : %manufacturer%
echo Model                   : %model%
echo Serial Number           : %serialnumber%
echo Operating System        : %osname%
echo Total Memory            : %totalMem%
echo Used  Memory            : %usedMem%
echo Computer Processor      : %processor_architecture%
echo Service Pack            : %sp%
echo Drive C:  %freeGiB% GB free / %sizeGiB% GB size = %freePCT%%% available  (= %freeB% / %sizeB%^)
)>%file% 

(
	echo --------------------------------------------
	echo           Installed Software  
	echo -------------------------------------------- 
	Powershell /command "Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | Format-List"
)>>%file%

REM request user to push any key to continue
pause & Start "" %file%
Exit

:NOCON
echo Error...Invalid Operating System...
echo Error...No actions were made...
goto END

:END
:wink: :lol:

BlinkyBlinky
Posts: 3
Joined: 21 Feb 2018 08:23

Re: Script to audit hardware and software

#4 Post by BlinkyBlinky » 23 Feb 2018 07:52

Thank you so much Hackoo for your fix as well as the script clean up.

The only bugs I have is that based on the stackoverflow reference is that we need to parse 2 registry key values


"SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\"
"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\"

As I don't think Windows\CurrentVersion\Uninstall holds all the software that is install on the machine, The only other bug I have is that some hotfixes are written to Windows\CurrentVersion\Uninstall and don't have a Display name or DisplayVersion this results in lots of empty software values is there a easy way to clean that up ?

Code: Select all

DisplayName    : 
DisplayVersion : 
Publisher      : 
InstallDate    : 

DisplayName    : 
DisplayVersion : 
Publisher      : 
InstallDate    : 

DisplayName    : 
DisplayVersion : 
Publisher      : 
InstallDate    : 

DisplayName    : Microsoft Visual C++ 2013 x86 Minimum Runtime - 12.0.21005
DisplayVersion : 12.0.21005
Publisher      : Microsoft Corporation
InstallDate    : 20170508

DisplayName    : 
DisplayVersion : 
Publisher      : 
InstallDate    : 
Thanks again for cleaning up the code

vijay
Posts: 1
Joined: 01 Nov 2019 10:57

Re: Script to audit hardware and software

#5 Post by vijay » 01 Nov 2019 11:02

Is there any way to upload the output file via ftp or Email.

Thanks

Post Reply