Created
March 25, 2023 05:13
-
-
Save emmaly/bcb26bf3b0869a9f91f50363bcac7240 to your computer and use it in GitHub Desktop.
Write-ThisDown.ps1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-ThisDownHere { | |
[CmdletBinding()] | |
param ( | |
[Parameter(ValueFromPipeline = $true)] | |
$InputObject, | |
[ValidateSet("Host", "Info", "Log")] | |
$Medium = "Host", | |
[ValidateSet("List", "Table", "Wide", "String", "JSON", "Raw")] | |
$Format = "Raw", | |
[switch] | |
$ShowAllProperties = $false | |
) | |
process { | |
$EffectiveMedium = $Medium | |
if (-not @("Host", "Info", "Log") -contains $EffectiveMedium) {return} # Unknown medium | |
if ($EffectiveMedium -eq "Host" -and (-not $WriteHost)) {return} # Host not requested | |
if ($EffectiveMedium -eq "Info" -and (-not $WriteInfo)) {return} # Info not requested or `InformationAction == SilentlyContinue` | |
if ($EffectiveMedium -eq "Log" -and (-not $WriteLog )) {return} # Log not requested or is disabled | |
if ($EffectiveMedium -eq "Host" -and (-not $Host.UI.SupportsVirtualTerminal)) {$EffectiveMedium = "NonVT-Host"} | |
$formatListProps = @{} | |
if ($ShowAllProperties) {$formatListProps = @{"Property"="*"}} | |
$convertJsonProps = @{} | |
switch ("$EffectiveMedium-$Format") { | |
"Host-Raw" {$InputObject | Out-Host} | |
"Host-String" {$InputObject | Out-String | Write-Host} | |
"Host-List" {$InputObject | Format-List @formatListProps | Out-Host} | |
"Host-Table" {$InputObject | Format-Table @formatListProps | Out-Host} | |
"Host-Wide" {$InputObject | Format-Wide @formatListProps | Out-Host} | |
"Host-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Out-Host} | |
"NonVT-Host-Raw" {$InputObject | Out-String | Write-Host} | |
"NonVT-Host-String" {$InputObject | Out-String | Write-Host} | |
"NonVT-Host-List" {$InputObject | Format-List @formatListProps | Out-String | Write-Host} | |
"NonVT-Host-Table" {$InputObject | Format-Table @formatListProps | Out-String | Write-Host} | |
"NonVT-Host-Wide" {$InputObject | Format-Wide @formatListProps | Out-String | Write-Host} | |
"NonVT-Host-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Write-Host} | |
"Info-Raw" {$InputObject | Out-String | Write-Information} | |
"Info-String" {$InputObject | Out-String | Write-Information} | |
"Info-List" {$InputObject | Format-List @formatListProps | Out-String | Write-Information} | |
"Info-Table" {$InputObject | Format-Table @formatListProps | Out-String | Write-Information} | |
"Info-Wide" {$InputObject | Format-Wide @formatListProps | Out-String | Write-Information} | |
"Info-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Write-Information} | |
"Log-Raw" {$InputObject | Out-File -FilePath $LogFilePath -Append} | |
"Log-String" {$InputObject | Out-String | Out-File -FilePath $LogFilePath -Append} | |
"Log-List" {$InputObject | Format-List @formatListProps | Out-File -FilePath $LogFilePath -Append} | |
"Log-Table" {$InputObject | Format-Table @formatListProps | Out-File -FilePath $LogFilePath -Append} | |
"Log-Wide" {$InputObject | Format-Wide @formatListProps | Out-File -FilePath $LogFilePath -Append} | |
"Log-JSON" {$InputObject | ConvertTo-Json @convertJsonProps | Out-File -FilePath $LogFilePath -Append} | |
Default {} | |
} | |
} | |
} | |
if ($WriteHost) { | |
Write-ThisDownHere -InputObject $InputObject -Medium "Host" -Format $Format -ShowAllProperties:$ShowAllProperties | |
} | |
if ($WriteInfo) { | |
Write-ThisDownHere -InputObject $InputObject -Medium "Info" -Format $Format -ShowAllProperties:$ShowAllProperties | |
} | |
if ($WriteLog) { | |
Write-ThisDownHere -InputObject $InputObject -Medium "Log" -Format $Format -ShowAllProperties:$ShowAllProperties | |
} | |
} | |
end { | |
if ($PassThru) { | |
$inputObject | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment