0

On Windows 11, I'm logged in as non-admin user. I want to execute a .SendKeys powershell .ps1 script. I verified the script works when run manually.

$obj = new-object -com wscript.shell 
$obj.SendKeys([char]175) # volume up
$obj.SendKeys([char]173) # volume mute
$obj.Dispose

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.keys

I tried both Group Policy Editor and Task Scheduler. Only Group Policy Editor works, and only on Start-Menu Restart. I haven't succeeded to automatically fire this script on Start-Menu Shutdown.

Here are the settings i tried:

gpedit (works on restart, but not shutdown)

  • Run gpedit as Admin.
  • Local Computer Policy > Computer Config > Windows Settings > Scripts > Shutdown > Powershell Scripts > Add > Browse to the .ps1 script
  • Local Computer Policy > Computer Config > Admin Templates > System > Scripts > Enable Display instructions in shutdown as they run > Enable
  • Start menu > Restart: Script runs
  • Start menu > Shutdown: Script doesn't run

Task Scheduler (fails on both start-menu shutdown and start-menu restart)

  • General: "user is logged on", or "user is logged on or off"
  • Three Triggers: Begin task on event > Basic > Log: System, Source: User32, Event ID's 1074 (shutdown), 4634 (account was logged off), and 4647 (User initiated logoff).
  • Action: Program: powershell, Arguments: pathname of .ps1 file (verified with manual Run)
  • Conditions: First 4 items checked
  • If i change General to "whether user is logged in or not", then i get alert "user doesn't have batch rights". I'm hoping i can fire a script on shutdown without changing user-rights. Changing the run-as account to the Windows admin account doesn't return the batch error, but it still doesn't run.
  • Fails with 2147943515 "User was logged off", or "A system shutdown is in progress", or "not logged on when the launching conditions were met", or "launched" but it doesn't actually execute

Other SU threads (answers don't work or are undesirable)

enter image description here

  • in gpedit, setting "Specify maximum wait time" to 5000 ms doesn't fire the script on start-menu shutdown. 5000 ms is shorter than the default of 10 minutes.
  • i don't want to create a custom "Shutdown" script, i want the user to be able to use start-menu shutdown.
  • i'm worried a wmi event watcher will consume system resources, and i want existing tools to work as advertised. If i can't find another solution, i'll try this.

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.