In WINDOWS BATCH - Datei (.bat) Abfragen, ob Netzteil angeschlossen ist?

3 Antworten

Vom Fragesteller als hilfreich ausgezeichnet

Teste mal folgende BAT-Datei. Es wird eine Meldung ausgegeben, ob Akku- oder Netzbetrieb besteht.
Sollte ja kein Problem sein nach deinen Wünschen abzuändern, wenn es funktioniert.
Ich kanns bei mir im Moment nicht testen, weil mein Akku im Notebook total hinüber ist. Der geht gleich aus, wenn ich auf Akku testen will.

@echo off
WMIC Path Win32_Battery Get BatteryStatus>c:\temp\Akku.txt
::
set LineNo=2
set "line="
set /a LineNo-=1
for /f %%a in ('more/e +%LineNo% ^< c:\temp\Akku.txt') do (
if not defined line set "line=%%a"
)
::
echo Status (1.Akkubetrieb / 2.Netzbetrieb): %line%
if %line% == 2 (
echo Es besteht Netzbetrieb
)
::
if NOT %line% == 2 (
echo Es besteht Akkubetrieb
)
ZunamiMaster 
Fragesteller
 03.09.2017, 08:47

Danke! Das werde ich demnächst mal probieren. ;D

1
ZunamiMaster 
Fragesteller
 28.09.2017, 14:54

Hallo!

Entschuldigung für die späte Rückmeldung, aber ich habe die Datei perfektioniert und das ist die Endlösung:

https://1drv.ms/f/s!AmDckeoLYAgcj3PNp55mZ-LSoSDu

for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1

if %batteryStatus% EQU 1 goto battery
if %batteryStatus% EQU 2 goto power adapter

@echo off & title BatteryTest
set "Password=4099"

for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1
if %batteryStatus% EQU 1 goto battery
if %batteryStatus% EQU 2 goto power adapter

echo ERROR!
pause > nul & exit

:battery
echo msgbox"+++ BATTERY +++", vbInformation, "POWER ADAPTER / BATTERY - Check..." > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs
echo msgbox"Shutdown in 30 seconds... || SAVE DATA!", vbInformation, "Use PC only with POWER ADAPTER!" > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs

shutdown -s -t 30

set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo MS WINDOWS 10 (64 Bit) was shutted down because of an error in>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

:password
set /p "in=ABORT: "
set "Password=4099"

echo msgbox"Shutting down aborted...", vbInformation, "ABORTED" > %temp%\abortedMessage.vbs
echo msgbox"Password does not match!", vbInformation, "ERROR" > %temp%\passwordErrorMessage.vbs

if "%in%"=="%Password%" (shutdown -a && echo Shutting down aborted... && %Temp%\abortedMessage.vbs && erase %temp%\abortedMessage.vbs && goto abortShutdownProtocoll) else (echo ERROR: Password does not match! && %Temp%\passwordErrorMessage.vbs && erase %temp%\passwordErrorMessage.vbs && goto password && goto passwordErrorProtocoll)
exit

:abortShutdownProtocoll
set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo SHUTTING DOWN ABORTED! --- ERROR: [shutdownProtocoll.txt]>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''; "OPERATION CANCELED!">> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

:passwordErrorProtocoll
set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo USER TRIED TO ABORT SHUTTING DOWN! --- ERROR: [shutdownProtocoll.txt]>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''; "PASSWORD DOES NOT MATCH">> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

pause > nul
exit

:power adapter
echo msgbox"+++ POWER ADAPTER +++", vbInformation, "POWER ADAPTER / BATTERY - Check..." > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs

timeout /t 1
exit

Nochmals vielen Dank für deine Hilfe!

1

Nimm den Akku raus, dann können Sie den Laptop nur mit Netzteil nutzen :D

Zur eigentlichen Frage: Keine Ahnung. 

ZunamiMaster 
Fragesteller
 31.08.2017, 16:11

Ist ei Notebook - Akku entfernen ist eher schwer.

Aber DANKE trotzdem. :D

1

Hab erst mal den Code von merkurus n bissle verkürzt:

for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1
if %batteryStatus% EQU 1 goto akku
if %batteryStatus% EQU 2 goto netz

und dir dann für den Rest eine hoffentlich funktionierende Lösung gebastelt (ich geb zu des mit dem Passwort hab ich jetzt nicht so elegant gelöst, sollte für den Zweck aber reichen):

