Skip to content

Instantly share code, notes, and snippets.

@stknohg
Last active January 11, 2016 01:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Out-Defaultをフックしてパイプラインに渡されたオブジェクトの型と値をホストに表示するサンプル

実行すると以下の様になり、暗黙的に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()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment