Need Batch command getting mac address and compare on a txt file?

Viewed 2188

How to get pc mac address and restart PC if mac is not on the list.txt?, i only have this getting mac command,

for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv | findstr Ethernet"') do set MAC=%%a 
echo MAC address of this computer is %MAC%
1 Answers
  1. You use getmac and pipe the result through findstr to filter on the required network adaptor.
    • You store the result into a variable ThisPCMAC
  2. You use the type command to get the content of the list.txt file piped through findstr to filter on ThisPCMAC.
    • You store the result into a variable FoundMAC.
  3. If FoundMAC is defined you goto :norestart
  4. If FoundMAC is not defined you goto :restart
  5. In :restart, you call shutdown /r with the required additional params
  6. If mistaken, you can call shutdown /a in the allotted time (10 minutes here, see /t 600).
  7. For further help, see shutdown /?

The 2 files should be in the same directory. Example content of list.txt:

FF-AA-BB-CC-DD-FA
FF-AA-BB-CC-DD-FB
FF-AA-BB-CC-DD-FC

Content of RestartIfThisPCMACnotInList.bat:

@echo off

set ScriptPath=%~dp0
set ThisPCMAC=
set FoundMAC=

echo.
echo ScriptPath = %ScriptPath%


for /f "tokens=3 delims=," %%a in ('"getmac /v /fo csv | findstr Ethernet"') do set ThisPCMAC=%%a
echo.
echo MAC address of this computer is %ThisPCMAC%

for /F "usebackq delims==" %%b in (`"type %ScriptPath%list.txt | findstr %ThisPCMAC%"`) do set FoundMAC=%%b

if DEFINED FoundMAC (
   goto :norestart
) else (
   goto :restart
)


:norestart
echo.
echo Found %FoundMAC% in %ScriptPath%list.txt: Nothing to do.
goto :end


:restart
echo.
echo %ThisPCMAC% not found in %ScriptPath%list.txt: Restarting...
echo.
echo shutdown /r /f /t 600 /d p:00:00
shutdown /r /f /t 600 /d p:00:00
echo.
echo Cancel restart with the following command:
echo    shutdown /a
goto :end


:end
echo.
echo %~fp0 ended.
pause

Example output for :norestart:

C:\test\>RestartIfThisPCMACnotInList.bat

ScriptPath = C:\test\

MAC address of this computer is "FF-AA-BB-CC-DD-FA"

Found FF-AA-BB-CC-DD-FA in C:\test\list.txt: Nothing to do.

C:\test\RestartIfThisPCMACnotInList.bat ended.
Press any key to continue . . .

Example output for :restart:

C:\test\>RestartIfThisPCMACnotInList.bat

ScriptPath = C:\test\

MAC address of this computer is "FF-AA-BB-CC-DD-FD"

"FF-AA-BB-CC-DD-FD" not found in C:\test\list.txt: Restarting...

shutdown /r /f /t 600 /d p:00:00

Cancel restart with the following command:
   shutdown /a

C:\test\RestartIfThisPCMACnotInList.bat ended.
Press any key to continue . . .
Related