How to READ the auto sleep time of pc from powercf

2019-09-24 02:35发布

问题:

I have a batch file that automatically sets the computer to sleep after 0 minutes (aka never sleep). Then I want to set it back to what it was before. My question is how would I set the current sleep time to a variable before I turn it off? Thanks in advance!

CLARIFICATION: I'm new to batch so feel free to point stuff out. Here's my current script:

@echo off
tasklist /v | find "Server" > nul

if errorlevel 1 (
    title Server
    set "time=[WHAT DO I PUT HERE?]" ::Gather current value for "put the computer to sleep:"
    C:
    cd C:windows\system32\powercfg.exe -change -standby-timeout-ac 0 ::Set the computer's sleep time to 0 (off)
    D:
    cd D:/Users/Elijah/Desktop/Tekkit_Server_v1.2.9g/ ::Do tasks
    java -Xmx12G -Xms10G -jar Tekkit.jar nogui
    cd C:windows\system32\powercfg.exe -change -standby-timeout-ac %time% ::When that's done, change the sleep setting back to what it was before we set it to 0
    title Server [STOPPED]
    pause
) else ( ::If bad stuff happens
    title Server [FAILED]
    echo A server is already running
    pause
)

I want to use this script for some automation with a Minecraft server. When I run a server from my computer, I have to change the "put the computer to sleep" setting to 0 minutes in control panel. I have already set this up in the script, but now my problem is turning auto-sleep back on and setting to what it was before. So to do that I want to store the value in a variable called time. How would I do that?

回答1:

A partial answer:

Try out powercfg -list. This will provide a list of the energy-schemes that are saved on your computer. Copy the ID of the mode you need (if there is more than one). Next you want to get another ID: powercfg -aliases You will get a list of GUID's and text behind them. Look for SUB_SLEEP and copy that ID as well.
Now running the command powercfg -q <1stID> <2ndID> will output more lists where you want to look for something like Sleep after. Below that there should be values like Possible Maximum Possible minimum and further below in that block values for the current setting.

This is why it is a partial answer... I have no idea how to parse the output. But I am sure that someone else will help you with that :)