[英]Runas in another Windows terminal session
為了簡單起見,假設用戶Administrator
在終端會話2
登錄。 另一個用戶Boda
登錄到終端會話3
。
是否有可能runas
在會議程序3
從會議2
?
為簡單起見,假設我要在會話3
(在Boda的會話中)啟動calc.exe
。 我怎么做? 能用runas
嗎?
就像Harry Johnston在評論中建議的那樣,您可以使用TechNet上提供的psexec
工具來執行此操作。 我已經使用運行終端服務的Windows 2008 Server對其進行了嘗試,並設法在另一個用戶會話中啟動了各種應用程序(盡管不是calc.exe,它已啟動但已最小化,並且窗口拒絕還原),其中包括cmd.exe。
我使用的命令是psexec.exe -i 3 cmd.exe
,其中3
是會話號(您可以從qwinsta.exe
獲取)。
示例:遠程會話,以管理員身份登錄; 使用qwinsta枚舉會話, psexec
使用psexec
在另一個會話上啟動cmd.exe
。
另一個會話:以Patrick身份登錄,管理員打開了桌面上的cmd.exe
窗口(窗口標題也顯示了該窗口)。
有一個命令行工具,稱為RunInSession 。 您至少需要指定要在其中啟動進程以及要啟動哪個進程的SessionId
。 如果要在遠程服務器上啟動,則服務器名稱是可選的。 如果不帶參數運行它,則會顯示一個帶有可能參數的對話框:
當前支持的操作系統版本是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
以其他用戶身份運行它。
可以使用Microsoft的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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.