In WINDOWS BATCH - Datei (.bat) Abfragen, ob Netzteil angeschlossen ist?
Hallo!
Ich bin Anfänger beim Programmieren von .BAT - Dateien. Deswegen meine Frage: Kann man es programmieren, dass die BATCH - Datei beim Ausführen zuerst im System nachsieht, ob ein Netzteil angeschlossen ist und, wenn NEIN, den PC herunterfährt?
Wenn möglich soll das Herunterfahren sogar noch 30 Sekunden lang durch Eingabe eines Passwortes gestoppt werden können - das wäre noch COOLER!
Wenn das mit einer .BAT - Datei ein Problem ist, würde auch ein .VBS - Skript gehen.
DANKE!
P.S.: Ich habe einen Laptop; sprich: Ohne Netzteil wir der Akku aktiviert. Falls ihr euch fragt, was die Frage soll. Ich möchte nämlich, dass ALLE Benutzer den PC NUR mit Netzteil verwenden dürfen.
3 Antworten
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 )
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!
Nimm den Akku raus, dann können Sie den Laptop nur mit Netzteil nutzen :D
Zur eigentlichen Frage: Keine Ahnung.
Ist ei Notebook - Akku entfernen ist eher schwer.
Aber DANKE trotzdem. :D
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
so was langes wollt ich dann doch nicht schreiben ;) freut mich aber das mein Code darin auch vorkommt :D
Vielen Dank! Ich werde es auf jeden Fall morgen mal ausprobieren. Dann sage ich dir, ob es funktioniert hat. ;D DANKE! JAN
Hi!
Sorry, dass ich dir erst jetzt antworte, aber wie versprochen hier der perfektionierte QUELL-CODE: