about_Debuggers

適用対象: Windows PowerShell 2.0, Windows PowerShell 3.0

概要をここに挿入してください。

トピック

about_Debuggers

概要

Windows PowerShell® デバッガ―について説明します。

詳細説明

デバッグとは、実行中のスクリプトを検査して、スクリプト命令のエラーを特定、修正するプロセスです。Windows PowerShell デバッガ―は、スクリプト、関数、コマンド、Windows PowerShell ワークフロー、または式のエラーや非効率な箇所を検査、特定するのに役立つように設計されています。

Windows PowerShell 4.0 では、Windows PowerShell デバッガーが更新されており、リモート コンピューター上の Windows PowerShell コンソール (ISE ではありません) で実行中のスクリプト、関数、ワークフロー、コマンド、式をデバッグできます。Enter-PSSession を実行して対話型のリモート PowerShell セッションを開始できます。このセッションで、ブレークポイントを設定してリモート コンピューター上のスクリプト ファイルとコマンドをデバッグできます。Enter-PSSession 機能が更新されており、リモート コンピューター上のスクリプトまたはコマンドを実行中のセッションが切断されても再接続して再開できます。実行中のスクリプトがブレークポイントに到達すると、クライアント セッションはデバッガーを自動的に開始します。スクリプトを実行中のセッションが切断時にブレークポイントに既に到達しており、ブレークポイントで停止していた場合、セッションに再接続した後、Enter-PSSession はコマンドライン デバッガーを自動的に開始します。

Windows PowerShell 4.0 デバッガーは、Windows PowerShell コンソールまたは Windows PowerShell ISE の Windows PowerShell ワークフローのデバッグにも使用できます。

Windows PowerShell デバッガーの機能を使用して、実行中の Windows PowerShell スクリプト、関数、コマンド、ワークフロー、または式を検査できます。Windows PowerShell デバッガーには、ブレークポイントを設定、管理し、呼び出し履歴を表示するための一連のコマンドレットが含まれています。

デバッガー コマンドレット

Windows PowerShell デバッガーに含まれているコマンドレットは次のとおりです。

          Set-PsBreakpoint:     Sets breakpoints on lines, variables, and
                                commands. 

          Get-PsBreakpoint:     Gets breakpoints in the current session.

          Disable-PsBreakpoint: Turns off breakpoints in the current session.

          Enable-PsBreakpoint:  Re-enables breakpoints in the current session.

          Remove-PsBreakpoint:  Deletes breakpoints from the current session.

          Get-PsCallStack:      Displays the current call stack. 

デバッガーの開始と停止

デバッガーを開始するには、1 つ以上のブレークポイントを設定します。その後、デバッグするスクリプト、コマンド、または関数を実行します。

ブレークポイントに到達すると、実行が停止して制御がデバッガーに移ります。

デバッガーを停止するには、完了するまでスクリプト、コマンド、または関数を実行します。あるいは、「stop」または「t」と入力します。

デバッガー コマンド

Windows PowerShell コンソールでデバッガーを使用する場合、次のコマンドを使用して実行を制御します。Windows PowerShell ISE では、[デバッグ] メニューでコマンドを使用します。

注記:その他のホスト アプリケーションでデバッガーを使用する方法については、ホスト アプリケーションのマニュアルを参照してください。

s, Step-into        Executes the next statement and then stops.


 v, Step-over        Executes the next statement, but skips functions
                            and invocations. The skipped statements are
                            executed, but not stepped through.


 o, Step-out         Steps out of the current function; up one level
                            if nested. If in the main body, it continues to
                            the end or the next breakpoint. The skipped
                            statements are executed, but not stepped through.


 c, Continue         Continues to run until the script is complete or
                            until the next breakpoint is reached. The skipped
                            statements are executed, but not stepped through.


        l, List             Displays the part of the script that is executing.
                            By default, it displays the current line, five
                            previous lines, and 10 subsequent lines. To continue
                            listing the script, press ENTER.
                        

        l <m>, List         Displays 16 lines of the script beginning with the
                            line number specified by <m>.                           

        l <m> <n>, List     Displays <n> lines of the script, beginning with the
                            line number specified by <m>.                           

        q, Stop             Stops executing the script, and exits the debugger.


        k, Get-PsCallStack  Displays the current call stack.


