如果 mac 不在列表中,如何获取 pc mac 地址并重新启动 PC,.txt?,我只有这个获取 mac 命令,
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%
您可以使用
getmac
并通过findstr
将结果通过管道来筛选所需的网络适配器。- 将结果存储到变量
ThisPCMAC
- 将结果存储到变量
- 您可以使用
type
命令通过管道获取list.txt
文件的内容,findstr
以筛选ThisPCMAC
。- 将结果存储到变量
FoundMAC
中。
- 将结果存储到变量
- 如果定义了
FoundMAC
,则goto :norestart
- 如果未定义
FoundMAC
,则goto :restart
- 在
:restart
中,您可以使用所需的附加参数调用shutdown /r
- 如果弄错了,您可以在分配的时间内拨打
shutdown /a
电话(此处为 10 分钟,请参阅/t 600
(。 - 有关进一步帮助,请参阅
shutdown /?
这两个文件应位于同一目录中。list.txt
示例内容:
FF-AA-BB-CC-DD-FA FF-AA-BB-CC-DD-FB FF-AA-BB-CC-DD-FC
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
: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:testlist.txt: Nothing to do. C:testRestartIfThisPCMACnotInList.bat ended. Press any key to continue . . .
: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:testlist.txt: Restarting... shutdown /r /f /t 600 /d p:00:00 Cancel restart with the following command: shutdown /a C:testRestartIfThisPCMACnotInList.bat ended. Press any key to continue . . .