Code Monkey home page Code Monkey logo

Comments (7)

gfody avatar gfody commented on June 12, 2024

try cmd.exe /C at the beginning of your shell command, you may have to double escape it for /C

from onshutdown.

roylleh avatar roylleh commented on June 12, 2024

Trying new strategy like so: New-Service OnShutdown -bin 'OnShutdown 5000 "cmd /c tv_off.lnk"' -start Automatic

I created a shortcut of tv_off.cmd in %systemroot%\System32 and confirmed that running cmd /c tv_off.lnk by itself on the command prompt works (without admin).

Unfortunately, it seems the script still doesn't fire when I shutdown my PC. I checked that the service is running first and increased the timeout to 5000. Not sure what I'm doing wrong, is there still a problem with the escaping?

EDIT:
This also didn't work: New-Service OnShutdown -bin 'OnShutdown 30000 "powershell -noninteractive -noprofile -c \"cmd /c tv_off.lnk\""' -start Automatic

from onshutdown.

gfody avatar gfody commented on June 12, 2024

after the service is started you can run this command from powershell to check how the command made it through from new-service

Get-WmiObject win32_process -filter 'Name="OnShutdown.exe"' | select CommandLine

everything between the quotes is passed to CreateProcess as-is, you can test that with the start command in powershell it should behave the same.

I don't think you can invoke shortcuts with CreateProcess - for that you'd want to change ServiceMain.cpp#L60 to be a call to ShellExecute instead of CreateProcess. That might also require that you allow the service to interact with the desktop but I'm not sure.

from onshutdown.

roylleh avatar roylleh commented on June 12, 2024

That command returns this: OnShutdown 30000 "powershell -noninteractive -noprofile -c \"cmd /c tv_off.lnk\""

Basically exactly the string I provided it. The Services app in Windows also reports the same string for the service.

I was using a shortcut due to dependencies in the source file's folder. I tried using a script instead, but still no dice: New-Service OnShutdown -bin 'OnShutdown 30000 "powershell -NonInteractive -ExecutionPolicy Bypass -File C:\Windows\System32\tv_off.ps1"' -StartupType Automatic

Invoking the script manually works, so I assume my string still isn't valid for CreateProcess.

By the way I really appreciate you helping me out, this seems to be my only solution to turn my TV off automatically haha.

from onshutdown.

gfody avatar gfody commented on June 12, 2024

the command string looks okay to me. I suggest adding some logging within tv_off.ps1 - you can confirm that your script is being executed and capture any errors preventing it from working

from onshutdown.

roylleh avatar roylleh commented on June 12, 2024

Running this command works and creates the txt file: powershell -NonInteractive -File C:\Windows\System32\tv_off.ps1 *> C:\Users\royll\Desktop\foo.txt

However, when shutting down my PC, the text file is not created. This is the exact command: New-Service OnShutdown -bin 'OnShutdown 30000 "powershell -NonInteractive -File C:\Windows\System32\tv_off.ps1 *> C:\Users\royll\Desktop\foo.txt"' -StartupType Automatic

I also tried just putting Stop-Computer at the end of my script and running that instead, but that causes a "different" shutdown to occur which prevents apps from starting before login.

from onshutdown.

gfody avatar gfody commented on June 12, 2024

*> doesn't seem to do anything when passed to powershell via createprocess, eg start-process powershell '-file test.ps1 *> test.txt' doesn't produce a file even though test.ps1 is executed. I think you'll need to add some debug code within the script to see what's going wrong.

from onshutdown.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.