0

You can gracefully terminate a program normally by:

Get-Process "*notepad*" | Foreach-Object {$_.CloseMainWindow()}

I'm using a DSC configuration with Package or a cChoco resource to install a package. I'd like to warn the user the program is updating (msg * "Closing in 5 minutes" for example) and then attempt to close the program gracefully.

I'm unable to get this to work in a Chocolatey package (chocolateyinstall.ps1) or in a Desired State Configuration Script. I've also tried it running as a task scheduled script with no luck. I'm guessing because the process is running in a different context that DSC/Task Scheduler is running in.

I'm able to kill the process with Get-Process "*notepad*" | Stop-Process, but that just kills the process.

Is there any way I can do this? I'm guessing this would be the same as calling CloseMainWindow() on a process with a login script.

Ranadip Dutta
  • 8,857
  • 3
  • 29
  • 45
  • CloseMainWindow uses GetWindowLong WINAPI which AFAIK requires privileges to read the other process memory. I don't know about DSC but you may try opening the running DSC process manually via marshalled WINAPI functions: OpenProcess, then OpenProcessToken, LookupPrivilegeValue for SeDebugPrivilege, and use it in AdjustTokenPrivileges. – wOxxOm Mar 04 '17 at 07:15
  • DSC is running as system, there shouldn't be any problem with that – 4c74356b41 Mar 04 '17 at 08:08
  • well, I'm not a big expert in that, but assuming `system` has got highest privileges possible, why not? – 4c74356b41 Mar 04 '17 at 10:49
  • I'm a bit surprised that you can't use `msg * "message here"` in a Chocolatey package. If you run it outside of DSC, does it work? – ferventcoder Mar 05 '17 at 00:06

0 Answers0