如何从另一个powershell窗口访问powershell会话?

发布于 2024-11-30 23:03:20 字数 532 浏览 1 评论 0原文

我创建了一个 powershell 会话,

New-PSSession -ComputerName localhost

现在我可以访问上述会话

PS C:\> Get-PSSession

 Id Name            ComputerName    State    ConfigurationName     Availability
 -- ----            ------------    -----    -----------------     ------------
  1 Session1        localhost       Opened   Microsoft.PowerShell     Available

PS C:\> Enter-PSSession -Id 1

,如果我打开另一个 powershell 窗口,那么我可以访问上面创建的会话吗? 我确实尝试过,但“Enter-PSSession -Id 1”对我不起作用。这有可能吗?

I have an powershell session created by

New-PSSession -ComputerName localhost

I'm able to access the above session by

PS C:\> Get-PSSession

 Id Name            ComputerName    State    ConfigurationName     Availability
 -- ----            ------------    -----    -----------------     ------------
  1 Session1        localhost       Opened   Microsoft.PowerShell     Available

PS C:\> Enter-PSSession -Id 1

Now if I open another powershell window, then can I access the session created above ?
I did try but "Enter-PSSession -Id 1" did't work for me. Is this possible at all ?

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

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

发布评论

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

评论(2

幽蝶幻影 2024-12-07 23:03:20

您无法从另一个实例连接到在一个实例中创建的会话,会话是隔离的并且仅对创建它们的主机可用。

You cannot connect to sessions created in one instance from another one, sessions are isolated and available only to the host that created them.

谎言月老 2024-12-07 23:03:20

Enter-PSHostProcess cmdlet 连接并进入与本地进程的交互式会话。从 PowerShell 6.2 开始,非 Windows 平台支持此 cmdlet。

要获取其他会话,您可以使用 $PID获取 PSHostProcessInfo

PS C:\>Get-PSHostProcessInfo
    
ProcessName ProcessId AppDomainName    MainWindowTitle
----------- --------- -------------    ---------------
powershell      17148 DefaultAppDomain
powershell      25172 DefaultAppDomain
powershell      72348 DefaultAppDomain
pwsh            70732 DefaultAppDomain
pwsh            35072 DefaultAppDomain
pwsh            46624 DefaultAppDomain
pwsh            73228 DefaultAppDomain

然后您可以使用 Enter-PSHostProcess 连接到它

PS C:\>(Get-Process).Where({ $_.ID -eq 70732 }) | Get-PSHostProcessInfo | Enter-PSHostProcess
[Process:70732]: PS C:\Users\username\Documents> Get-Runspace
    
 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Busy
  3 Runspace3       localhost       Local         Opened        Available
  4 Runspace4       localhost       Local         Opened        Available
  5 RemoteHost      localhost       Local         Opened        Busy

请参阅 Enable-RunspaceDebug 和 < a href="https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/debug-runspace" rel="nofollow noreferrer">Debug-Runspace 从您连接的会话调试运行空间。

The Enter-PSHostProcess cmdlet connects to and enters into an interactive session with a local process. Beginning in PowerShell 6.2, this cmdlet is supported on non-Windows platforms.

To get the other session(s), you can use $PID or Get-PSHostProcessInfo.

PS C:\>Get-PSHostProcessInfo
    
ProcessName ProcessId AppDomainName    MainWindowTitle
----------- --------- -------------    ---------------
powershell      17148 DefaultAppDomain
powershell      25172 DefaultAppDomain
powershell      72348 DefaultAppDomain
pwsh            70732 DefaultAppDomain
pwsh            35072 DefaultAppDomain
pwsh            46624 DefaultAppDomain
pwsh            73228 DefaultAppDomain

Then you can connect to it using Enter-PSHostProcess.

PS C:\>(Get-Process).Where({ $_.ID -eq 70732 }) | Get-PSHostProcessInfo | Enter-PSHostProcess
[Process:70732]: PS C:\Users\username\Documents> Get-Runspace
    
 Id Name            ComputerName    Type          State         Availability
 -- ----            ------------    ----          -----         ------------
  1 Runspace1       localhost       Local         Opened        Busy
  3 Runspace3       localhost       Local         Opened        Available
  4 Runspace4       localhost       Local         Opened        Available
  5 RemoteHost      localhost       Local         Opened        Busy

See Enable-RunspaceDebug and Debug-Runspace on debugging a Runspace from the session you connected to.

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