1
\$\begingroup\$

This is my first time coding! My intent for this program was to be able to give my technicians a flash drive that will pull system info and email it to me. Currently everything is working, but I was hoping for suggestions or tips on what I can improve on or add to it. I'm sure there is a lot of messy code that I got from Googling and jamming in there to fit my needs.

Any notes or help is super appreciated!

:: *** Get System Info.bat *** Version 1.0 *** 2021-10-22 *** Author: Boogershut ***
:: This batch file uses command line and PowerShell functions to retrieve system information, place it into a text file, and email the file for documentation.
:: Text file format: "'Account Name' 'Folder Number' - System Info_YearMonthDay-HourMinuteSecond"

    @echo off
    title Retrieving System Information
    
:: Variables
    set Cur_YYYY=&Rem Current Year
    set Cur_MM=&Rem Current Month
    set Cur_DD=&Rem Current Day
    set Cur_HH=&Rem Current Hour
    set Cur_Min=&Rem Current Minute
    set Cur_SS=&Rem Current Second
    set Cur_Date_Time=&Rem Current Date and Time set as YearMonthDay-HourMinuteSecond
    set AccountName=&Rem Account Name - defined by user
    set FolderNumber=&Rem Folder Number (Panel ID) - defined by user
    set FileName=&Rem Final File Name
    set TotalMem=&Rem Total amount of RAM installed
    set AvailableMem=&Rem Total amount of unused RAM
    set UsedMem=&Rem Total amount of used RAM
    set MemUsage=&Rem RAM being used shown as a percent
    set Count=&Rem Temporary counter variable used in Delayed Expansion
    set Var=&Rem Temporary variable used in Delayed Expansion

:: Assigning Date and Time Variables
    set Cur_YYYY=%date:~10,4%
    set Cur_MM=%date:~4,2%
    set Cur_DD=%date:~7,2%
    set Cur_HH=%time:~0,2%
    if %Cur_HH% lss 10 (set Cur_HH=0%time:~1,1%)
    set Cur_Min=%time:~3,2%
    set Cur_SS=%time:~6,2%
    set Cur_Date_Time=%Cur_YYYY%%Cur_MM%%Cur_DD%-%Cur_HH%%Cur_Min%%Cur_SS%
    
:: Assigning User Input Variables
    :LoopAccountName
        set /p AccountName=Input Account Name: 
        (echo %AccountName% | findstr /i /c:"name" /c:"@" /c:"{" /c:"}" /c:"=" >nul) && ((echo Account Name cannot contain "name" or special characters "@, {}, =") && (goto LoopAccountName)) || (goto LoopFolderNumber)
    :LoopFolderNumber
        set /p FolderNumber=Input Folder Number (Panel ID): 
        (echo %FolderNumber% | findstr /i /c:"name" /c:"@" /c:"{" /c:"}" /c:"=" >nul) && ((echo Folder Number cannot contain "name" or special characters "@, {}, =") && (goto LoopFolderNumber)) || (goto LoopStop)
    :LoopStop
    set FileName=%AccountName% %FolderNumber% - System Info_%Cur_Date_Time%
    
:: Display to user
    echo.
    echo Please wait - Retrieving "%AccountName% %FolderNumber%" System Information
    echo.
    
:: Create text file in current directory and input label
    echo %FileName% >> "%FileName%.txt"

:: Add date and time to file
    echo. >> "%FileName%.txt"
    echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
    echo. >> "%FileName%.txt"
    echo Info retreived on: >> "%FileName%.txt"
    cmd /c "date /t && time /t" >> "%FileName%.txt"

:: Run script to add Public IP Address to file
    echo. >> "%FileName%.txt"
    echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
    echo. >> "%FileName%.txt"
    if exist ".\ps.ps1" (PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '.\ps.ps1'") else (
        echo Unable to get Public IP Address. Script file not found. >> "%FileName%.txt"
        goto SkipPS
    )
    :SkipPS

