![](/img/trans.png)
[英]Debug powershell in Visual studio code with parameters from command line
[英]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放在scriptblock的前面:
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>>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.