Superscript for Work

In an effort to minimize work for my fellow co-workers I wrote a script over two days to provide us with some functionality lost when we migrated from SCCM 2009 to 2013. Specifically we lost the much-needed ability to query the computers for information in order to make our work much easier. Information such as mac address, service tag, model number, etc. Since the upgrade, our Enterprise team was tasked with increasing the security for users of SCCM. Due to this we had been waiting several months for the permissions to be set so that we could access the tools.

Unsatisfied with this I wrote this rough script in order to solve that problem.

Some current features:

  1. nslookup
  2. connectivity ping check
  3. getmac
  4. obtain model and chassis info
  5. grab OU information

I’m currently adding more features, but of course just my luck, the day I release this script the Enterprise team decides to release access to the tool. Coincidence? I think not. Thankfully this tool is will still be useful for us especially for registering devices and hostnames, a feature I’ll be adding soon.

@Echo Off

:clear
CLS
:currentversion
SET toolversion=0.06
::as of 09.17.14 03:07pm
:versioncheck
IF EXIST q:\SuperTool\versioncheck\currentversion.cmd (
CALL q:\SuperTool\versioncheck\currentversion.cmd
) ELSE (
ECHO Cannot determine latest version. 
GOTO BEGIN
)

IF %currentversion% GTR %toolversion% (
ECHO You are using version %toolversion%. %currentversion% is now available.
ECHO Please copy over updated file at q:\SuperTool\
)
IF %currentversion% EQU %toolversion% (
ECHO This script is up to date.
)
IF %currentversion% LSS %toolversion% (
ECHO Something is wrong, this script version is newer than the latest version.
ECHO Maybe you are testing an unreleased version.
ECHO Let us know if this is not the case.
)
:begin
IF NOT EXIST k:\_SuperTool MD k:\_SuperTool
ECHO.
ECHO ======================================================
ECHO === NSLOOKUP/GETMAC/GETSERIAL COMBO TOOL V. %toolversion%   ===
ECHO ===                                                ===
ECHO ------------------------------------------------------
ECHO === Enter the hostname or IP address of the device ===      
ECHO === you want to obtain information from.           ===
ECHO ======================================================
:initialized

SET hostname=""
ECHO.
SET /p hostname=^<HAL^>: Enter hostname or IP address: 
IF %hostname%==todo GOTO todo
IF %hostname%=="" (
ECHO ^<HAL^>: This field cannot be left blank.
GOTO initialized
)
:: Generating File
ECHO ------------------------- > k:\_SuperTool\%hostname%.txt
ECHO %TIME% - %USERNAME% - INPUT: %HOSTNAME% >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
::checking to see if it's an IP or hostname
ECHO %hostname%| FINDSTR /b /e /r "[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*" >NUL
IF ERRORLEVEL 1 ( 
ECHO ^<HAL^>: %hostname% Appears to be a hostname.
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO HOSTNAME: %hostname% >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
SET isip=0
GOTO getip
)
ECHO.
ECHO ^<HAL^>: IP address identified.
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO IP ADDRESS: %hostname% >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
SET isip=1
GOTO getip
::GOTO ipvalidator

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ipvalidator
::I'll have to work on this later, it's not quite working the way I want it to yet.
SET "ipiswrong=1"
ECHO Validating IP
if not errorlevel 1 for /f "tokens=1-4 delims=." %%a in ("%hostname%") do (
if %%a GTR 0 if %%a LSS 255 if %%b LEQ 255 if %%c LEQ 255 if %%d GTR 0 if %%d LSS 255 SET "ipiswrong=0"
if %%a EQU 0 SET "ipiswrong=1"
)
IF ipiswrong EQU 0 GOTO getip
IF ipiswrong EQU 1 ECHO INVALID RESPONSE DAVID
::end ip validator
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:getip
ECHO.
ECHO =================
ECHO CHECKING NAME/IP
ECHO =================
::apparently nslookup doesn't include an exit status and always returns 1 so I have to do this workaround
ECHO.
nslookup %hostname% 2> %temp%\temp.txt
FINDSTR "Non-existent" %temp%\temp.txt
IF errorlevel 1 (
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO REGISTRATION INFORMATION: >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
nslookup %hostname% >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
:: This is for the registration information printout.
IF %isip% EQU 1 (
SET regip=%hostname%
)
GOTO getping
) ELSE (
ECHO.
ECHO ^<HAL^>: Device is not registered
IF %isip% EQU 1 (
ECHO ^<HAL^>: Using IP address, testing connection: 
SET regip=%hostname%
GOTO getping
)
IF %isip% EQU 0 (
ECHO ^<HAL^>: You used a hostname which is incorrect or not registered. Please restart.
GOTO begin
)
)
:getping
ECHO.
ECHO =====================
ECHO TESTING CONNECTIVITY
ECHO =====================
ping %hostname% | find "TTL=" > nul
IF ERRORLEVEL 1 (
 ECHO.
 ECHO ^<HAL^>: Device is not pingable. Bypassing available network tests:
 GOTO getou
) else ( 
ECHO.
ECHO ^<HAL^>: Host is reachable. Continuing operations.
GOTO getmac
)

:getmac
SETLOCAL ENABLEDELAYEDEXPANSION
SET "regmac="
SET "regmac1="
SET "regmac2="
SET "regmac3="

ECHO.
ECHO =================
ECHO CHECKING MAC
ECHO =================
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO MAC ADDRESS: >> k:\_SuperTool\%hostname%.txt
For /f "tokens=1" %%g in ('getmac /s %hostname% /nh') do (
For /f "usebackq tokens=1-6 delims=-" %%h in ('%%g') do (
::echo %%h%%i%%j%%k%%l%%m >> k:\_SuperTool\%hostname%.txt
set "regmac=!regmac!%%h%%i%%j%%k%%l%%m "
)
)
ECHO ^<HAL^>: Parsing ^"%regmac%^" into seperate MACs
For /f "usebackq tokens=1-3* delims= " %%n in ('%regmac%') do (
echo %%n >> k:\_SuperTool\%hostname%.txt
echo %%o >> k:\_SuperTool\%hostname%.txt
echo %%p >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
set "regmac1=!regmac1!%%n"
set "regmac2=!regmac2!%%o"
set "regmac3=!regmac3!%%p"
)
IF /I NOT "%regmac1%" == "" (
IF /I NOT "%regmac1%" == "Disabled" (
IF /I NOT "%regmac1%" == "N/A" (
ECHO ^<HAL^>: %regmac1%
)
)
)
IF /I NOT "%regmac2%" == "" (
IF /I NOT "%regmac2%" == "Disabled" (
IF /I NOT "%regmac2%" == "N/A" (
ECHO ^<HAL^>: %regmac2%
)
)
)
IF /I NOT "%regmac3%" == "" (
IF /I NOT "%regmac3%" == "Disabled" (
IF /I NOT "%regmac3%" == "N/A" (
ECHO ^<HAL^>: %regmac3%
)
)
)
::IF /I NOT "%regmac2%" == "" IF /I NOT "%regmac2%" == "Disabled" IF /I NOT "%regmac1%" == "N/A" ECHO ^<HAL^>: %regmac2%
::IF /I NOT "%regmac3%" == "" IF /I NOT "%regmac3%" == "Disabled" IF /I NOT "%regmac1%" == "N/A" ECHO ^<HAL^>: %regmac3%
ECHO.
ECHO ^<HAL^>: MAC address recorded.


:getname
ECHO.
ECHO =================
ECHO CHECKING NAME
ECHO =================
ECHO.