@echo off & title AkkuTest
set "Passwort=n6i74x"
for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1
if %batteryStatus% EQU 1 goto akku
if %batteryStatus% EQU 2 goto netz
echo Fehler.
pause >nul & exit

:akku
echo Bitte immer Netzteil benutzen!
echo Computer wird in 30 sekunden ausgeschalten.
shutdown -s -t 30
set /p "in=Um das herunterfahren zu stoppen geben sie das Passwort ein: "
if "%in%"=="%Passwort%" ( shutdown -a && echo Abgebrochen. ) else ( echo Passwort falsch )
pause >nul
exit

:netz
echo Super, Du hast ans Kabel gedacht ;)
timeout /t 3
exit

~Tim

ZunamiMaster 
Fragesteller
 08.09.2017, 21:16

Vielen Dank! Ich werde es auf jeden Fall morgen mal ausprobieren. Dann sage ich dir, ob es funktioniert hat. ;D DANKE! JAN

1
ZunamiMaster 
Fragesteller
 06.11.2017, 19:49

Hi!

Sorry, dass ich dir erst jetzt antworte, aber wie versprochen hier der perfektionierte QUELL-CODE:

for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1

if %batteryStatus% EQU 1 goto battery
if %batteryStatus% EQU 2 goto power adapter

@echo off & title BatteryTest
set "Password=4099"

for /f "delims=" %%A in ('WMIC Path Win32_Battery Get BatteryStatus') do (set /a "batteryStatus=%%A")>nul 2>&1
if %batteryStatus% EQU 1 goto battery
if %batteryStatus% EQU 2 goto power adapter

echo ERROR!
pause > nul & exit

:battery
echo msgbox"+++ BATTERY +++", vbInformation, "POWER ADAPTER / BATTERY - Check..." > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs
echo msgbox"Shutdown in 30 seconds... || SAVE DATA!", vbInformation, "Use PC only with POWER ADAPTER!" > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs

shutdown -s -t 30

set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo MS WINDOWS 10 (64 Bit) was shutted down because of an error in>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

:password
set /p "in=ABORT: "
set "Password=4099"

echo msgbox"Shutting down aborted...", vbInformation, "ABORTED" > %temp%\abortedMessage.vbs
echo msgbox"Password does not match!", vbInformation, "ERROR" > %temp%\passwordErrorMessage.vbs

if "%in%"=="%Password%" (shutdown -a && echo Shutting down aborted... && %Temp%\abortedMessage.vbs && erase %temp%\abortedMessage.vbs && goto abortShutdownProtocoll) else (echo ERROR: Password does not match! && %Temp%\passwordErrorMessage.vbs && erase %temp%\passwordErrorMessage.vbs && goto password && goto passwordErrorProtocoll)
exit

:abortShutdownProtocoll
set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo SHUTTING DOWN ABORTED! --- ERROR: [shutdownProtocoll.txt]>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''; "OPERATION CANCELED!">> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

:passwordErrorProtocoll
set shutdownProtocollFile = shutdownProtocoll.txt
if not exist %DATEI% (copy shutdownProtocoll.txt)
REM del shutdownProtocoll.txt
REM echo SYSTEM - REPORT>> shutdownProtocoll.txt
REM echo.>> shutdownProtocoll.txt
echo USER TRIED TO ABORT SHUTTING DOWN! --- ERROR: [shutdownProtocoll.txt]>> shutdownProtocoll.txt
echo 'C:\Users\Familie\Desktop\Programs\Stromversorgung.bat'>> shutdownProtocoll.txt
echo Program OutPut: ''BATTERY WAS USED!''; "PASSWORD DOES NOT MATCH">> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo %date% - %time% UHR>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
echo.>> shutdownProtocoll.txt
REM shutdownProtocoll.txt

pause > nul
exit

:power adapter
echo msgbox"+++ POWER ADAPTER +++", vbInformation, "POWER ADAPTER / BATTERY - Check..." > %temp%\msg.vbs
%Temp%\msg.vbs
erase %temp%\msg.vbs

timeout /t 1
exit
1
timlg07  06.11.2017, 22:05
@ZunamiMaster

so was langes wollt ich dann doch nicht schreiben ;) freut mich aber das mein Code darin auch vorkommt :D

0