共用方式為


Enter-PSHostProcess

連線到互動式工作階段並進入具本機處理程序的互動式工作階段。

Syntax

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>]

Description

Enter-PSHostProcess Cmdlet 會連線到互動式工作階段並進入具本機處理程序的互動式工作階段。

遠端的互動式工作階段不會建立新的處理程序以裝載 Windows PowerShell 並執行遠端工作階段,而會在已經執行 Windows PowerShell 的現有處理程序中執行。 當您與指定處理程序的遠端工作階段互動時,您可以列舉執行中的 Runspace,然後選取執行 Debug-Runspace 或 Enable-RunspaceDebug 選取要進行偵錯的 Runspace。

您想要進入的程序必須裝載 Windows PowerShell (System.Management.Automation.dll)。 您在找到處理序的電腦上必須是 Administrators 群組的成員,或者您必須是執行啟動處理序之指令碼的使用者。

選取要進行偵錯的 Runspace 之後,如果該 Runspace 目前正在執行命令,或已在偵錯工具中停止,就會為該 Runspace 開啟遠端偵錯工作階段。 然後您可以使用對其他遠端工作階段指令碼進行偵錯相同的方式對 Runspace 指令碼進行偵錯。

與偵錯工作階段中斷,然後執行兩次 exit 中斷具處理程序的互動式工作階段,或在現有的偵錯工具執行 quit 命令停止指令碼執行。

如果您使用 Name 參數指定處理程序,而且只找到一個具指定名稱的處理程序,就會進入該處理程序。 如果找到多個處理程序具有指定的名稱,則 Windows PowerShell 會傳回錯誤,並列出所有具有指定名稱的處理程序。

若要支援連線到遠端電腦上的處理程序,必須在指定的遠端電腦上啟用 Enter-PSHostProcess Cmdlet,以便您可以在遠端的 Windows PowerShell 工作階段中連線到本機處理程序。

範例

範例 1︰在 Windows PowerShell ISE 處理程序中開始對 Runspace 進行偵錯

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

依處理序識別碼指定處理程序。 若要取得處理程序識別碼,請執行 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。