Skip to content

Prevent outputing secrets to PS console #57

@sayedihashimi

Description

@sayedihashimi

We should have a way to prevent secrets from getting displayed in the PS console. This can be achieved by overriding the PS output functions. The ones I can think of are:

  • Output-Default
  • Write-Output
  • Write-Host
  • Write-Debug
  • Write-Error
  • Write-Warning
  • Write-Verbose

Here is a basic sample showing how to do this.

'executing test.ps1' | Write-Output


function Out-Default{
    [cmdletbinding(ConfirmImpact='Medium')]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [System.Management.Automation.PSObject]$InputObject
    )
    begin{
        $wrappedObject = $ExecutionContext.InvokeCommand.GetCmdlet('Out-Default')
        $sb = { & $wrappedObject @PSBoundParameters }
        $__sp = $sb.GetSteppablePipeline()
        $__sp.Begin($pscmdlet)
    }
    process{
        $__sp.Process('***' + $_)
    }
    end{
        $__sp.End()
    }
}


function Write-Host{
    [cmdletbinding(ConfirmImpact='Medium')]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [System.Management.Automation.PSObject]$InputObject
    )
    begin{
        $wrappedObject = $ExecutionContext.InvokeCommand.GetCmdlet('Write-Host')
        $sb = { & $wrappedObject @PSBoundParameters }
        $__sp = $sb.GetSteppablePipeline()
        $__sp.Begin($pscmdlet)
    }
    process{
        $__sp.Process('***' + $_)
    }
    end{
        $__sp.End()
    }
}


function Write-Output{
    [cmdletbinding(ConfirmImpact='Medium')]
    param(
        [Parameter(ValueFromPipeline=$true)]
        [System.Management.Automation.PSObject]$InputObject
    )
    begin{
        $wrappedObject = $ExecutionContext.InvokeCommand.GetCmdlet('Write-Host')
        $sb = { & $wrappedObject @PSBoundParameters }
        $__sp = $sb.GetSteppablePipeline()
        $__sp.Begin($pscmdlet)
    }
    process{
        $__sp.Process('***' + $_)
    }
    end{
        $__sp.End()
    }
}


'test' | out-default
'http://sedodream.com' | Out-Default
'foo'|Write-Output
'bar'|Write-Host

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions