Wait-Debugger
Hiermee stopt u een script in het foutopsporingsprogramma voordat u de volgende instructie in het script uitvoert.
Syntaxis
Default (Standaard)
Wait-Debugger
Description
Stopt de PowerShell-scriptuitvoeringsengine op het punt direct na de Wait-Debugger cmdlet en wacht tot er een foutopsporingsprogramma is gekoppeld.
Waarschuwing
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_Debuggersvoor 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 ingevoegd in de functie 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 van de opdrachtregel in. De opdracht l bevat de scriptregels en u kunt andere opdrachten voor foutopsporingsprogramma's gebruiken om de scriptstatus te controleren.
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 de uitvoer van de l laat zien dat de uitvoering van het script is gestopt op de Wait-Debugger op regel 13.
Voorbeeld 2: Onderbrekingspunt invoegen voor foutopsporing van een DSC-resource
In dit voorbeeld is de opdracht Wait-Debugger ingevoegd in de CopyFile methode van een DSC-resource. Dit is vergelijkbaar met het gebruik van Enable-RunspaceDebug -BreakAll in een DSC-resource, maar wordt verbroken op een specifiek punt in het 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
}
}
Invoerwaarden
None
U kunt geen objecten doorsluisen naar deze cmdlet.
Uitvoerwaarden
None
Deze cmdlet retourneert geen uitvoer.