2015-02-06 104 views
1

我试图找到一个简单的方法来达到ScriptBlockInvoke-Command。我已阅读Hey Scripting Guy博客,但他们只调用脚本文件,我对如何做到这一点感到困惑。PowerShell调试调用命令

有人可以告诉我如何让调试器停止在线说$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 

谢谢您的帮助。

回答

2

这是我的工作。顺便说一句,我在PowerShell 4.0中。

首先,我把设置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>> 
+0

谢谢你的帮助,但是当我用我的代码尝试这是一个比较复杂一点,我得到以下错误信息:'不能在远程会话中设置断点,因为 当前主机不支持远程调试。 + CategoryInfo:NotImplemented:(:) [SetPSBreakpoint],PSNotSupportedException + FullyQualifiedErrorId:SetPSBreakpoint:RemoteDebuggerNotSupported,Microsoft.PowerShell.Commands.SetPSBreakpointCommand'虽然我也对本地主机的Windows 2008 R2服务器上使用PowerShell 4.0。 – DarkLite1 2015-02-09 08:44:24

+0

这是来自PowerShell ISE的错误。在正常的PowerShell控制台中试用后,它确实告诉我有一个开放的会话,就像你有。但是,在Set-PSBreakpoint之后的几个'Invoke-Commands'后,'Get-PSSession' CmdLet不再给我任何打开的会话来连接.. – DarkLite1 2015-02-09 08:50:52