:: Add PC info to file
        echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
        echo. >> "%FileName%.txt"
        echo System Information: >> "%FileName%.txt"
        echo. >> "%FileName%.txt"
        echo Current User: >> "%FileName%.txt"
        cmd /c "whoami" >> "%FileName%.txt"
    :: Add Serial Number to file
        echo. >> "%FileName%.txt"
        echo Serial Number/Service Tag: >> "%FileName%.txt"
        setlocal EnableDelayedExpansion
        set Count=1
        for /F "tokens=* USEBACKQ" %%F in (`cmd /c "wmic bios get serialnumber"`) do (
            set Var!Count!=%%F
            set /a Count=!Count!+1
        )
        echo %var2% >> "%FileName%.txt"
        endlocal
    :: Add CPU load to file
        echo. >> "%FileName%.txt"
        echo CPU Load: >> "%FileName%.txt"
        setlocal EnableDelayedExpansion
        set Count=1
        for /F "tokens=* USEBACKQ" %%F in (`cmd /c "wmic cpu get loadpercentage"`) do (
            set Var!Count!=%%F
            set /a Count=!Count!+1
        )
        for /f "tokens=* delims= " %%A in ('echo %var2% ') do set var2=%%A
        set var2=%var2:~0,-1%
        echo %var2%%% >> "%FileName%.txt"
        endlocal
    :: Add Memory Usage to file
        setlocal EnableDelayedExpansion
        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
        set /a MemUsage=(%UsedMem%*100)/%TotalMem%
        echo Memory Usage: >> "%FileName%.txt"
        echo %MemUsage%%% >> "%FileName%.txt"
        endlocal
    :: Add CPU details to file
        echo. >> "%FileName%.txt"
        echo CPU Info: >> "%FileName%.txt"
        setlocal EnableDelayedExpansion
        set Count=1
        for /F "tokens=* USEBACKQ" %%F in (`cmd /c "wmic cpu get name, numberofcores, status"`) do (
            set Var!Count!=%%F
            set /a Count=!Count!+1
        )
        echo %var1% >> "%FileName%.txt"
        echo %var2% >> "%FileName%.txt"
        endlocal
    :: Add Video card info to file
        echo. >> "%FileName%.txt"
        echo Video Card: >> "%FileName%.txt"
        setlocal EnableDelayedExpansion
        set Count=1
        for /F "tokens=* USEBACKQ" %%F in (`cmd /c "wmic path win32_VideoController get AdapterRAM, DeviceID, DriverVersion, Name, Status"`) do (
            set Var!Count!=%%F
            set /a Count=!Count!+1
        )
        echo %var1% >> "%FileName%.txt"
        echo %var2% >> "%FileName%.txt"
        endlocal
    :: Add ram details to file
        echo. >> "%FileName%.txt"
        echo RAM Info: >> "%FileName%.txt"
        setlocal EnableDelayedExpansion
        set Count=1
        for /F "tokens=* USEBACKQ" %%F in (`cmd /c "wmic memorychip get devicelocator, manufacturer, partnumber, serialnumber, capacity, speed, memorytype, formfactor"`) do (
            set Var!Count!=%%F
            set /a Count=!Count!+1
            :LoopRam
                if "(%%F)"=="" (
                    goto LoopRam
                ) else (
                    echo(%%F >> "%FileName%.txt"
                )
        )
        endlocal
    :: Add systeminfo to file
        cmd /c "systeminfo" >> "%FileName%.txt"
        
:: Add local IP info to file
    echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
    echo. >> "%FileName%.txt"
    cmd /c "ipconfig /all" >> "%FileName%.txt"

:: Add Drive info to file
    echo. >> "%FileName%.txt"
    echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
    echo. >> "%FileName%.txt"
    echo Drive Information: >> "%FileName%.txt"
    cmd /c "powershell.exe -c "get-psdrive -psprovider filesystem"" >> "%FileName%.txt"

:: Run script to email file
    if exist ".\ps2.ps1" (PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '.\ps2.ps1'") else (
        echo __________________________________________________________________________________________________________________ >> "%FileName%.txt"
        echo. >> "%FileName%.txt"
        echo Unable to send email of text file. Script file not found. >> "%FileName%.txt"
        goto skipPS2
    )
    :skipPS2

:: End of program
    cls
    echo.
    echo.
    echo.
    echo System information has been retrieved. 
    echo Press any key to exit.
    pause >nul
# *** ps.ps1 *** Version 1.0 *** 2021-10-22 *** Author: Boogershut ***
# This script adds the Public IP to the text file created by 'Get System Info.bat'.

# Tests connection
    if (Test-Connection 216.146.43.70 -Count 1 -Quiet) {
        # Remove excess garbage from filename for proper formatting
        $filename = gci .\ | sort LastWriteTime | select -last 1 | select Name
        $filename = $filename -replace 'Name'
        $filename = $filename -replace '\@'
        $filename = $filename -replace '[{}]'
        $filename = $filename -replace '\='
        # Add html from Public IP webpage
        invoke-webrequest 'http://checkip.dyndns.com/' -UseBasicParsing | Add-Content $filename
        
        # Remove excess html garbage
        $Content = Get-Content $filename
        $Content.replace("<html><head><title>Current IP Check</title></head><body>Current","Public") | Set-Content $filename
        $Content = Get-Content $filename
        $Content.replace("</body></html>","") | Set-Content $filename
    }else {
        # Remove excess garbage from filename for proper formatting
        $filename = gci .\ | sort LastWriteTime | select -last 1 | select Name
        $filename = $filename -replace 'Name'
        $filename = $filename -replace '\@'
        $filename = $filename -replace '[{}]'
        $filename = $filename -replace '\='
        
        # Add text to file
        Add-Content $filename 'Unable to get Public IP Address. Was unable to ping webpage.'
    }
# *** ps2.ps1 *** Version 1.0 *** 2021-10-22 *** Author: Boogershut ***
# This script sends an email with the text file created by 'Get System Info.bat' attached.

# Tests connection
    if (Test-Connection 142.250.141.27 -Count 1 -Quiet) {
        # Remove excess garbage from filename for proper formatting
        $filename = gci .\ | sort LastWriteTime | select -last 1 | select Name
        $filename = $filename -replace 'Name'
        $filename = $filename -replace '\@'
        $filename = $filename -replace '[{}]'
        $filename = $filename -replace '\='
        $shortname = $filename.Substring(0,$filename.Length-4)
        
        # Send email
        Send-MailMessage -From 'NAME <EMAIL>' -To 'NAME <EMAIL>' -Subject $shortname -Body $shortname -Attachments $filename -Priority High -SmtpServer 'smtp.google.com'
    }else {
        # Remove excess garbage from filename for proper formatting
        $filename = gci .\ | sort LastWriteTime | select -last 1 | select Name
        $filename = $filename -replace 'Name'
        $filename = $filename -replace '\@'
        $filename = $filename -replace '[{}]'
        $filename = $filename -replace '\='
        
        # Add text to file
        Add-Content $filename '__________________________________________________________________________________________________________________'
        Add-Content $filename ' '
        Add-Content $filename 'Unable to send email. Was unable to ping SMTP Server.'
    }
\$\endgroup\$
12
  • 1
    \$\begingroup\$ is there a reason to NOT use the Get-ComputerInfo cmdlet? \$\endgroup\$
    – Lee_Dailey
    Commented Oct 22, 2021 at 20:59
  • \$\begingroup\$ @Lee_Dailey I like the formatting of systeminfo a bit better. Plus, I'm not sure if Get-ComputerInfo has all the same info I'm pulling from the other commands. \$\endgroup\$
    – Boogershut
    Commented Oct 22, 2021 at 21:27
  • \$\begingroup\$ i would try to stay in one type of scripting lingo instead of mixing .BAT and .ps1 stuff. however, if you can't ... then you can't. \$\endgroup\$
    – Lee_Dailey
    Commented Oct 23, 2021 at 0:08
  • \$\begingroup\$ @Lee_Dailey Thanks, I like that advice. This is my first time coding and I wasn't sure how to accomplish everything in one language. \$\endgroup\$
    – Boogershut
    Commented Oct 25, 2021 at 15:54
  • \$\begingroup\$ you are most welcome! [grin] ///// as an aside ... why are you using such a strange series of replacements on your file names? plus, why are you doing it twice? the else test makes sense ... but grabbing the newest file, stripping out unwanted stuff looks like the same process being done 2 times. \$\endgroup\$
    – Lee_Dailey
    Commented Oct 25, 2021 at 22:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.