Compartilhar via


Wait-Debugger

Interrompe um script no depurador antes de executar a próxima instrução no script.

Sintaxe

Default (Padrão)

Wait-Debugger

Description

Interrompe o mecanismo de execução de script do PowerShell no ponto imediatamente após o cmdlet Wait-Debugger e aguarda que um depurador seja anexado.

Cuidado

Certifique-se de remover as linhas de Wait-Debugger depois de terminar. Um script em execução parece estar travado quando ele é parado em um Wait-Debugger.

Para obter mais informações sobre depuração no PowerShell, consulte about_Debuggers.

Exemplos

Exemplo 1: Inserir ponto de interrupção para depuração

O dbgtest.ps1 de arquivo contém uma função Test-Condition. O comando Wait-Debugger foi inserido na função para interromper a execução do script naquele momento. Quando você executa a função, o script para na linha Wait-Debugger e insere o depurador de linha de comando. O comando l lista as linhas de script e você pode usar outros comandos de depurador para inspecionar o estado do script.

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:\>

Observe que a saída do l mostra que a execução do script foi interrompida no Wait-Debugger na linha 13.

Exemplo 2: Inserir ponto de interrupção para depurar um recurso DSC

Neste exemplo, o comando Wait-Debugger foi inserido no método CopyFile de um recurso DSC. Isso é semelhante ao uso de Enable-RunspaceDebug -BreakAll em um recurso DSC, mas é interrompido em um ponto específico do script.

[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
    }
}

Entradas

None

Não é possível transferir objetos para esse cmdlet.

Saídas

None

Esse cmdlet não retorna nenhuma saída.