You can also experiment with your function that will be called instead of cmdlet Write-Debug
. Here is very quick implementation:
$global:__DebugInfo = new-object PSObject -prop @{
Enabled=$false
Messages=new-object System.Collections.ArrayList
}
function Write-Debug {
param([Parameter(Mandatory=$true)][string]$Message)
$d = Get-Command Write-Debug -CommandType cmdlet; & $d $Message;
if ($global:__DebugInfo.Enabled) {
$global:__DebugInfo.Messages.Add($Message) > $null
}
}
function Enable-Debug {
$global:DebugPreference = 'continue'
$global:__DebugInfo.Enabled = $true
}
function Disable-Debug {
$global:DebugPreference = 'silentlycontinue'
$global:__DebugInfo.Enabled = $false
}
# test
Enable-Debug
Write-Debug 'this is test debug message'
Write-Debug 'switch off'
Disable-Debug
Write-Debug 'this message should not be included'
Write-Host "Debug messages:"
Write-Host ($__DebugInfo.Messages -join "`n")