PowerShellLoggingModule icon indicating copy to clipboard operation
PowerShellLoggingModule copied to clipboard

Enable-OutputSubscriber script blocks have the potential for a stackoverflow exception

Open Sarafian opened this issue 8 years ago • 0 comments

When inside one of the script blocks, something will be written to the console, then this is also captured. Initially it sounds reasonable but there is a danger here.

$onWriteDebugBlock={
    param(
        [string]$Message
    )
    Write-Host "[OnWriteDebug] $Message"
}
$onWriteVerboseBlock={
    param(
        [string]$Message
    )
    Write-Host "[OnWriteVerbose] $Message"
}
$onWriteErrorBlock={
    param(
        [string]$Message
    )
    Write-Host "[OnWriteError] $Message"
}
$onWriteWarningBlock={
    param(
        [string]$Message
    )
    Write-Host "[OnWriteWarning] $Message"
}
$onWriteOutputBlock={
    param(
        [string]$Message
    )
    Write-Host "[OnWriteOutput] $Message"
}

$splat=@{
    OnWriteDebug=$onWriteDebugBlock
    OnWriteVerbose=$onWriteVerboseBlock
    OnWriteError=$onWriteErrorBlock
    OnWriteOutput=$onWriteOutputBlock
    OnWriteWarning=$onWriteWarningBlock
}

$outputSubscriber=Enable-OutputSubscriber @splat

Write-Error "An error message"

The Write-Error "An error message" will be captured by onWriteErrorBlock and written to the host as [OnWriteError] An error message which in turn will be captured by onWriteOutputBlock and written to the host as [OnWriteError] [OnWriteError] An error message. This flow is recursive.

The danger here is not only with the error or output but also with debug. Imagine using in the OnWriteDebug block a cmdlet that outputs debug messages.

The module is very cool but have you considered this flow?

Sarafian avatar Jun 13 '17 07:06 Sarafian