Delen via


Wait-Debugger

Hiermee stopt u een script in het foutopsporingsprogramma voordat u de volgende instructie in het script uitvoert.

Syntaxis

Wait-Debugger []

Description

Stopt de Engine voor het uitvoeren van PowerShell-scripts onmiddellijk na de Wait-Debugger cmdlet en wacht tot er een foutopsporingsprogramma is gekoppeld.

Let op

Zorg ervoor dat u de Wait-Debugger regels verwijdert nadat u klaar bent. Een actief script lijkt vast te hangen wanneer het is gestopt bij een Wait-Debugger.

Zie about_Debuggers voor meer informatie over foutopsporing in PowerShell.

Voorbeelden

Voorbeeld 1: Onderbrekingspunt invoegen voor foutopsporing

Het bestand dbgtest.ps1 bevat een functie Test-Condition. De Wait-Debugger opdracht is in de functie ingevoegd om de uitvoering van het script op dat moment te stoppen. Wanneer u de functie uitvoert, stopt het script op de Wait-Debugger regel en voert u het foutopsporingsprogramma voor de opdrachtregel in. De l opdracht bevat de scriptregels en u kunt andere foutopsporingsprogrammaopdrachten gebruiken om de scriptstatus te inspecteren.

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

U ziet dat uitvoer van de l uitvoer laat zien dat de uitvoering van het script is gestopt op regel Wait-Debugger 13.

Voorbeeld 2: Onderbrekingspunt invoegen voor foutopsporing van een DSC-resource

In dit voorbeeld is de Wait-Debugger opdracht ingevoegd in de CopyFile methode van een DSC-resource. Dit is vergelijkbaar met het gebruik Enable-RunspaceDebug -BreakAll in een DSC-resource, maar wordt op een specifiek punt in het script verbroken.

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

Invoerwaarden

None

U kunt geen objecten doorsluisen naar deze cmdlet.

Uitvoerwaarden

None

Deze cmdlet retourneert geen uitvoer.