実行すると以下の様になり、暗黙的にOut-Defaultが呼び出されているのがわかる。
PS C:\> Write-Output "Hello World."
Called Out-Default | Value Type = string
Called Out-Default | Input value = Hello World.
Hello World.
実行すると以下の様になり、暗黙的にOut-Defaultが呼び出されているのがわかる。
PS C:\> Write-Output "Hello World."
Called Out-Default | Value Type = string
Called Out-Default | Input value = Hello World.
Hello World.
<# | |
Out-Defaultをフックしてパイプラインに渡されたオブジェクトの型と値をホストに表示する。 | |
Windows PowerShell in Actionのコードを参考。 | |
#> | |
function Out-Default | |
{ | |
[CmdletBinding(ConfirmImpact="Medium")] | |
param | |
( | |
[Parameter(ValueFromPipeline=$true)] | |
[System.Management.Automation.PSObject] $InputObject | |
) | |
begin | |
{ | |
$wrappedCmdlet = $ExecutionContext.InvokeCommand.GetCmdlet("Out-Default") | |
$sb = { & $wrappedCmdlet @PSBoundParameters } | |
$__sp = $sb.GetSteppablePipeline() | |
$__sp.Begin($pscmdlet) | |
} | |
process | |
{ | |
# パイプラインに渡されたオブジェクトの型と値をホストに表示 | |
Write-Host "Called Out-Default | Value Type = $($_.GetType())" | |
Write-Host "Called Out-Default | Input value = $($_)" | |
# パイプライン継続 | |
$__sp.Process($_) | |
} | |
end | |
{ | |
$__sp.End() | |
} | |
} |