PowerShell 调试调用命令
PowerShell Debug Invoke-Command
我正在尝试找到一种在 Invoke-Command
的 ScriptBlock
内达到峰值的简单方法。我已经阅读了 Hey Scripting Guy 博客,但他们只调用脚本文件,我对如何做到这一点感到有点困惑。
谁能告诉我如何让调试器在 $Var = 5
行停止?我试过 Set-PSBreakpoint
,但总是失败。这将方便检查值以及所有代码是否正确。
代码示例:
$session = New-PSSession -ComputerName localhost
Invoke-Command -Session $session -ScriptBlock $LoadFunctions
# Do other stuff
Invoke-Command -Session $session -ScriptBlock {
$Time = Get-Date
$Var = '5' # Stop debugger here
Set-PSBreakpoint -Variable $Var
}
Remove-PSSession $Session
感谢您的帮助。
以下是对我有用的方法。顺便说一句,我在使用 PowerShell 4.0。
首先,我将 set-psbreakpoint 放在脚本块的前面:
Invoke-Command -ComputerName . -ScriptBlock {
Set-PSBreakpoint -Variable metoo;
$test="foo";
$one="1";
$metoo="metoo";
} -Credential (Get-Credential)
当我 运行 收到以下消息时:
WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 on computer localhost has been
disconnected because the script running on the session has stopped at a breakpoint. Use the Enter-PSSession cmdlet on
this session to connect back to the session and begin interactive debugging.
WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 has been created for reconnection.
为了看到会话仍然存在,我执行了 Get-PSSession:
> Get-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
9 Session8 localhost Disconnected Microsoft.PowerShell None
太好了,会话已经存在,只需重新连接并输入:
> Get-PSSession | Connect-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
9 Session8 localhost Opened Microsoft.PowerShell RemoteDebug
并进入会话:
> Get-PSSession | Enter-PSSession
WARNING: You have entered a session that is currently stopped at a debug breakpoint inside a running command or script.
Use the Windows PowerShell command line debugger to continue debugging.
Entering debug mode. Use h or ? for help.
Hit Variable breakpoint on ':$metoo' (Write access)
At line:1 char:59
+ Set-PSBreakpoint -Variable metoo; $test="foo"; $one="1"; $metoo="metoo";
+ ~
[localhost]: [DBG]: PS C:\Users\Foo\Documents>>
太好了,现在我正在进行远程调试会话!只需按照提示操作,例如输入 "h" 寻求帮助或输入 "k" 获取 psscallstack 等
[localhost]: [DBG]: PS C:\Users\Foo\Documents>> h
s, stepInto Single step (step into functions, scripts, etc.)
v, stepOver Step to next statement (step over functions, scripts, etc.)
o, stepOut Step out of the current function, script, etc.
c, continue Continue operation
q, quit Stop operation and exit the debugger
k, Get-PSCallStack Display call stack
l, list List source code for the current script.
Use "list" to start from the current line, "list <m>"
to start from line <m>, and "list <m> <n>" to list <n>
lines starting from line <m>
<enter> Repeat last command if it was stepInto, stepOver or list
?, h displays this help message.
For instructions about how to customize your debugger prompt, type "help about_prompt".
[localhost]: [DBG]: PS C:\Users\Foo\Documents>>
我正在尝试找到一种在 Invoke-Command
的 ScriptBlock
内达到峰值的简单方法。我已经阅读了 Hey Scripting Guy 博客,但他们只调用脚本文件,我对如何做到这一点感到有点困惑。
谁能告诉我如何让调试器在 $Var = 5
行停止?我试过 Set-PSBreakpoint
,但总是失败。这将方便检查值以及所有代码是否正确。
代码示例:
$session = New-PSSession -ComputerName localhost
Invoke-Command -Session $session -ScriptBlock $LoadFunctions
# Do other stuff
Invoke-Command -Session $session -ScriptBlock {
$Time = Get-Date
$Var = '5' # Stop debugger here
Set-PSBreakpoint -Variable $Var
}
Remove-PSSession $Session
感谢您的帮助。
以下是对我有用的方法。顺便说一句,我在使用 PowerShell 4.0。
首先,我将 set-psbreakpoint 放在脚本块的前面:
Invoke-Command -ComputerName . -ScriptBlock {
Set-PSBreakpoint -Variable metoo;
$test="foo";
$one="1";
$metoo="metoo";
} -Credential (Get-Credential)
当我 运行 收到以下消息时:
WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 on computer localhost has been
disconnected because the script running on the session has stopped at a breakpoint. Use the Enter-PSSession cmdlet on
this session to connect back to the session and begin interactive debugging.
WARNING: Session Session8 with instance ID 4a02c5f4-b333-4e58-85b7-78ccd4f31318 has been created for reconnection.
为了看到会话仍然存在,我执行了 Get-PSSession:
> Get-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
9 Session8 localhost Disconnected Microsoft.PowerShell None
太好了,会话已经存在,只需重新连接并输入:
> Get-PSSession | Connect-PSSession
Id Name ComputerName State ConfigurationName Availability
-- ---- ------------ ----- ----------------- ------------
9 Session8 localhost Opened Microsoft.PowerShell RemoteDebug
并进入会话:
> Get-PSSession | Enter-PSSession
WARNING: You have entered a session that is currently stopped at a debug breakpoint inside a running command or script.
Use the Windows PowerShell command line debugger to continue debugging.
Entering debug mode. Use h or ? for help.
Hit Variable breakpoint on ':$metoo' (Write access)
At line:1 char:59
+ Set-PSBreakpoint -Variable metoo; $test="foo"; $one="1"; $metoo="metoo";
+ ~
[localhost]: [DBG]: PS C:\Users\Foo\Documents>>
太好了,现在我正在进行远程调试会话!只需按照提示操作,例如输入 "h" 寻求帮助或输入 "k" 获取 psscallstack 等
[localhost]: [DBG]: PS C:\Users\Foo\Documents>> h
s, stepInto Single step (step into functions, scripts, etc.)
v, stepOver Step to next statement (step over functions, scripts, etc.)
o, stepOut Step out of the current function, script, etc.
c, continue Continue operation
q, quit Stop operation and exit the debugger
k, Get-PSCallStack Display call stack
l, list List source code for the current script.
Use "list" to start from the current line, "list <m>"
to start from line <m>, and "list <m> <n>" to list <n>
lines starting from line <m>
<enter> Repeat last command if it was stepInto, stepOver or list
?, h displays this help message.
For instructions about how to customize your debugger prompt, type "help about_prompt".
[localhost]: [DBG]: PS C:\Users\Foo\Documents>>