How to use the exec binding with Windows 10

Let‘s take into consideration what Rich said, that Apps will stay running.
Could you change our batch file as follows?

dir c:\output.txt
start "" notepad.exe

The „start“ command gives control back to the batch file immediately.
Do not omit the two quotation marks.

Furthermore we might look into notepad being started as user „openhab“ while you are logged in as user say „reinhard“.

May I ask why you need to start an app anyway?

1 Like

I was away for the weekend.

The response of

dir >c:\output.txt
start " " notepad.exe

is:

C:\openHAB\userdata>dir  1>c:\output.txt 
C:\openHAB\userdata>start "" notepad.exe 

but notepad is not showing up

Actually I don’t want to start a program from OH. I want to stop and restart OH. I used to do this with winthing, which is not running anymore with JAVA 17. I am using IOTLink now, after I figured out how to get the rights (turn of UAC) to do it. But IOTLink is not supported any more and will not work sooner or later. So I thought about usind the exec binding or the executeCommandLine again, which used to work a long time ago when I tried it. But it could be that I used a PI at that time.

I checked for the user OH is running under. It was my user which has admin rights. I changed it to the Local System user which is allowed to exchange data with the desktop, without sucess. There is no openhab user under windows. It looks like the user could be the problem. Which one should the OH service use?

For a test I uninstalled OH as service and started OH through the start.bat. Sucess the test.bat can start notpad.
Running OH as a service and using the exec binding or the executeCommandLine seems to be the problem. Any solution for that?

Did you check with taskmanager if the program that you wanted to start is running ? It may just not disaplay something on your screen but is running in the background.

Sorry, I haven’t used OH on Windows yet.
You cannot do a restart by restarting the service from command line? Restarting requires probably elevated rights. But there are also workarounds for that.
I remember that you can elevate a batch file in its properties.

I found a part of a solution. Using the start command in a batch file, it is possible to start exe and bat files through executeCommandLine. But bat files run blind without a cmd window. It seams that OH doesn’t get a window.
Is there a possibility to start bat files through the executeCommandLine and get a CMD Window just like starting the bat file manually?

It is all in this thread

cmd /c test.bat

Actually, the OP’s last question is a bit trickier than it would appear.
In order to cast a window into a different session in windows you have to initiate the cmd window in that logged on users context basically a Run As for the user whom the GUI is currently assigned to. and then add the elevation to the bat file.
Or you can set up task scheduler to handle the session management for you and run a cmd to fire the task that is preconfigured. Think about how sudo works in linux. and systemctl and cron jobs.This behavior is all very similar.
This all seems a bit icky to me like the OP is trying to code a restart task around a different issue.
Also keep in mind if your openhab service instance is in a bad state it will likely get hung in a stopping condition that the only way you can recover cleanly is to restart the windows OS.
One final thought(suggestion) for the OP try and set all of this up in windows task schedular first and get it all working cleanly and correctly.
Then use the openhab Executecommandline to fire your windows tasks and manage the events. It is always better to let windows manage windows natively then to try and force in a 3rd party management unless you are using a precompiled windows executable that can correctly interact natively with the OS.
Like the previous programs you stated where in use in the past.

I am trying to follow your consideration, but I am wondering why the cmd is starting in a new session. Right now I am running OH as a program and not a service. I started OH with admin rights. So I tought it would be running under my user and my session.
IOTLink, triggered by MQTT is running as a service and starts the bat files just like manually.
Restarting the PC is allway an option but I am trying to unterstand how OH and windows work.

IOTlink is a precompiled windows executable. that is running as a service and capable of hooking into the natively OS using windows dot net 4.72 and C# methods. Thus, it allows interacting with the logged in user session.

Requirements

  • Windows 10 with administrator rights.
  • .NET Framework 4.7.2
  • Minimal understanding of YAML files.
  • Observations:
    • It might work with other Windows version, however it has been developed and tested only on Windows 10 (Update 1803+).
    • Please if you have it fully working on other Windows versions, inform it commenting on the following Issue: Working Windows Versions.

The java process you are running openhab under may be your windows user account but the actual openhab is running under whatever user name you configured when you set up open hab.
So when you defined your log on for openhab (say as an example you used admin as the user name) that is what is being passed out as the implied credentials to the executecommandline spawned process and since windows account management does not know who or what account SID “admin” is associated to it treats it as an unknown user.

I don‘t think so. The OP does not want to start a GUI based app for a user which is currently logged in. He wants to simply restart or stop openhab. You do not need „run as“ for this kind of commands. The OP just needs to add the commands for restarting openhab to the batch file.

no actually this was

his question I responded to.
So in order for him to get a cmd window on the gui session he has to establish the cmd window in the current user owning the gui for it to display. Thus run as.
But perhaps I misunderstood what he was asking.

This my understanding.
It is always good, if the requirements are defined precisely.

Agree wholeheartedly with you on getting precise requirements. OP changed his requirements with his last question.
That was why I started my response with

And obviously cmd.exe is a program of course, so run as in the context of the user owning the gui is needed to display it on the screen.

Execatly and how can I achive this?

google is your helper here. ie search something like “use windows runas in batch file with password”
this one gives you some insight on what to use and how to set it up

Also this is windows how to
C:\WINDOWS\system32>runas /?
but basically it is something like this
runas /noprofile /user:<your pc name here>\<user name you want to run as here> /savedcred <program you want here>
replace the angle brackets and the text within them with you correct info of course :upside_down_face:
so lets say you want to start a command window and run it under the windows user called Reinhard and the computer name you are running on is named ReinhardPC then the command would be
runas /noprofile /user:ReinhardPC\Reinhard /savedcred cmd.exe

The Reinhard windows account must have a password set it cannot be blank.
That should do what you want.
But again, it may be much easier to use tasks and also as @Oliver2 pointed out you do not need to have a visible command window to call the start and stop of openhab.

I have tried it with tasks, but

var response = executeCommandLine(Duration.ofSeconds(120),"C:\\Windows\\System32\\schtasks.exe /Run /U \"Reini\" /P \"xxxxx\" /TN \"Neustart\"") 

leads to:

2023-09-09 18:51:29.823 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Failed to execute commandLine '[C:\Windows\System32\schtasks.exe /Run /U "Reini" /P "xxxxxx" /TN "Neustart"]'

The runas command runs perfect in a cmd window but

var response = executeCommandLine(Duration.ofSeconds(120),"runas /user:bt3pro\\Reini /savedcred C:\\openHAB\\userdata\\bin\\neustart.bat")

leads to

2023-09-09 18:48:16.403 [WARN ] [rg.openhab.core.io.net.exec.ExecUtil] - Failed to execute commandLine '[runas /user:bt3pro\Reini /savedcred C:\openHAB\userdata\bin\neustart.bat]'

After trying and learning a lot it looks like there is a bug with Windows OH and executeCommandLine.

It is saying that it cannot find the command

It is interpreted to be one command or in other words it says it cannot find the file
runas /user:bt3pro\Reini /savedcred C:\openHAB\userdata\bin\neustart.bat
Reason is that arguments need to be split of by using commas.
Sorry to say, but this is documented behavior and does apply to Windows as well as to Linux.
On command line you don’t have that problem because on command line spaces are the separator between the different arguments.

please see here

Thanks to all!

var response = executeCommandLine("runas", "/user:bt3pro\\Reini", "/savedcred", "C:\\openHAB\\userdata\\bin\\neustart.bat")

does the job.

Putting the command in a bat file and starting it like this:

var response = executeCommandLine("C:\\openHAB\\test.bat")

works to.

If you want to get the output of the command back to your openhab script, you need to add a Duration (timeout) as the first argument.

1 Like