Enter-PSHostProcess

连接到并进入与本地进程的交互会话。

语法

Enter-PSHostProcess
     [-Id] <Int32>
     [[-AppDomainName] <String>]
     [<CommonParameters>]
Enter-PSHostProcess
     [-Process] <Process>
     [[-AppDomainName] <String>]
     [<CommonParameters>]
Enter-PSHostProcess
     [-Name] <String>
     [[-AppDomainName] <String>]
     [<CommonParameters>]
Enter-PSHostProcess
     [-HostProcessInfo] <PSHostProcessInfo>
     [[-AppDomainName] <String>]
     [<CommonParameters>]

说明

Enter-PSHostProcess cmdlet 用于连接本地进程的交互会话并进入该会话。

远程的交互会话在已运行 Windows PowerShell 的现有进程中运行,而不是创建一个新进程来托管主机 Windows PowerShell 并运行远程会话。 与指定进程上的远程会话交互时,可以枚举正在运行的运行空间,然后选择一个运行空间,通过运行 Debug-Runspace 或 Enable-RunspaceDebug 对其进行调试。

你想要进入的进程必须能够托管 Windows PowerShell (System.Management.Automation.dll)。 而你必须是发现该进程的计算机上“管理员”组的成员,或必须是运行启动进程的脚本的用户。

选择要调试的运行空间后,无论该运行空间当前是正在运行某个命令还是在调试器中已停止运行,都将为该运行空间打开一个远程调试会话。 你可以在其中按照调试其他远程会话脚本的相同方式调试该运行空间脚本。

运行两次 exit,先后离开调试会话和进程的交互会话,或通过运行现有调试器 Quit 命令停止脚本执行。

如果使用 Name 参数指定进程,并且只找到一个具有指定名称的进程,则进入该进程。 如果找到多个具有指定名称的进程,则 Windows PowerShell 将返回错误,并列出所有找到的具有指定名称的进程。

若要支持连接到远程计算机上的进程,则在指定的远程计算机中启用 Enter-PSHostProcess cmdlet,以便你可以连接到远程 Windows PowerShell 会话中的本地进程。

示例

示例 1:在 Windows PowerShell ISE 进程内开始调试运行空间

In this example, you run **Enter-PSHostProcess** from within the Windows PowerShell console to enter the Windows PowerShell ISE process. In the resulting interactive session, you can find a runspace that you want to debug by running Get-Runspace, and then debug the runspace.
PS C:\> Enter-PSHostProcess -Name powershell_ise
[Process:1520]: PS C:\Test\Documents>

Next, get available runspaces within the process you have entered.
PS C:\> [Process:1520]: PS C:\>  Get-Runspace
Id    Name          InstanceId                               State           Availability
--    -------       -----------                              ------          -------------
1     Runspace1     2d91211d-9cce-42f0-ab0e-71ac258b32b5     Opened          Available
2     Runspace2     a3855043-cb16-424a-a616-685360c3763b     Opened          RemoteDebug
3     MyLocalRS     2236dbd8-2105-4dec-a15a-a27d0bfaacb5     Opened          LocalDebug
4     MyRunspace    771356e9-8c44-4b70-9de5-dd17cb41e48e     Opened          Busy
5     Runspace8     3e517382-a97a-49ba-9c3c-fd21f6664288     Broken          None

The runspace objects returned by **Get-Runspace** also have a NoteProperty called **ScriptStackTrace** of the running command stack, if available.Next, debug runspace ID 4, that is running another user's long-running script. From the list returned from **Get-Runspace**, note that the runspace state is **Opened**, and **Availability** is **Busy**, meaning that the runspace is still running the long-running script.
PS C:\> [Process:1520]: PS C:\>  (Get-Runspace -Id 4).ScriptStackTrace
Command                    Arguments                           Location
-------                    ---------                           --------
MyModuleWorkflowF1         {}                                  TestNoFile3.psm1: line 6
WFTest1                    {}                                  TestNoFile2.ps1: line 14
TestNoFile2.ps1            {}                                  TestNoFile2.ps1: line 22
<ScriptBlock>              {}                                  <No file>

Start an interactive debugging session with this runspace by running the Debug-Runspace cmdlet.
PS C:\> [Process: 1520]: PS C:\>  Debug-Runspace -Id 4
Hit Line breakpoint on 'C:\TestWFVar1.ps1:83'

At C:\TestWFVar1.ps1:83 char:1
+ $scriptVar = "Script Variable"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

[Process: 1520]: [RSDBG: 4]: PS C:\> >

After you are finished debugging, allow the script to continue running without the debugger attached by running the **exit** debugger command. Alternatively, you can quit the debugger with the **q** or **Stop** commands.
PS C:\> [Process:346]: [RSDBG: 3]: PS C:\> > exit
[Process:1520]: PS C:\>

When you are finished working in the process, exit the process by running the Exit-PSHostProcess cmdlet. This returns you to the PS C:\> prompt.
PS C:\> [Process:1520]: PS C:\>  Exit-PSHostProcess
PS C:\>

参数

-AppDomainName

Type:String
Position:1
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-HostProcessInfo

Type:PSHostProcessInfo
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

-Id

按进程 ID 指定进程。 若要获取进程 ID,请运行 Get-Process cmdlet。

Type:Int32
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Name

按进程名称指定进程。 若要获取进程名称,请运行 Get-Process cmdlet。 还可以在“任务管理器”中通过进程的“属性”对话框获取进程名称。

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Process

按进程对象指定进程。 使用此参数最简单的方法是,保存 Get-Process 命令的结果,该命令可返回要通过某个变量进入的进程,然后将该变量指定为此参数的值。

Type:Process
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

输入

Process

备注

  • Enter-PSHostProcess 无法进入在其中运行命令的 Windows PowerShell 会话的进程。 但是,可以进入另一个 Windows PowerShell 会话的进程,或与运行 Enter-PSHostProcess 的会话同时运行的 Windows PowerShell ISE 会话的进程。

    Enter-PSHostProcess 只能进入托管 Windows PowerShell 的进程。 也就是说,它们已加载 Windows PowerShell 引擎。

    若要从进程内退出进程,请键入 exit,然后按 Enter。