简体   繁体   English

如何(管理员)使用Windows 2008 R2远程桌面服务上的Powershell正常关闭在另一个用户会话中运行的窗口进程

[英]How do I (administrator) gracefully close a window process running in another user session using powershell on Windows 2008 R2 Remote Desktop Services

If I run the following command in my session... 如果我在会话中运行以下命令...

(Get-Process -Id $pid).CloseMainWindow()

I am able to gracefully shut down a process (no modal windows or other popups arise). 我能够正常关闭进程(不会出现模式窗口或其他弹出窗口)。

If, however, the pid is in another user's session on the same machine (running RDS), the process does not close, and CloseMainWindow() returns FALSE (it returns TRUE if it's running in my own session). 但是,如果该pid在同一台计算机上的另一个用户会话中(正在运行RDS),则该进程不会关闭,并且CloseMainWindow()返回FALSE(如果在我自己的会话中运行,则返回TRUE)。 It also works if I run the powershell from the other user's session. 如果我从其他用户的会话中运行Powershell,它也将起作用。

I specifically need a way to gracefully shut down the program as the program has a few important cleanup actions required to keep its database in order. 我特别需要一种可以正常关闭程序的方法,因为该程序具有一些重要的清除操作,可以使其数据库保持秩序。 So stop-process or process.kill() will not work. 因此stop-process或process.kill()将不起作用。

After lengthy research, it does not seem possible to do this. 经过长时间的研究,似乎不可能做到这一点。 There is, however, a solution which met at least some of my requirements. 但是,有一种解决方案至少可以满足我的一些要求。

You can create a Windows Scheduled Task which is triggered on session disconnect . 您可以创建Windows计划任务,该任务在会话断开连接时触发。 This allows you to run a cleanup job as the user , rather than as the administrator, which allows programs to exit gracefully. 这允许您以用户 (而不是管理员)身份运行清理作业,从而允许程序正常退出。

It has two major drawbacks.... 它有两个主要缺点。

  1. It is called even if the user just has a minor network interruption (so you have to build a wait() function in the script to sleep for a bit and then check if it is still disconnected - not a clean solution. 即使用户只是轻微的网络中断,它也会被调用(因此,您必须在脚本中构建一个wait()函数以使其休眠一会儿,然后检查它是否仍然断开连接-这不是一个干净的解决方案。

  2. It isn't called during a log-off event. 注销事件期间不会调用它。 For that you need to use a logoff script triggered by GPO. 为此,您需要使用由GPO触发的注销脚本。

Hope this helps someone in the future. 希望这对以后的人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在远程桌面上为非管理员用户建立 PowerShell 会话 - Establishing PowerShell session on remote desktop, for user who is not an administrator 如何使用Powershell在Windows Server 2008 R2上管理本地用户帐户? - How can I manage local useraccounts on windows server 2008 r2 with powershell? 如何使用PowerShell脚本在Windows Server 2008 R2上静默安装.NET 4.5。 - How to install .NET 4.5 silently on Windows Server 2008 R2 using powershell Script.? Powershell 4与Windows 2008 R2的兼容性 - Powershell 4 compatibility with Windows 2008 r2 如何在Windows Server 2008 R2上通过Powershell 4.0创建计划任务? - How can I create scheduled tasks via Powershell 4.0 on Windows Server 2008 R2? 如何在Windows 2008 R2域控制器上本地获取有关具有Powershell的RootDSE的信息 - How can I get information about the RootDSE with Powershell locally on a Windows 2008 R2 Domain Controller PowerShell无法在Windows 2008 R2上运行 - PowerShell not working on Windows 2008 R2 在Windows 2012 R2上以管理员权限执行PowerShell脚本,而无需用户交互 - Execute PowerShell script with administrator privileges on Windows 2012 R2 without user interaction 为什么我在 Windows Server 2012 r2 上的 PowerShell ISE 中运行 Get-WindowsCapability 时出现错误? - Why do I get an error running Get-WindowsCapability in PowerShell ISE on Windows Server 2012 r2? 使用PowerShell为Administrator配置远程桌面 - Configure Remote Desktop for Administrator with PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM