CmdID / PowerShell / Cmdlets / Wait
Wait-Debugger
Stops a script in the debugger before running the next statement in the script. PowerShell reference
From the PowerShell reference
What it does
Stops a script in the debugger before running the next statement in the script. Stops the PowerShell script execution engine at the point immediately after the Wait-Debugger cmdlet and waits for a debugger to be attached. CAUTION: Make sure you remove the Wait-Debugger lines after you are done. A running script appears to be hung when it's stopped at a Wait-Debugger. For more information about debugging in PowerShell, see about_Debuggers.
Wait-Debugger [<CommonParameters>]
Insert breakpoint for debugging
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."
}
}IdentifyInsert breakpoint for debugging a DSC resource
[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
}
}IdentifyTrips 1 Sigma rule: lowUnusually Long PowerShell CommandLine by oscd.community, Natalia Shornikova
Its page in the reference: Wait-Debugger.