Remote Shutdown PC Using EXEC on Windows PC OpenHAB

@fals3illusion

Please read the following articles:

Important:

You have to add a registry-key.
You have to use the command “net use” first.
Username of the remote machine is: PC-Name\username or IP-Address\username
.

But you also first can try to add the user of the PC were you execute the command, in the policy of PC that should be shut down:

User = PC-Name\username or IP-Address\username

ALSO IMPORTANT: shutdown.exe has to be running with Administrator rights. See here:

Btw, this was only running with older Windows Versions: e.g. Win9x, WinXp.

This is on both PC’s running Windows 10 Pro

i have tried this

val String results1 = executeCommandLine("C:\\Users\\fals3illusion\\Desktop\\ComputerShutdown.bat", 5000)

and i get

[WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\Users\fals3illusion\Desktop\ComputerShutdown.bat" (in directory "."): CreateProcess error=2, The system cannot find the file specified)

I have tried the actual location of the file and get the same thing too

I have tried this with
Justin-Server\fals3illusion (Computer that sends the shutdown command)
and also Justin-Computer\fals3illusion (Computer that receives the shutdown command)


and i get an error that says
image

@fals3illusion

First of all you should check if your shutdown.exe command is running, by doing the following steps manually.

  1. Type on JUSTIN-COMPUTER regedit (in “Search” or “Run”) and press Enter.

  2. Either go to the following registry key: Computer\HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System or copy and paste the registry path to the address bar and press Enter.

  3. Right-click System folder.

  4. Select “New -> DWORD (32-bit) Value.”

  5. Enter the label: LocalAccountTokenFilterPolicy

  6. Double-click the value, and change it to “1.”

  7. Close and exit the registry.

  8. Reboot to confirm changes. (You can now shut down or restart your PC remotely.)

  9. After restart --> Next, click Search and type Command Prompt.

  10. Right-click and select “Run as Administrator.”

  1. At the Command Prompt type: net use \\JUSTIN-COMPUTER /User:JUSTIN-COMPUTER\fals3illusion your-password and press Enter.

  2. Type shutdown /r /m \\JUSTIN-COMPUTER /t 001 and press Enter.

If this then reboots your remote PC, then you can do further steps.

If all steps 1-12 are running manually, you now can create a batch file e.g. shutdown.bat with the following content:

net use \\JUSTIN-COMPUTER /User:JUSTIN-COMPUTER\fals3illusion your-password
c:\windows\system32\shutdown.exe /r /m \\JUSTIN-COMPUTER /t 001

.
place shutdown.bat into the following folder:

   C:\Openhab-Folder\conf\sripts\

To auto-run a batch file as administrator, you need to create a shortcut for it. To create a shortcut, right-click the shutdown.bat, and select “Create shortcut”. This will place the shortcut in the same folder. Rename it to shut-down.bat

Once the shortcut has been created right-click it, select Properties from the context menu. On the Shortcut tab, you will see an Advanced button at the bottom. Click it.

image

Last step:

your rule:

val String results1 = executeCommandLine("C:\\Openhab-folder\\conf\\scripts\\shut-down.bat", 5000)
1 Like

This is the error i am getting now once running the script.

C:\openHAB2\userdata>net use \\JUSTIN-COMPUTER /User:JUSTIN-COMPUTER\fals3illusion mypassword1234

'net' is not recognized as an internal or external command,
operable program or batch file.
C:\openHAB2\userdata>c:\windows\system32\shutdown.exe /r /m \\JUSTIN-COMPUTER /t 001
JUSTIN-COMPUTER: Access is denied.(5)

@fals3illusion

Try:

c:\windows\system32\net use \\JUSTIN-COMPUTER /User:JUSTIN-COMPUTER\fals3illusion mypassword1234
c:\windows\system32\shutdown.exe /r /m \\JUSTIN-COMPUTER /t 001

Yes, that did it!
Thank You so much for you help !

1 Like

Hello @Celaeno1, thank you so much for the clear instructions! I am attempting to replicate @fals3illusion’s success in remotely restarting the windows 8 laptop I am running OH on- I was successfully able to follow your instructions to the very last step, the executeCommandLine step, but I seem to be having a couple of problems. First, My OH folder is within the C:\Program Files folder of my computer, and the space appears to be at least part of my problem- I can’t figure out how to represent it correctly to the rule so that it can successfully locate the file shut-down.bat (I get the error ‘Cannot run program “C:\Program”’…‘The system cannot find the file specified’). The second problem is that even after I move shut-down.bat to the C:\ directory and point the rule there, I get the exact same error (‘The system cannot find the file specified’)!. Any idea what I am doing wrong here? Thanks so much in advance!

Here is the rule i’m attempting to implement, just like your example:
val String results1 executeCommandLine("C:\\Program Files\\OpenHAB2\\conf\\scripts\\shut-down.bat

Error when I attempt to use the original OH directory within C:\Program Files…

15:58:39.478 [WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program “C:\Program” (in directory “.”): CreateProcess error=2, The system cannot find the file specified)

Error when I attempt to use the directory C:…

16:01:27.900 [WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program “C:\shut-down.bat” (in directory “.”): CreateProcess error=2, The system cannot find the file specified)

@Marcusfacius

You also could use Progra~1 (or Progra~2) instead of "Program Files" in your rule, depending on your Windows version (32-bit or 64-bit):

val String results1 executeCommandLine("C:\\Progra~1\\OpenHAB2\\conf\\scripts\\shut-down.bat", 5000) 

Make sure that shut-down.bat is a shortcut to shutdown.bat.

P.S: If you also have a “Program Files (x86)” folder, then you have a 64-bit Windows version.
You can find out whether it is Progra~1 or Progra~2 by executing the following command: dir /X

19.08.2020  14:44    <DIR>          PROGRA~1     Program Files
21.08.2020  13:56    <DIR>          PROGRA~2     Program Files (x86)
1 Like

Aha! That was definitely part 1 of the problem- thank you. But alas, I still get the (new) error below. Strange, because the .bat file works when I run it from Windows manually…

21:56:37.428 [WARN ] [clipse.smarthome.io.net.exec.ExecUtil] - Execution failed (Exit value: -559038737. Caused by java.io.IOException: Cannot run program "C:\Progra~1\OpenHAB2\conf\scripts\shut-d~1.lnk" (in directory "."): CreateProcess error=193, %1 is not a valid Win32 application)

Hey there,

I wrote a tutorial for this a few months ago:

and it works like a charm :slightly_smiling_face:

1 Like

@Marcusfacius

I tried out myself (on Win10, 64-bit, build: 19041.508 ), but it is not running anymore to “execute” via a shortcut directly !! ??? !!

So please create another batch, called e.g.: execute.bat

Content of execute.bat:

C:\Progra~1\openHAB2\conf\scripts\shut-d~1.lnk

your rule:

val String results1 executeCommandLine("C:\\Progra~1\\OpenHAB2\\conf\\scripts\\execute.bat", 5000) 

.
I tried myself and it is running!

@Celaeno1,

You are a saint, for trying so hard to help me… but I must have a permissions issue or some other configuration error with the laptop I am using to run OH. I attempted the execute.bat change above and although there is now no error in my OH log when I trigger the rule, it does not successfully restart the target Windows laptop running OH. I think that maybe there must still be an issue with not being able to run the execute.bat file as an administrator, because when I launch it in windows explorer, I get a prompt “do you want to allow the following program to make changes to this computer?” - and if I click yes, it successfully restarts! I am at a loss.

@Felix_Schneider,

Thank you for the info! I think I did stumble upon your tutorial- I haven’t tried it yet because I have OH running on a Windows laptop, and to be honest I am not sure how to replicate the “sudo” command in Windows. I believe I have come across a few OH forums alleging that there is no Windows equivalent? I see your comment in step #3 stating “When using Windows as a server this commands should only be somewhat different”… but as a shameless self-taught noob I don’t know how to accomplish those changes myself.

1 Like

@Marcusfacius

Please disable UAC.

Controlpanel --> User Accounts --> Change User Account Control Settings

image

Also make sure that your shortcut still is running as Administrator. See also above.

image

@Celaeno1, Sadly, still no success. Must be some obscure permissions setting burried deep somewhere in the bowels of Windows. Starting to think I’ll just have to resign myself to restarting it manually :confused:

As far as I understand you do not any error or warning message in openhab logs any longer.
What about Windows system event logs on the host you run OH and on the host you would like to shutdown ?

@Wolfgang_S, yes that’s correct - no errors in the OH log since the changes that Alex suggested here:

So please create another batch, called e.g.: execute.bat
Content of execute.bat :

C:\Progra~1\openHAB2\conf\scripts\shut-d~1.lnk

your rule:

val String results1 executeCommandLine("C:\\Progra~1\\OpenHAB2\\conf\\scripts\\execute

To be honest I had never opened the windows logs before… but it seems that there are three entries with the first executeCommandLine attempt: “A logon was attempted using explicit credentials.” (Event ID 4648) followed by “Special privileges assigned to new logon.” (Event ID 4672 and specific privileges as in the screenshot below) and finally “An account was successfully logged on.” (Event ID 4624). With every subsequent executeCommandLine attempt there are slightly different entries… “An account was logged on.” (event ID 4624) followed by “An account was logged off.” (event ID 4634) followed by a repeat log on (event ID 4624) and log off (event ID 4634).

Here is the screenshot for the second event above regarding special privileges the first time I run the executeCommandLine rule: