共用方式為


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>]
Enter-PSHostProcess
     -CustomPipeName <String>
     [<CommonParameters>]

Description

Cmdlet Enter-PSHostProcess 會連線到 ,並使用本機進程進入互動式會話。 從 PowerShell 6.2 開始,非 Windows 平台上支援此 Cmdlet。

遠端互動式會話是在已執行 PowerShell 的現有進程中執行,而不是建立新的進程來裝載 PowerShell 並執行遠端會話。 當您在指定的進程上與遠端會話互動時,您可以列舉執行 Runspaces,然後執行 或 Enable-RunspaceDebug來選取 Runspace 進行偵Debug-Runspace錯。

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

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

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

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

為了支援在遠端電腦上附加至進程,Cmdlet Enter-PSHostProcess 會在指定的遠端電腦上啟用,讓您可以在遠端 PowerShell 會話內附加至本機進程。

範例

範例 1:開始偵錯 PowerShell ISE 進程內的 Runspace

在此範例中,您會從 PowerShell 控制台內執行 Enter-PSHostProcess ,以輸入 PowerShell ISE 程式。 在產生的互動式會話中,您可以執行 Get-Runspace來尋找您想要偵錯的 Runspace,然後偵錯 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

指定要在省略時連線的應用程式功能變數名稱,請使用 DefaultAppDomain。 用來 Get-PSHostProcessInfo 顯示應用程式功能變數名稱。

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

-CustomPipeName

取得或設定要連接的自定義命名管道名稱。 這通常與 pwsh -CustomPipeName搭配使用。

此參數是在 PowerShell 6.2 中引進的。

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

-HostProcessInfo

指定可以使用 PowerShell 連線的 PSHostProcessInfo 物件。 使用 Get-PSHostProcessInfo 來取得物件。

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 無法輸入執行命令之 PowerShell 工作階段的程式。 不過,您可以輸入另一個 PowerShell 工作階段的程式,或執行所在的工作階段同時執行的 PowerShell ISE 會話 Enter-PSHostProcess

Enter-PSHostProcess 只能輸入裝載 PowerShell 的進程。 也就是說,他們已載入 PowerShell 引擎。

若要從進程內結束進程,請輸入 exit,然後按 Enter