为了简单起见,假设用户Administrator
在terminal会话2
login。 另一个用户Boda
在terminal会话3
login。
会话2
中的会话3
可以运行一个程序吗?
为了简单起见,假设我想在会话3
(在Boda的会话中)启动calc.exe
。 我怎么做? 可以用runas
来完成吗?
像Harry Johnston在评论中建议的那样,您可以使用TechNet上提供的psexec
工具来完成此操作。 我已经尝试使用运行终端服务的Windows 2008服务器,并设法启动另一个用户会话中的各种应用程序(虽然不calc.exe – 它启动,但最小化,窗口拒绝还原),其中cmd.exe。
我使用的命令是psexec.exe -i 3 cmd.exe
,其中3
是会话编号(可以从qwinsta.exe
获得)。
示例:以管理员身份登录的远程会话; 使用qwinsta枚举会话和psexec
在另一个会话上启动cmd.exe
。
另一个会话:以Patrick身份登录,由Administrator打开的桌面上的cmd.exe
窗口(窗口标题也显示)。
有一个命令行工具,它叫做RunInSession 。 您至少需要指定要启动进程的SessionId
以及要启动的进程。 如果要在远程服务器上启动,可选是servername。 如果不带参数运行它,将显示一个包含可能参数的对话框:
目前支持的操作系统版本是Windows XP,2003,Vista和2008。
该程序需要在Localsystem用户的上下文中运行,因此它暂时将自己安装为服务并自行启动。 使用WTSQueryUserToken,它获得所请求的终端会话的主用户令牌。 最后,使用CreateProcessAsUser启动该进程,服务将自行删除。
更多细节:
它的一种黑客,但它对我非常有用。 比我的环境中的psexec.exe更快。
只要在远程计算机上创建一个临时任务,为特定的用户或组运行它,而不是删除任务。
我为它创建了一个powershell脚本:
param ( [string]$Computer = ($env:computername), [string]$User = "", [string]$Command, [string]$Args ) $script_task = { param ( [string]$User = "", [string]$Command, [string]$Args ) #Action $Action = New-ScheduledTaskAction –Execute $Command if($Args.Length > 0) { $Action = New-ScheduledTaskAction –Execute $Command -Argument $Args} #Principal $P = New-ScheduledTaskPrincipal -UserId $User -LogonType Interactive -ErrorAction Ignore #Settings $S = New-ScheduledTaskSettingsSet -MultipleInstances Parallel -Hidden #Create TEMPTASK $TASK = New-ScheduledTask -Action $Action -Settings $S -Principal $P #Unregister old TEMPTASK Unregister-ScheduledTask -TaskName 'TEMPTASK' -ErrorAction Ignore -Confirm:$false #Register TEMPTASK Register-ScheduledTask -InputObject $TASK -TaskPath '\KD\' -TaskName 'TEMPTASK' #Execute TEMPTASK Get-ScheduledTask -TaskName 'TEMPTASK' -TaskPath '\KD\' | Start-ScheduledTask #Unregister TEMPTASK Unregister-ScheduledTask -TaskName 'TEMPTASK' -ErrorAction Ignore -Confirm:$false } #The scriptblock get the same parameters of the .ps1 Invoke-Command -ComputerName $Computer -ScriptBlock $script_task -ArgumentList $User, $Command, $Args
用法示例:
file.ps1 -User USER_NAME -Command notepad.exe -Computer REMOTE_COMPUTER
我不知道有什么办法可以控制另一个打开的cmd会话。 但是,您应该能够使用runas
作为另一个用户运行它。
这可以使用微软的Sysinternals工具进行存档。 除了远程运行命令和脚本列表之外,它们对很多事情都很有用。 作为管理员,他们多次是我的救世主。
#To run a command on single computer remotly psexec \\RemoteComputerName Path_Of_Executable_On_Remote_Computer Argument_list #To run a command on list of computers remotely. psexec @Remote_Computer_list Path_Of_Executable_On_Remote_Computer Argument_list /AcceptEULA #To run list of commands on list of remote computer. make sure you copy batch file before you run command below. psexec @Remote_Computer_List Path_Of_Batch_On_Remote_Computer Argument_list