hass-agent / HASS.Agent

Unofficial development project for the HASS.Agent platform.
https://hass-agent.io
MIT License
314 stars 9 forks source link

Bug: HASS.Agent doesn't run my Powershell command #112

Closed ars4l4n closed 2 months ago

ars4l4n commented 3 months ago

Describe the bug I’m trying to run the following Powershell command via HASS.Agent: Invoke-Item "C:\Users\arsi\Desktop\Autohotkey Scripts\fertige\OBS Replaypuffer restarten.ahk" but said file doesn’t start.

To Reproduce Steps to reproduce the behavior:

HASS.Agent>Commands>New>Powershell Then enter said command into the "Command or Script" box or enter the path to the .ps1 script containing this line there: C:\Users\arsi\Desktop\test.ps1 Then click Store Command>Store and Activate Commands. Then run that action via a Home Assisstant script. Since there is no AHK hotkey in the taskbar, that means the script isn't running

Expected behavior OBS Replaypuffer restarten.ahk starting

Misc info (please complete the following information): winver_2024-07-03_17-32-20

Please check what's applicable (multiple answers possible):

Additional context Launching the .ahk via a regular Windows Powershell window does work.

Logs Logs after restarting HASS.Agent and running the Powershell Command as a quick action: https://pastebin.com/SA81RvxB I'm not seeing the name of the command in the log.

amadeo-alex commented 3 months ago

Hello, how are you invoking the command from Home Assistant's side?

ars4l4n commented 3 months ago

Hello, how are you invoking the command from Home Assistant's side?

Eg. via Developer Tools>Services>Button: Press and then choosing my Powershell command as an entity.

amadeo-alex commented 3 months ago

Hm, I'm running the bleeding edge test version and (although it should be same on 2.0.1):

I'm not sure if the Invoke-Item ever worked on it's own (although it's executed properly nothing happens on the system.

Another topic is the other command not working for you - are none of the commands working or? Because then it might be another issue.

ars4l4n commented 3 months ago

Invoke-Item "C:\Users\arsi\Desktop\test.ps1" was the only one out of the 3 that did something. However, it only opened the script in Notepad instead of running it. To fix this, I thought of setting Powershell as the default application to run .ps1 files within Windows. I don't think that would do anything though since rightclicking a .ps1 script and selecting open with>Powershell doesn't execute the command either. I also updated to 2.1.0.-beta2 and tried all previous workarounds again with no success.

Another workaround I tried was executing a CustomExecutorCommand: "C:\Users\arsi\Desktop\Autohotkey Scripts\fertige\OBS Replaypuffer restarten.ahk" with C:\Program Files\AutoHotkey\AutoHotkey.exe set as the Custom Executor Binary under Configuration. It worked at first but now the script doesn't start anymore for some reason.

What now?

By the way, Ignore when I said the other command didn't work. It's working again.

ars4l4n commented 2 months ago

@amadeo-alex Did you see I edited my comment?

amadeo-alex commented 2 months ago

Invoke-Item "C:\Users\arsi\Desktop\test.ps1" was the only one out of the 3 that did something. However, it only opened the script in Notepad instead of running it. To fix this, I thought of setting Powershell as the default application to run .ps1 files within Windows. I don't think that would do anything though since rightclicking a .ps1 script and selecting open with>Powershell doesn't execute the command either. I also updated to 2.1.0.-beta2 and tried all previous workarounds again with no success.

Another workaround I tried was executing a CustomExecutorCommand: "C:\Users\arsi\Desktop\Autohotkey Scripts\fertige\OBS Replaypuffer restarten.ahk" with C:\Program Files\AutoHotkey\AutoHotkey.exe set as the Custom Executor Binary under Configuration. It worked at first but now the script doesn't start anymore for some reason.

What now?

By the way, Ignore when I said the other command didn't work. It's working again.

If the first refers to the third bullet point from my message then it's not quite correct - "Invoke-item" needs to be only inside the ps1 file: VqYPyD

and the contents of the "playmp3.ps1" file: Invoke-Item 'X:\SomePath\The Prodigy - Spitfire.mp3'

Context: the "Invoke-Item" launches the default program for given file, for ps1 files it's notepad by default.

ars4l4n commented 2 months ago

I even have the issue of nothing happening upon triggering the command when putting invoke-item in my .ps1 file instead of inside the HASS.Agent Command field.

amadeo-alex commented 2 months ago

Does it work with a simpler example like mp3 file?

ars4l4n commented 2 months ago

No.

amadeo-alex commented 2 months ago

Then there is another issue present I think - if the following example that I posted doesn't work https://github.com/hass-agent/HASS.Agent/issues/112#issuecomment-2211832370

ars4l4n commented 2 months ago

What's our next troubleshooting step?

amadeo-alex commented 2 months ago

I'll be honest that I'm not sure - the most basic example with mp3 file should work. Could you please paste me the contents of "sensors.json" file? (it's in the config folder, open the log folder from HASS.Agent UI, navigate up one folder and then to "config")

ars4l4n commented 2 months ago
[
  {
    "Type": "LastActiveSensor",
    "Id": "d7159840-588f-4464-a41f-474ed0abcf3a",
    "Name": "ARS_COMPUTER_lastactive",
    "UpdateInterval": 300,
    "Query": "",
    "Scope": null,
    "WindowName": "",
    "Category": "",
    "Counter": "",
    "Instance": "",
    "EntityName": "ARS_COMPUTER_lastactive",
    "IgnoreAvailability": false,
    "ApplyRounding": false,
    "Round": 30,
    "AdvancedSettings": null
  },
  {
    "Type": "LastSystemStateChangeSensor",
    "Id": "d9239d36-30d5-4ce6-b817-34a0bbd1ae22",
    "Name": "ARS_COMPUTER_lastsystemstatechange",
    "UpdateInterval": 10,
    "Query": "",
    "Scope": null,
    "WindowName": "",
    "Category": "",
    "Counter": "",
    "Instance": "",
    "EntityName": "ARS_COMPUTER_lastsystemstatechange",
    "IgnoreAvailability": false,
    "ApplyRounding": false,
    "Round": null,
    "AdvancedSettings": null
  }
]
ars4l4n commented 2 months ago

I solved the issue. Running scripts was disabled on my system, as per default for Windows-Clients and I had to set an execution policy to enable it. I did that by running PowerShell as an administrator and using Set-ExecutionPolicy -ExecutionPolicy RemoteSigned there.

amadeo-alex commented 2 months ago

Thank you for the info! We should probably add it to the docs, @DrR0X-glitch what do you think?

DrR0X-glitch commented 2 months ago

Thank you for the info! We should probably add it to the docs, @DrR0X-glitch what do you think?

Yeah might do it in my next PR, although the first troubleshooting step we suggest in this case is if the script can be run from a normal terminal, which would've failed if execution policy is wrong.