Wait-Debugger
在執行下一句指令前,先在除錯器中停止腳本。
語法
Default (預設值)
Wait-Debugger
Description
在 cmdlet 之後 Wait-Debugger 立即停止 PowerShell 腳本執行引擎,並等待附接除錯器。
Caution
完成後務必移除線 Wait-Debugger 條。 當一個正在執行的腳本被停 Wait-Debugger在 .
欲了解更多關於 PowerShell 除錯的資訊,請參見 about_Debuggers。
範例
範例 1:插入除錯斷點
該檔案 dbgtest.ps1 包含一個函式 Test-Condition。
Wait-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 輸出顯示腳本執行在 Wait-Debugger 第 13 行停止。
範例 2:插入斷點以除錯 DSC 資源
在此範例中,指令 Wait-Debugger 入於 CopyFile DSC 資源的方法中。 這類似於在 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 不會傳回任何輸出。