[R] - Script to Allow or Prevent Devices to Wake Up Computer

Discussion in 'Scripting' started by Mr.X, Oct 16, 2016.

  1. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,855
    13,398
    180
    Instead of clicking lots of mouse clicks I think is a better way doing it via script (powercfg command, I guess). A script able to:

    1. Enable or disable some devices to use their feature of waking up a computer.
    2. Device selective, ie, the ability for the user to choose which one(s) to enable or disable, e.gr. mouse and/or keyboards.
    3. Windows 7/8.x/10 compatible.
     
  2. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    104
    10
    #2 Compo, Oct 19, 2016
    Last edited by a moderator: Apr 20, 2017
    Perhaps something like this will do:
    Code:
    @ECHO OFF
    SET "i=0"
    ECHO=   STATE        PROGRAMMABLE DEVICE
    ECHO=
    FOR /F "DELIMS=" %%A IN ('PowerCfg -DEVICEQUERY wake_programmable') DO (
    SET/A i+=1
    CALL SET NL=%%NL%%%%i%%
    POWERCFG -DEVICEQUERY wake_armed|FIND "%%A">NUL 2>&1&&(SET "WS=Enabled ")||(
    SET "WS=Disabled")
    CALL SET WP[%%i%%]=%%WS%%     %%A
    CALL CALL ECHO=%%i%%. %%%%WP[%%i%%]%%%%)
    ECHO=
    IF %i% GTR 0 CHOICE /C %NL% /M "Please select a device to toggle "
    FOR /F "TOKENS=1*" %%A IN ('CALL ECHO %%WP[%ERRORLEVEL%]%%') DO (SET DEVICE=%%B
    IF "%%A"=="Disabled" (SET TO=En) ELSE (SET TO=Dis))
    ECHO=
    CHOICE /C YN /M "Would you like to toggle %DEVICE% to %TO%abled "
    IF ERRORLEVEL 2 EXIT/B
    POWERCFG -Device%TO%ableWake "%DEVICE%"
    TIMEOUT 5 1>NUL
    EXIT/B
     
  3. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,855
    13,398
    180
    #3 Mr.X, Oct 19, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    @Compo
    Many thanks. It works fine! :worthy:

    Now, it is working just fine on Windows 8.1 x64 EN but it fails on Windows 7 x64 ES


    Also I tried translating the following lines but no luck:

    Code:
    @ECHO OFF
    SET "i=0"
    ECHO=   ESTADO       DISPOSITIVO PROGRAMABLE
    ECHO=
    FOR /F "DELIMS=" %%A IN ('PowerCfg -DEVICEQUERY wake_programmable') DO (
        SET/A i+=1
        CALL SET NL=%%NL%%%%i%%
        POWERCFG -DEVICEQUERY wake_armed|FIND "%%A">NUL 2>&1&&(SET "WS=Habilitado ")||(
            SET "WS=Deshabilitado")
        CALL SET WP[%%i%%]=%%WS%% %%A
        CALL CALL ECHO=%%i%%. %%%%WP[%%i%%]%%%%)
    ECHO=
    IF %i% GTR 0 CHOICE /C %NL% /M "Por favor selecciona un dispositivo para conmutar "
    FOR /F "TOKENS=1*" %%A IN ('CALL ECHO %%WP[%ERRORLEVEL%]%%') DO (SET DEVICE=%%B
        IF "%%A"=="Deshabilitado" (SET TO=Ha) ELSE (SET TO=Desha))
    ECHO=
    CHOICE /C SN /M "Quieres conmutar %DEVICE% a %TO%bilitado "
    IF ERRORLEVEL 2 EXIT/B
    POWERCFG -Device%TO%ableWake "%DEVICE%" <--Have doubts on how to translate this one
    TIMEOUT 5 1>NUL
    EXIT/B
    
     
  4. Compo

    Compo MDL Member

    Mar 29, 2011
    137
    104
    10
    #4 Compo, Oct 19, 2016
    Last edited by a moderator: Apr 20, 2017
    Maybe an additional section:
    Code:
    @ECHO OFF
    SET "i=0"
    ECHO=   ESTADO            DISPOSITIVO PROGRAMABLE
    ECHO=
    FOR /F "DELIMS=" %%A IN ('PowerCfg -DEVICEQUERY wake_programmable') DO (
    SET/A i+=1
    CALL SET NL=%%NL%%%%i%%
    POWERCFG -DEVICEQUERY wake_armed|FIND "%%A">NUL 2>&1&&(
    SET "WS=Habilitado   ")||(SET "WS=Deshabilitado")
    CALL SET WP[%%i%%]=%%WS%% %%A
    CALL CALL ECHO=%%i%%. %%%%WP[%%i%%]%%%%)
    ECHO=
    IF %i% GTR 0 CHOICE /C %NL% /M "Por favor selecciona un dispositivo para conmutar "
    FOR /F "TOKENS=1*" %%A IN ('CALL ECHO %%WP[%ERRORLEVEL%]%%') DO (SET DEVICE=%%B
    IF "%%A"=="Deshabilitado" (SET "TO=En" & SET "TM=H") ELSE (
    SET "TO=Dis" & SET "TM=Desh"))
    ECHO=
    CHOICE /C SN /M "Quieres conmutar %DEVICE% a %TM%abilitado "
    IF ERRORLEVEL 2 EXIT/B
    POWERCFG -Device%TO%ableWake "%DEVICE%"
    TIMEOUT 5 1>NUL
    EXIT/B
     
  5. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,855
    13,398
    180
    Damn!
    I was trying on a vm. No Power Management there lol
    When I have some time to find a PC in Spanish I'll test.
    Meantime thank you very much.
     
  6. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    138
    15
    10
    #6 ramg1967, Oct 20, 2016
    Last edited: Oct 20, 2016
    How to translate the script to English???

    Is this correct

    @ECHO OFF
    SET "i=0"
    ECHO= STATE PROGRAMMABLE DEVICE
    ECHO=
    FOR /F "DELIMS=" %%A IN ('PowerCfg -DEVICEQUERY wake_programmable') DO (
    SET/A i+=1
    CALL SET NL=%%NL%%%%i%%
    POWERCFG -DEVICEQUERY wake_armed|FIND "%%A">NUL 2>&1&&(
    SET "WS=Enabled ")||(SET "WS=Disabled")
    CALL SET WP[%%i%%]=%%WS%% %%A
    CALL CALL ECHO=%%i%%. %%%%WP[%%i%%]%%%%)
    ECHO=
    IF %i% GTR 0 CHOICE /C %NL% /M "Please select device for switching"
    FOR /F "TOKENS=1*" %%A IN ('CALL ECHO %%WP[%ERRORLEVEL%]%%') DO (SET DEVICE=%%B
    IF "%%A"=="Disabled" (SET "TO=En" & SET "TM=H") ELSE (
    SET "TO=Dis" & SET "TM=Desh"))
    ECHO=
    CHOICE /C SN /M "You want to switch %DEVICE% a %TM%abilitado "
    IF ERRORLEVEL 2 EXIT/B
    POWERCFG -Device%TO%ableWake "%DEVICE%"
    TIMEOUT 5 1>NUL
    EXIT/B
     
  7. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,855
    13,398
    180

    The script Compo did on post #2 is working in English so I don't know why you ask.
     
  8. ramg1967

    ramg1967 MDL Member

    Sep 13, 2012
    138
    15
    10
    Sorry, I missed that post.
     
  9. Mr.X

    Mr.X MDL Guru

    Jul 14, 2013
    5,855
    13,398
    180
    #9 Mr.X, Nov 4, 2016
    Last edited by a moderator: Apr 20, 2017
    (OP)
    @Compo
    It did work on a lappy with Spanish lang.
    Thanks a lot.

    However I just did a slight adjustment to one line in order to display messages aligned, I just added a few bar spaces.

    Code:
    @ECHO OFF
    SET "i=0"
    ECHO=   ESTADO            DISPOSITIVO PROGRAMABLE
    ECHO=
    FOR /F "DELIMS=" %%A IN ('PowerCfg -DEVICEQUERY wake_programmable') DO (
    SET/A i+=1
    CALL SET NL=%%NL%%%%i%%
    POWERCFG -DEVICEQUERY wake_armed|FIND "%%A">NUL 2>&1&&(
    SET "WS=Habilitado   ")||(SET "WS=Deshabilitado")
    CALL SET WP[%%i%%]=%%WS%%     %%A
    CALL CALL ECHO=%%i%%. %%%%WP[%%i%%]%%%%)
    ECHO=
    IF %i% GTR 0 CHOICE /C %NL% /M "Por favor selecciona un dispositivo para conmutar "
    FOR /F "TOKENS=1*" %%A IN ('CALL ECHO %%WP[%ERRORLEVEL%]%%') DO (SET DEVICE=%%B
    IF "%%A"=="Deshabilitado" (SET "TO=En" & SET "TM=H") ELSE (
    SET "TO=Dis" & SET "TM=Desh"))
    ECHO=
    CHOICE /C SN /M "Quieres conmutar %DEVICE% a %TM%abilitado "
    IF ERRORLEVEL 2 EXIT/B
    POWERCFG -Device%TO%ableWake "%DEVICE%"
    TIMEOUT 5 1>NUL
    EXIT/B