<Enter>             Repeats the last command if it was Step (s), 
                            Step-over (v), or List (l). Otherwise, represents a
                            submit action.  
                           

?, h                Displays the debugger command Help.


      To exit the debugger, use Stop (q).

これらのデバッガー コマンドを使用すると、スクリプトを実行して関心のある点で停止し、変数の値とシステムの状態を検査して問題を特定するまでスクリプトの実行を継続することができます。

注記:

">" などのリダイレクト演算子を含むステートメントにステップインすると、Windows PowerShell デバッガーはスクリプト内の残りのすべてのステートメントをステップオーバーします。

スクリプト変数の値の表示

デバッガーを実行している間も、コマンドを入力して変数の値を表示できるほか、コマンドレットを使用してコマンド ラインでスクリプトを実行できます。

次の自動変数を除いて、デバッグされているスクリプト内のすべての変数の現在の値を表示できます。

          $_
          $Args
          $Input
          $MyInvocation
          $PSBoundParameters

これらの変数のいずれかの値を表示しようとすると、スクリプト内の変数の値ではなく、デバッガーが使用する内部パイプライン用の変数の値が表示されます。

デバッグされているスクリプトについてこれらの変数の値を表示するには、スクリプトで自動変数の値を新しい変数に代入します。その後、新しい変数の値を表示できます。

例:

          $scriptArgs = $Args
          $scriptArgs

このトピックの例では、$MyInvocation 変数の値は次のように再割り当てされます。

          $scriptname = $MyInvocation.MyCommand.Path

デバッガー環境

ブレークポイントに到達すると、デバッガー環境が開始します。コマンド プロンプトが変更され、"[DBG]" で始まるものになります。ワークフローをデバッグする場合は、プロンプトは "[WFDBG]" になります。プロンプトはカスタマイズできます。

また、(Windows PowerShell Integrated Scripting Environment [ISE] では異なりますが) Windows PowerShell コンソールなどのいくつかのホスト アプリケーションでは、入れ子になったプロンプトがデバッグ用に開きます。コマンド プロンプトに大なり記号 (ASCII 62) が連続して表示されることから、入れ子になったプロンプトがわかります。

たとえば、Windows PowerShell コンソールの既定のデバッグ プロンプトは次のとおりです。

[DBG]: PS (get-location)>>>

$NestedPromptLevel 自動変数を使用して入れ子のレベルを確認できます。

また、自動変数 $PSDebugContext はローカル スコープで定義されます。$PsDebugContext 変数の存在を利用して、デバッガーが実行中であるかどうかを確認できます。

たとえば、次のように入力します。

          if ($psdebugcontext) {"Debugging"} else {"Not Debugging"}

デバッグの $PSDebugContext 変数の値を使用できます。

[DBG]: PS>>> $psdebugcontext.invocationinfo

        Name   CommandLineParameters  UnboundArguments  Location
        ----   ---------------------  ----------------  --------
        =      {}                     {}                C:\ps-test\vote.ps1 (1)

デバッグとスコープ

デバッガーに割り込んでも、動作しているスコープは変更されませんが、スクリプトのブレークポイントに到達すると、スクリプト スコープに移行します。スクリプト スコープは、デバッガーを実行したスコープの子スコープです。

スクリプト スコープで定義した変数とエイリアスを確認するには、Get-Alias コマンドレットまたは Get-Variable コマンドレットの Scope パラメーターを使用します。

たとえば、次のコマンドはローカル (スクリプト) スコープの変数を取得します。

  get-variable -scope 0

このコマンドは次のように省略できます。

gv -s 0

この方法は、スクリプトで定義した変数やデバッグ中に定義した変数のみを表示するのに便利です。

コマンド ラインでのデバッグ

変数のブレークポイントまたはコマンドのブレークポイントを設定する場合、スクリプト ファイル内にのみブレークポイントを設定できます。ただし、既定では、ブレークポイントは現在のセッションで実行している要素に設定されています。

たとえば、ブレークポイントを $name 変数に設定すると、デバッガーは、ブレークポイントを無効にするか削除するまで、すべてのスクリプト、コマンド、関数、スクリプト コマンドレット、または式のすべての $name 変数で中断します。

これにより、スクリプトが関数や変数のほか、セッションおよびユーザー プロファイル中の他のスクリプトから影響を受ける可能性があるという、より現実的なコンテキストでスクリプトをデバッグできます。

行のブレークポイントはスクリプト ファイルに固有であるため、スクリプト ファイル内でのみ設定されます。

ワークフローのデバッグ

Windows PowerShell 4.0 デバッガーは Windows PowerShell コンソールまたは Windows PowerShell ISE の Windows PowerShell ワークフローをデバッグするために使用できます。Windows PowerShell デバッガーを使用してワークフローをデバッグする場合、一部制限があります。

-- You can view workflow variables while you are in the debugger, but
         setting workflow variables from within the debugger is not supported.
      -- Tab completion when stopped in the workflow debugger is not
         available.
      -- Workflow debugging works only with synchronous running of workflows from
         a Windows PowerShell script. You cannot debug workflows if they are
         running as a job (with the –AsJob parameter).
      -- Other nested debugging scenarios--such as a workflow calling another
         workflow, or a workflow calling a script--are not implemented.

次の例はワークフローのデバッグを示しています。デバッガーがワークフロー関数にステップインすると、デバッガー プロンプトが [WFDBG] に変わることに注意してください。

          PS C:\> Set-PSBreakpoint -Script C:\TestWFDemo1.ps1 -Line 8

          ID Script           Line Command    Variable     Action
          -- ------           ---- -------    --------     ------
          0 TestWFDemo1.ps1   8

          PS C:\> C:\TestWFDemo1.ps1
          Entering debug mode. Use h or ? for help.

          Hit Line breakpoint on 'C:\TestWFDemo1.ps1:8'

          At C:\TestWFDemo1.ps1:8 char:5
          +     Write-Output -InputObject "Now writing output:"
          +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          [WFDBG:localhost]: PS C:\>> list

              3:
              4:  workflow SampleWorkflowTest
              5:  {
              6:      param ($MyOutput)
              7:
              8:*     Write-Output -InputObject "Now writing output:"
              9:      Write-Output -Input $MyOutput
             10:
             11:      Write-Output -InputObject "Get PowerShell process:"
             12:      Get-Process -Name powershell
             13:
             14:      Write-Output -InputObject "Workflow function complete."
             15:  }
             16:
             17:  # Call workflow function
             18:  SampleWorkflowTest -MyOutput "Hello"

          [WFDBG:localhost]: PS C:\>> $MyOutput
          Hello
          [WFDBG:localhost]: PS C:\>> stepOver
          Now writing output:
          At C:\TestWFDemo1.ps1:9 char:5
          +     Write-Output -Input $MyOutput
          +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          [WFDBG:localhost]: PS C:\>> list

              4:  workflow SampleWorkflowTest
              5:  {
              6:      param ($MyOutput)
              7:
              8:      Write-Output -InputObject "Now writing output:"
              9:*     Write-Output -Input $MyOutput
             10:
             11:      Write-Output -InputObject "Get PowerShell process:"
             12:      Get-Process -Name powershell
             13:
             14:      Write-Output -InputObject "Workflow function complete."
             15:  }
             16:
             17:  # Call workflow function
             18:  SampleWorkflowTest -MyOutput "Hello"
             19:

          [WFDBG:localhost]: PS C:\>> stepOver
          Hello
          At C:\TestWFDemo1.ps1:11 char:5
          +     Write-Output -InputObject "Get PowerShell process:"
          +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
          [WFDBG:localhost]: PS C:\>> stepOut
          Get PowerShell process:

          Handles  NPM(K)    PM(K)   WS(K) VM(M)   CPU(s)     Id ProcessName    PSComputerName
          -------  ------    -----   ----- -----   ------     -- -----------    --------------
              433      35   106688   128392   726     2.67   7124 powershell    localhost
              499      44   134244   172096   787     2.79   7452 powershell    localhost
          Workflow function complete.

関数のデバッグ

Begin セクション、Process セクション、および End セクションを含む関数にブレークポイントを設定すると、デバッガーは各セクションの最初の行で中断します。

たとえば、次のように入力します。

              function test-cmdlet
              {
                  begin
                  {
                      write-output "Begin"
                  }
                  process
                  {
                      write-output "Process"
                  }
                  end
                  {
                      write-output "End"
                  }
              }
        
          C:\PS> set-psbreakpoint -command test-cmdlet

          C:\PS> test-cmdlet
    
          Begin
          Entering debug mode. Use h or ? for help.

          Hit Command breakpoint on 'prompt:test-cmdlet'

          test-cmdlet

          [DBG]: C:\PS> c
          Process
          Entering debug mode. Use h or ? for help.

          Hit Command breakpoint on 'prompt:test-cmdlet'

          test-cmdlet

          [DBG]: C:\PS> c
          End
          Entering debug mode. Use h or ? for help.

          Hit Command breakpoint on 'prompt:test-cmdlet'

          test-cmdlet

          [DBG]: C:\PS> 

リモート スクリプトのデバッグ

Windows PowerShell 4.0 では、Windows PowerShell 4.0 コンソールでリモート セッションの Windows PowerShell デバッガーを実行できます。Enter-PSSession 機能が更新されており、リモート コンピューターで実行中の、スクリプトを現在実行しているセッションが切断されても再接続して再開できます。実行中のスクリプトがブレークポイントに到達すると、クライアント セッションはデバッガーを自動的に開始します。次の例は、ブレークポイントがスクリプトの 6 行目、11 行目、22 行目、25 行目に設定されている場合に、これがどのように機能するかを示しています。この例では、デバッガーの開始時に 2 つの識別プロンプトが存在することに注意してください。具体的には、セッションを実行しているコンピューターの名前と、デバッガー モードであることを示す DBG プロンプトです。

          Enter-Pssession -Cn localhost
          [localhost]: PS C:\psscripts> Set-PSBreakpoint .\ttest19.ps1 6,11,22,25

          ID Script          Line     Command          Variable          Action
          -- ------          ----     -------          --------          ------
          0 ttest19.ps1          6
          1 ttest19.ps1          11
          2 ttest19.ps1          22
          3 ttest19.ps1          25

           [localhost]: PS C:\psscripts> .\ttest19.ps1
          Hit Line breakpoint on 'C:\psscripts\ttest19.ps1:11'

          At C:\psscripts\ttest19.ps1:11 char:1
          + $winRMName = "WinRM"
          + ~
           [localhost]: [DBG]: PS C:\psscripts>> list


          6:      1..5 | foreach { sleep 1; Write-Output "hello2day $_" }
          7:  }
          8:
          9:  $count = 10
          10:  $psName = "PowerShell"
          11:* $winRMName = "WinRM"
          12:  $myVar = 102
          13:
          14:  for ($i=0; $i -lt $count; $i++)
          15:  {
          16:      sleep 1
          17:      Write-Output "Loop iteration is: $i"
          18:      Write-Output "MyVar is $myVar"
          19:
          20:      hello2day
          21:

           [localhost]: [DBG]: PS C:\psscripts>> stepover
          At C:\psscripts\ttest19.ps1:12 char:1
          + $myVar = 102
          + ~
           [localhost]: [DBG]: PS C:\psscripts>> quit
           [localhost]: PS C:\psscripts> Exit-PSSession
          PS C:\psscripts>

このテスト スクリプトでは、オペレーティング システムのバージョンが検出され、システムに応じたメッセージが表示されます。これには、関数、関数呼び出し、変数が含まれます。

次のコマンドを使用すると、テスト スクリプト ファイルの内容が表示されます。

  c:>\PS-test>  get-content test.ps1


  function psversion {
             "Windows PowerShell " + $psversiontable.psversion
              if ($psversiontable.psversion.major -lt 2) {
                  "Upgrade to Windows PowerShell 2.0!"
              }
              else {
                  "Have you run a background job today (start-job)?"
              }
          }

  $scriptname = $MyInvocation.MyCommand.Path
  psversion
  "Done $scriptname."

開始するには、行、コマンド、変数、関数など、スクリプト内の関心のある点にブレークポイントを設定します。

現在のディレクトリの Test.ps1 スクリプトの最初の行に行のブレークポイントを作成して開始します。

PS C:\ps-test> set-psbreakpoint -line 1 -script test.ps1 

このコマンドは、省略形

PS C:\ps-test> spb 1 -s test.ps1

コマンドは行のブレークポイント オブジェクトを返します (System.Management.Automation.LineBreakpoint)。

      Column     : 0
            Line       : 1
            Action     :
            Enabled    : True
            HitCount   : 0
            Id         : 0
            Script     : C:\ps-test\test.ps1
            ScriptName : C:\ps-test\test.ps1

次に、スクリプトを開始します。

PS C:\ps-test> .\test.ps1

スクリプトが最初のブレークポイントに到達すると、デバッガーがアクティブであることを示すブレークポイント メッセージが表示されます。これはブレークポイントについて説明し、スクリプトの最初の行 (関数宣言) をプレビューするものです。また、コマンド プロンプトが変更され、デバッガーが制御していることを示します。

プレビューの行には、スクリプト名とプレビューされたコマンドの行番号が含まれます。

          Entering debug mode. Use h or ? for help.

          Hit Line breakpoint on 'C:\ps-test\test.ps1:1'

          test.ps1:1   function psversion {
          DBG>

Step コマンド (s) を使用することで、スクリプトの最初のステートメントを実行し、次のステートメントをプレビューします。次のステートメントでは $MyInvocation 自動変数を使用して $ScriptName 変数の値をスクリプト ファイルのパスとファイル名に設定します。

          DBG> s
          test.ps1:11  $scriptname = $MyInvocation.MyCommand.Path

この時点で、$ScriptName 変数は入力されていませんが、その値を表示して変数の値を確認できます。この場合、値は $null です。

          DBG> $scriptname
          DBG>

もう一度 Step コマンド (s) を使用することで、現在のステートメントを実行し、スクリプトの次のステートメントをプレビューします。次のステートメントは PsVersion 関数を呼び出します。

  DBG> s
  test.ps1:12  psversion

この時点で、$ScriptName 変数は入力されていませんが、その値を表示して変数の値を確認します。この場合、値はスクリプト パスに設定されています。

          DBG> $scriptname
          C:\ps-test\test.ps1

もう一度 Step コマンドを使用して関数呼び出しを実行します。Enter キーを押すか、Step の「s」を入力します。

  DBG> s
  test.ps1:2       "Windows PowerShell " + $psversiontable.psversion

デバッグ メッセージには、関数のステートメントのプレビューが含まれます。このステートメントを実行し、関数の次のステートメントをプレビューするには、Step コマンドを使用できます。ただし、この例では、Step-Out コマンド (o) を使用します。このコマンドは (ブレークポイントに到達しない限り) 関数の実行を完了してスクリプトの次のステートメントに移動します。

  DBG> o
  Windows PowerShell 2.0
  Have you run a background job today (start-job)?
  test.ps1:13  "Done $scriptname"

スクリプトの最後のステートメントであるため、Step コマンド、Step-Out コマンド、Continue コマンドの結果は同じになります。この例では、Step-Out (o) を使用します。

  Done C:\ps-test\test.ps1
  PS C:\ps-test>

Step-Out コマンドが最後のコマンドを実行します。標準のコマンド プロンプトが、デバッガーが終了して制御をコマンド プロセッサに返したことを示します。

ここで、デバッガーを再度実行します。まず、現在のブレークポイントを削除するには、Get-PsBreakpoint コマンドレットと Remove-PsBreakpoint コマンドレットを使用します (ブレークポイントを再利用する可能性がある場合は、Remove-PsBreakpoint コマンドレットではなく Disable-PsBreakpoint コマンドレットを使用します)。

  PS C:\ps-test> Get-PsBreakpoint | Remove-PSBreakpoint

このコマンドは、省略形

  PS C:\ps-test> gbp | rbp

または、次のような関数を書き込んでコマンドを実行します。

  function delbr { gbp | rbp }

次に、$scriptname 変数にブレークポイントを作成します。

  PS C:\ps-test> set-psbreakpoint -variable scriptname -script test.ps1

このコマンドは次のように省略できます。

  PS C:\ps-test> sbp -v scriptname -s test.ps1

次に、スクリプトを開始します。スクリプトが変数のブレークポイントに到達します。既定のモードは Write です。そのため、実行は変数の値を変更するステートメントの直前で停止します。

  PS C:\ps-test> .\test.ps1
  Hit Variable breakpoint on 'C:\ps-test\test.ps1:$scriptname'
          (Write access)

  test.ps1:11  $scriptname = $MyInvocation.mycommand.path
  DBG>

$scriptname 変数の現在の値を表示します。値は $null です。

          DBG> $scriptname
          DBG>

Step コマンド (s) を使用して、変数を設定するステートメントを実行します。その後、$scriptname 変数の新しい値を表示します。

  DBG> $scriptname
  C:\ps-test\test.ps1

Step コマンド (s) を使用してスクリプトの次のステートメントをプレビューします。

  DBG> s
  test.ps1:12  psversion

次のステートメントは PsVersion 関数に対する呼び出しです。関数をスキップしながら実行するには、Step-Over コマンド (v) を使用します。Step-Over コマンドの使用時に既に関数内に位置している場合は、効果はありません。関数呼び出しが表示されますが、実行されません。

  DBG> v
  Windows PowerShell 2.0
  Have you run a background job today (start-job)?
  test.ps1:13  "Done $scriptname"   

Step-Over コマンドは関数を実行してスクリプトの次のステートメントをプレビューします。次のステートメントは最後の行を出力します。

Stop コマンド (t) を使用してデバッガーを終了します。コマンド プロンプトが標準のコマンド プロンプトに戻ります。

  C:\ps-test>

ブレークポイントを削除するには、Get-PsBreakpoint コマンドレットと Remove-PsBreakpoint コマンドレットを使用します。

  PS C:\ps-test> Get-PsBreakpoint | Remove-PSBreakpoint

新しいコマンド ブレークポイントを PsVersion 関数に作成します。

          PS C:\ps-test> Set-PsBreakpoint -command psversion -script test.ps1 

このコマンドは次のように省略できます。

          PS C:\ps-test> sbp -c psversion -s test.ps1 

次に、スクリプトを実行します。

          PS C:\ps-test> .\test.ps1
          Hit Command breakpoint on 'C:\ps-test\test.ps1:psversion'

          test.ps1:12  psversion
          DBG>

このスクリプトが関数呼び出しでブレークポイントに到達します。この時点では、関数はまだ呼び出されていません。これにより、Set-PsBreakpoint の Action パラメーターを使用してブレークポイントの実行の条件を設定するか、ログの開始や、診断またはセキュリティ スクリプトの呼び出しなど、準備タスクまたは診断タスクを実行することができます。

このアクションを設定するには、Continue コマンド (c) でスクリプトを終了し、Remove-PsBreakpoint コマンドで現在のブレークポイントを削除します (ブレークポイントが読み取り専用のため、アクションを現在のブレークポイントに追加できません)。

  DBG> c
  Windows PowerShell 2.0
  Have you run a background job today (start-job)?
  Done C:\ps-test\test.ps1

  PS C:\ps-test> get-psbreakpoint | remove-psbreakpoint
  PS C:\ps-test>

次に、新しいコマンド ブレークポイントをアクションに作成します。次のコマンドでは、関数が呼び出されたときに $scriptname 変数の値をログに記録するアクションにコマンド ブレークポイントを設定します。アクションで Break キーワードが使用されていないため、実行は停止しません(バックティック (`) は行継続文字です)。

         PS C:\ps-test> set-psbreakpoint -command psversion -script test.ps1  `
         -action { add-content "The value of `$scriptname is $scriptname." `
         -path action.log}

ブレークポイントの条件を設定するアクションを追加することもできます。次のコマンドでは、コマンド ブレークポイントは、実行ポリシーが RemoteSigned に設定された場合にのみ実行されます。このポリシーは最も制限が厳しいものの、スクリプトを実行できます (バックティック (`) は継続文字です)。

          PS C:\ps-test> set-psbreakpoint -script test.ps1 -command psversion `
          -action { if ((get-executionpolicy) -eq "RemoteSigned") { break }}

アクションの Break キーワードはデバッガーにブレークポイントを実行するように指示します。Continue キーワードを使用してデバッガーに中断せずに実行するように指示することもできます。既定のキーワードが Continue であるため、実行を停止するには Break を指定する必要があります。

次に、スクリプトを実行します。

  PS C:\ps-test> .\test.ps1
  Hit Command breakpoint on 'C:\ps-test\test.ps1:psversion'

  test.ps1:12  psversion

実行ポリシーが RemoteSigned に設定されているため、実行は関数呼び出しで停止します。

この時点で、呼び出し履歴を確認することもできます。Get-PsCallStack コマンドレットまたは Get-PsCallStack デバッガー コマンド (k) を使用します。次のコマンドは現在の呼び出し履歴を取得します。

  DBG> k
  2: prompt
  1: .\test.ps1: $args=[]
  0: prompt: $args=[]

この例では、Windows PowerShell デバッガーを使用するさまざまな方法のごく一部を示したにすぎません。

デバッガー コマンドレットの詳細を確認するには、次のコマンドを入力してください。

          help <cmdlet-name> -full

たとえば、次のように入力します。

          help set-psbreakpoint -full

Windows Powershell のその他のデバッグ機能

Windows PowerShell デバッガーのほか、Windows PowerShell には、スクリプトと関数をデバッグするために使用できるその他の機能が複数含まれています。

    -- Windows PowerShell Integrated Scripting Environment (ISE) includes
       an interactive graphical debugger. For more information, start Windows
       PowerShell ISE and press F1.

    -- The Set-PSDebug cmdlet offers very basic script debugging features,
       including stepping and tracing. 

    -- Use the Set-StrictMode cmdlet to detect references to 
       uninitialized variables, to references to non-existent properties
       of an object, and to function syntax that is not valid. 

    -- Add diagnostic statements to a script, such as statements that
       display the value of variables, statements that read input from
       the command line, or statements that report the current 
       instruction. Use the cmdlets that contain the Write verb for 
       this task, such as Write-Host, Write-Debug, Write-Warning, and
       Write-Verbose.          

関連項目

Disable-PsBreakpoint

Enable-PsBreakpoint

Get-PsBreakpoint

Get-PsCallStack

Remove-PsBreakpoint

Set-PsBreakpoint

Set-PsDebug

Set-Strictmode

Write-Debug

Write-Verbose