如何使用 script/powershell/... 确定远程计算机上的会话 ID 以便与 psexec -i 一起使用?

发布于 2024-11-14 21:10:36 字数 593 浏览 4 评论 0原文

我需要一个脚本或 powershell 命令来确定远程计算机上特定登录用户的会话 ID,稍后用作该会话上远程 gui 进程的 psexec -i 执行的参数远程计算机上的用户。

到目前为止,我设法用来

psexec \\remoteMachine -u user -p pswrd query session

获取远程计算机上的会话列表:

SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
console                                     0  Conn    wdcon
rdp-tcp#919       user                     1  Active  rdpwd
rdp-tcp#916       user                     3  Active  rdpwd

所以我想我可以以某种方式隔离所需的 id 并使用它 - 但还没有设法做到这一点

有什么想法吗? 也许还有其他更简单的方法?

感谢您的帮助。

I am in need of a script or powershell command that will be able to determine the session id of a specific logged in user on remote machine, to be later used as parameter to the psexec -i execution of remote gui process on that session of that user on the remote machine.

So far i managed to use

psexec \\remoteMachine -u user -p pswrd query session

to get list of sessions on the remote machine:

SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
console                                     0  Conn    wdcon
rdp-tcp#919       user                     1  Active  rdpwd
rdp-tcp#916       user                     3  Active  rdpwd

so i guess i could somehow isolate the needed id and use it - but haven't managed to do that yet

Any ideas?
Maybe other - simpler ways?

Thanks for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剧终人散尽 2024-11-21 21:10:36

只要您使用 PSExec,我就会坚持使用它。给定用户名,您可以很容易地获取 ID 字段,例如:

$username = 'joe'
$results = psexec \\remoteMachine -u adminuser -p password query session
$id = $results | Select-String "$username\s+(\w+)" |
                 Foreach {$_.Matches[0].Groups[1].Value}

psexec \\remoteMachine -u $username -i $id -d notepad.exe

请注意,您希望将 -d 与 PSExec 一起使用,否则它将等到启动的程序退出。

As long as you're using PSExec, I would just stick with it. You can get the ID field pretty easily given a username e.g.:

$username = 'joe'
$results = psexec \\remoteMachine -u adminuser -p password query session
$id = $results | Select-String "$username\s+(\w+)" |
                 Foreach {$_.Matches[0].Groups[1].Value}

psexec \\remoteMachine -u $username -i $id -d notepad.exe

Note that you want to use -d with PSExec otherwise it will wait until the launched program exits.

岁月染过的梦 2024-11-21 21:10:36

无需 PowerShell 即可完成此操作。
qwinsta 命令行工具 随 Windows 一起提供,您可以使用。

示例:

c:\>qwinsta
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 console                                     1  Conn
>rdp-tcp#0         YourUser                  2  Active  rdpwd
 rdp-tcp                                 65536  Listen

用法:

c:\>qwinsta /?
Display information about Remote Desktop Sessions.

QUERY SESSION [sessionname | username | sessionid]
              [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM]

  sessionname         Identifies the session named sessionname.
  username            Identifies the session with user username.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server to be queried (default is current).
  /MODE               Display current line settings.
  /FLOW               Display current flow control settings.
  /CONNECT            Display current connect settings.
  /COUNTER            Display current Remote Desktop Services counters information.
  /VM                 Display information about sessions within virtual machines.

It's possible to do that without PowerShell.
There is qwinsta command line tool that ships with Windows that you can use.

Example:

c:\>qwinsta
 SESSIONNAME       USERNAME                 ID  STATE   TYPE        DEVICE
 services                                    0  Disc
 console                                     1  Conn
>rdp-tcp#0         YourUser                  2  Active  rdpwd
 rdp-tcp                                 65536  Listen

Usage:

c:\>qwinsta /?
Display information about Remote Desktop Sessions.

QUERY SESSION [sessionname | username | sessionid]
              [/SERVER:servername] [/MODE] [/FLOW] [/CONNECT] [/COUNTER] [/VM]

  sessionname         Identifies the session named sessionname.
  username            Identifies the session with user username.
  sessionid           Identifies the session with ID sessionid.
  /SERVER:servername  The server to be queried (default is current).
  /MODE               Display current line settings.
  /FLOW               Display current flow control settings.
  /CONNECT            Display current connect settings.
  /COUNTER            Display current Remote Desktop Services counters information.
  /VM                 Display information about sessions within virtual machines.
笑脸一如从前 2024-11-21 21:10:36

使用 PSTeinalServices powershell 模块,您可以获得用户会话和 ID。
该模块可以在此处找到:http://code.msdn.microsoft.com/PSTerminalServices

PS > Get-TSSession -UserName user1 -ComputerName pc1 | select UserName,SessionId

UserName SessionId
-------- ---------
User             1

With the PSTerinalServices powershell module you can get the user sessions and IDs.
The module can be found here: http://code.msdn.microsoft.com/PSTerminalServices

PS > Get-TSSession -UserName user1 -ComputerName pc1 | select UserName,SessionId

UserName SessionId
-------- ---------
User             1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文