共用方式為


Wait-Debugger

在腳本中執行下一個語句之前,先停止調試程式中的腳本。

語法

Default (預設值)

Wait-Debugger

Description

Wait-Debugger Cmdlet 之後立即停止 PowerShell 腳本執行引擎,並等候調試程式附加。

謹慎

完成之後,請務必移除 Wait-Debugger 行。 當執行中的腳本在 Wait-Debugger停止時,它似乎已停止。

如需在 PowerShell 中偵錯的詳細資訊,請參閱 about_Debuggers

範例

範例 1:插入斷點以進行偵錯

檔案 dbgtest.ps1 包含函式 Test-ConditionWait-Debugger 命令已插入函式中,以在該時間點停止腳本執行。 當您執行函式時,腳本會在 Wait-Debugger 行停止,並輸入命令行調試程式。 l 命令會列出腳本行,而且您可以使用其他調試程式命令來檢查腳本狀態。

function Test-Condition {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)]
        [string]$Name,
        [string]$Message = "Hello, $Name!"
    )

    if ($Name -eq $Env:USERNAME) {
        Write-Output "$Message"
    } else {
        # Remove after debugging
        Wait-Debugger

        Write-Output "$Name is not the current user."
    }
}
PS D:\> Test-Condition Fred
Entering debug mode. Use h or ? for help.

At D:\temp\test\dbgtest.ps1:13 char:9
+         Wait-Debugger
+         ~~~~~~~~~~~~~
[DBG]: PS D:\>> l

    8:
    9:      if ($Name -eq $Env:USERNAME) {
   10:          Write-Output "$Message"
   11:      } else {
   12:          # Remove after debugging
   13:*         Wait-Debugger
   14:
   15:          Write-Output "$Name is not the current user."
   16:      }
   17:  }

[DBG]: PS D:\>> $Env:USERNAME
User01
[DBG]: PS D:\>> exit
PS D:\>

請注意,來自 l 的輸出會顯示腳本執行已停止於第 13 行的 Wait-Debugger

範例 2:插入斷點以偵錯 DSC 資源

在此範例中,Wait-Debugger 命令已插入 DSC 資源的 CopyFile 方法中。 這類似於在 DSC 資源中使用 Enable-RunspaceDebug -BreakAll,但在腳本的特定時間點中斷。

[DscResource()]
class FileResource
{
    [DscProperty(Key)]
    [string] $Path

    [DscProperty(Mandatory)]
    [Ensure] $Ensure

    [DscProperty(Mandatory)]
    [string] $SourcePath

    [DscProperty(NotConfigurable)]
    [Nullable[datetime]] $CreationTime


    [void] Set() {
        $fileExists = $this.TestFilePath($this.Path)
        if ($this.Ensure -eq [Ensure]::Present) {
            if (! $fileExists) {
               $this.CopyFile()
            }
        } else {
            if ($fileExists) {
                Write-Verbose -Message "Deleting the file $($this.Path)"
                Remove-Item -LiteralPath $this.Path -Force
            }
        }
    }

    [bool] Test() {
        $present = Test-Path -LiteralPath $this.Path
        if ($this.Ensure -eq [Ensure]::Present) {
            return $present
        } else {
            return (! $present)
        }
    }

    [FileResource] Get() {
        $present = Test-Path -Path $this.Path
        if ($present) {
            $file = Get-ChildItem -LiteralPath $this.Path
            $this.CreationTime = $file.CreationTime
            $this.Ensure = [Ensure]::Present
        } else {
            $this.CreationTime = $null
            $this.Ensure = [Ensure]::Absent
        }
        return $this
    }

    [void] CopyFile() {
        # Testing only - Remove before deployment!
        Wait-Debugger

        if (! (Test-Path -LiteralPath $this.SourcePath)) {
            throw "SourcePath $($this.SourcePath) is not found."
        }
        if (Test-Path -LiteralPath $this.Path -PathType Container) {
            throw "Path $($this.Path) is a directory path"
        }
        Write-Verbose "Copying $($this.SourcePath) to $($this.Path)"
        Copy-Item -LiteralPath $this.SourcePath -Destination $this.Path -Force
    }
}

輸入

None

您不能將物件透過管道傳送到此 Cmdlet。

輸出

None

此 Cmdlet 不會傳回任何輸出。