Last active
January 29, 2016 06:52
-
-
Save aetos382/6128df251617ac5bea99 to your computer and use it in GitHub Desktop.
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
最後に実行したコマンドの結果を $0 にとっておく関数。 | |
$PROFILE に書いておこう。 | |
$PSDefaultParameterValues = @{ '*:OutVariable' = '0' } | |
ってのが実現したいこと。なんだけど、この方法では $0 は 1 回しか表示できない。 | |
おそらく、$0 と打つのは(Out-Default が再定義されていなければ)$0 | Out-Host を暗黙に指定したことになる。 | |
Out-Host は -OutVariable パラメーターを持つくせに、何も出力しないので、$0 | Out-Host を 1 回やると $0 がクリアされてしまう。 | |
この Out-Default 関数なら、そうはなりません。 | |
以下の記事を参考にさせて頂きました。 | |
http://stknohg.hatenablog.jp/entry/2016/01/11/151201 |
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 Out-Default { | |
[CmdletBinding()] | |
param( | |
[Parameter(ValueFromPipeline)] | |
[PSObject] $InputObject, | |
[Switch] $Transcript) | |
begin { | |
$wrappedCmdlet = $ExecutionContext.InvokeCommand.GetCmdlet('Microsoft.PowerShell.Core\Out-Default') | |
$sb = { & $wrappedCmdlet @PSBoundParameters } | |
$p = $sb.GetSteppablePipeline() | |
$p.Begin($PSCmdlet) | |
$outputs = @() | |
} | |
process { | |
$p.Process($_) | |
if ($_) { | |
$outputs += $_ | |
} | |
} | |
end { | |
$p.End() | |
if ($outputs) { | |
$global:0 = $outputs | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment