executeCommandLine does not run batch file

As said, delete pause.
If that does not help it may be related to access right problem. Who is the user openhab is running under?
Maybe try to log in as this user or provide the user temporarily with admin rights.

Timeout in this context means that the command was not successfully executed/finished within the specified 5 seconds ( which work as timeout limit ).

According to c# - Is there any way to start a GUI application from a windows service on Windows 7? - Stack Overflow :

Windows 7 introduced what is called “Session 0 isolation” that in practice means that every service (except system services) run in a separate non-interactive session. For this reason you cannot directly create a GUI from within the service, except if you run in legacy mode by flagging the Interact With Destop option, which is not good if you plan to run your service for some years in the future.

As David Heffernan said, the best is to use a client-server architecture. WCF makes it easy to communicate with named pipes.

This page is a good starting point to read about Session 0 Isolation and this white paper is also very good.

So as long as openhab is running as a service you will not / never get a UI related dialog to be opened.
In case this is a must have - then do run openhab with your user’s privileges. This then may have other disadvantages.
What you need to do is to use non UI related programs/scripts. Test them with your user privileges and then test them and run them as openhab service user.

Thanks for the answers. When I remove the pause it is also not working. But what I found is this:

When double click on the batch file which is opening the powershell script (Win11) I get a new windows window, which says: “Would you like that this App makes changes on your device…”? Here I need to click yes or no.

Of course OH can not do this… On the other hand I do have admin rights but can not find any win-option to disable such messages when opening a batch file.

Is there a way to call instead of the batch file directly the powershell script from OH? Or would it be better to run an exe?

Or is there any windows option to prevent such messages?

You haven’t mentioned a powershell SCRIPT earlier. What does the script do ?
If that is the script that you run from within the batch file then of course that one also would ask for the dialog to be accepted/denied and will wait forever ( until the timeout occurs ).

Warning: But you give admin rights then to OH. In case something goes wrong e.g. by accident the wrong command is executed this command will be executed with admin rights.

To start powershell script the script interpreter needs to be called. The script will be argument as far as I understand.

do you mean there is a direct call I can use from OH to run the powershell script? If yes, do you know how to do this?

I thought I can only run batch files with the executeCommandLine and therefore I did not mention it. The batch file is a workaround to start the powershell script at the end. This script is opening some com-ports to control an external device.

`

would you please explain a bit more in detail what you exaclty would like to do ?
In case you would like send commands via serial COM interface and/or receive responses via serial COM ports there is Serial - Bindings | openHAB

No, I need to control 4 serial ports which enable/disable an input port of an attached amplifier. Therefore I wrote a small powershell script in order to enable/disable each amplifier input - that’s it.

To run and control those inputs I would like to run the batch file which calls at the end the powershell script.

Now as mentioned, I have the problem to run this powershellscript.

and why don“t you run your powershell script by this batch file?

as far as I understand he tried running it but as admin privileges are required the powershell script requests admin access confirmation ( changes to be done on computer; do you accept ).

Of course this will not be different between a batch file that start a powershell or a powershell started directly.

Yes, but you can create a link and add “run as admin” to the extended properties of the link. That should eliminate the prompt for elevated rights

:frowning:

  • List item

ell this is exactly what I am trying to do. The batch file calls the powershell script - but as said this does not work on my win11 pc. :frowning:

or ist it possible directly run (call) the powershell script instead of the batch file first?

As said, the powershell script opens (close) a serial port as well triggers the DTR line. Is there a plugin in OH which I can use instead of trying to run the powershell script? This would be perfect and maybe much easier with OH.

If you have problems in starting cmd.exe I doubt you will succeed with powershell.exe.
Did you already bypass execution policy? For testing purposes please do one simple command in your ps script (like write-output) and start it inside your test.bat:

powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "your.ps1“

Also place .bat and .ps1 in your openhab script directory and provide access right (or make openhab user the owner)

:frowning:
frowning: k - now I do have this batch file script:

powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\openhab\conf\scripts\test.ps1“

The powershell script is:

dir > d:\output.txt

Both the ps1 and batch file are in the same OH folder. Both files have full permission as the OH user has. When running now the batch file which calls the ps1 I do not see the ouput.txt file :frowning:

The log file writes

Failed to execute rule ‘5e0474553a' with status 'RUNNING'

Is this the content of your ps1 script? This is not a valid Powershell command.
As I wrote above do a simple output command
You have to do a test from command shell first to check if execution policy allows the script and what the expected outcome is.

you are right - my mistake. Now I corrected it and it looks like this:

OH rule:

var Response =
        executeCommandLine(Duration.ofSeconds(5),"C:\\openhab\\conf\\scripts\\Russ.bat")

Russ.bat

@echo off
PowerShell -WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\openhab\conf\scripts\test.ps1""' -Verb RunAs}"
ps1'"

test.ps1:

Get-ChildItem D:\*.* | Set-Content D:\output.txt

Now when I manually run the “Russ.bat” script I can see the output.txt created on the drive D: as expected (no windows popup window anymore). When I manually trigger the rule from OH I can see the rule is shortly “running” and no error, warning in the log are generated. The only thing I don’t see is the output.txt file :frowning:

Can someone test it or is the OH call wrong? This gives me really headache…

Do you output the content of the variable Response to the openhab.log file ?

I do not have OH running on Windows. I can‘t test.
But again, you have chosen a test setup where you rely on OH user having write access to D:
Why don‘t you use a bullet proof test script like I mentioned above?
And as Wolfgang said, write the response variable to oh log.

ok I am using the variable to oh log now and I double check that powershell is installed and working correctly again. The PATH to powershell is correct and I can run the ps1 script from the cmd line directly.

@Oliver2
when using your script with

C:\openhab\userdata>powershell -WindowStyle Hidden -ExecutionPolicy Bypass -File "C:\openhab\conf\scripts\test.ps1“ 

I get following log:

Response=The cmd "powershell" is incorrectly or could not be found.

When using this call:

@echo off
PowerShell -WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\\openhab\\conf\\scripts\\test.ps1""' -Verb RunAs}"
ps1'

I get

Response=The cmd "powershell" is incorrectly or could not be found. The cmd "ps1'"" is incorrectly or could not be found.

so both calls are pretty much the same. I do have both files (.bat and .ps1) in the same oh folder and all users (incl. OH) has full permission. What I also have to mention OH is running as a service on my Win11 server.