For /f "tokens=2 delims==" %%q in ('wmic /node:%hostname% computersystem get name /value') do (
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO HOSTNAME: >> k:\_SuperTool\%hostname%.txt
ECHO %%q >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
SET reghostname=%%q
)
:getserial
ECHO.
ECHO =================
ECHO CHECKING BIOS
ECHO =================
ECHO.
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO SERIAL NUMBER: >> k:\_SuperTool\%hostname%.txt
For /f "tokens=2 delims==" %%r in ('wmic /node:%hostname% bios get serialnumber /value') do (
ECHO %%r >> k:\_SuperTool\%hostname%.txt
)
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO ^<HAL^>: Obtained Serial Number
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO MODEL: >> k:\_SuperTool\%hostname%.txt
For /f "tokens=2 delims==" %%s in ('wmic /node:%hostname% computersystem get model /value') do (
ECHO %%s >> k:\_SuperTool\%hostname%.txt
)
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO ^<HAL^>: Obtained Model
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO CHASSIS: >> k:\_SuperTool\%hostname%.txt
For /f "tokens=2 delims==" %%t in ('wmic /node:%hostname% systemenclosure get chassistypes /value') do (
ECHO %%t >> k:\_SuperTool\%hostname%.txt
)
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO ^<HAL^>: Obtained Chassis type
ECHO -------------------------------------

:getou
if %isip% EQU 0 (
ECHO.
ECHO ======================
ECHO CHECKING AD MEMBERSHIP
ECHO ======================
ECHO.
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
ECHO ACTIVE DIRECTORY OU INFORMATION >> k:\_SuperTool\%hostname%.txt
dsquery computer -name %hostname% >> k:\_SuperTool\%hostname%.txt
ECHO ------------------------- >> k:\_SuperTool\%hostname%.txt
GOTO restartorexit
)
if %isip% EQU 1 (
ECHO.
ECHO ^<HAL^>: An IP Address was used so we cannot get AD information.
GOTO restartorexit
)


:restartorexit

ECHO.
ECHO ======================
ECHO OPERATIONS COMPLETE
ECHO ======================
:restartorexit2
ECHO.
ECHO ^<HAL^>: Would you like to (r)restart, (rc)restart and clear screen 
ECHO (p)Print Registration to email, or (q)quit?
set /p prompt="(r, rc, p, or quit)"
if %prompt%==r goto begin
if %prompt%==restart goto begin
if %prompt%==p goto generatereg
if %prompt%==rc goto clear
if %prompt%==quit goto quit
if %prompt%==q goto quit
if %prompt%==todo goto todo
goto restartorexit2

:generatereg
ECHO.
ECHO ========================
ECHO GENERATING REGISTRATION
ECHO ========================
start "" outlook /c ipm.note /m "hostmaster@domain.com&subject=Please Register this Device&body=host1@domain.com : Windows 7 : LOCATION : %regip% : %reghostname% : %regmac1% "
ECHO Generating Registration request
goto restartorexit2

:quit
ENDLOCAL
ECHO.
ECHO =================
ECHO ENDING PROGRAM
ECHO =================
pause
exit

:todo
CLS
ECHO.
ECHO =================================================
ECHO You have accessed the secret to do / feature list
ECHO.
ECHO CURRENT FEATURES
ECHO 1. Checks if you used an IP or hostname
ECHO 2. Verifies registration
ECHO 3. Runs a connectivity test to see if we should continue further
ECHO 4. Checks for model version
ECHO 5. Checks for chassis version
ECHO 6. Checks OU information if you used a hostname
ECHO.
ECHO -------------------------------------------------------------------
ECHO.
ECHO TODO

ECHO 1. create a settings file that this script will search for and read
ECHO it will check for "export options" such as file location or whether to do it at all
ECHO 2. Format the whole system nicer so it looks better [WIP]
ECHO 3. introduce hostname/ip error checking (partially implemented) [WIP]
Echo 4. if using an ip and registration is success, grab hostname to use for OU info.
ECHO END OF SECRET LIST
ECHO ==================================================
goto restartorexit

 

Agent[31]

Agent[31] works in the IT field for a living. He enjoys a wide variety of interests from music to photography to computer games. He wants to share the joy he finds with others.