Last active
August 29, 2015 14:01
-
-
Save aetos382/380826e24a2672e27944 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
filter Filter-Sample1 { | |
Write-Host "process: 複数回実行されます: $_" | |
} | |
filter Filter-Sample2 { | |
param($X) | |
Write-Host "process: 複数回実行されます: $_; $X" | |
} | |
1,2,3 | Filter-Sample1 | |
4,5,6 | Filter-Sample2 -X ほげ |
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 Pipeline-Sample { | |
param( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | |
[int[]] $X) | |
begin { | |
Write-Host 'begin: 一回だけ実行されます' | |
} | |
process { | |
@($X) | % { | |
Write-Host "process: 複数回実行されます: $_" | |
} | |
} | |
end { | |
Write-Host 'end: 一回だけ実行されます' | |
} | |
} | |
Write-Host '=== パイプライン経由で実行 ===' | |
1,2,3 | Pipeline-Sample | |
Write-Host '=== 引数経由で実行 ===' | |
Pipeline-Sample 4,5,6 |
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 Using-Resource { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory, Position = 0, ValueFromPipeline)] | |
[int[]] $X) | |
begin { | |
$resource = New-Object PSCustomObject | | |
Add-Member ScriptMethod Initialize { Write-Host '初期化処理をしたつもり' } -PassThru | | |
Add-Member ScriptMethod CleanUp { Write-Host 'クリーンアップ処理をしたつもり' } -PassThru | |
$resource.Initialize() | |
function CleanUp-Resource { | |
$resource.CleanUp() | |
} | |
} | |
process { | |
@($X) | % { | |
try { | |
Write-Host "処理中:$_" | |
if ($_ -lt 0) { | |
throw 'あかん' | |
} | |
Write-Host "処理完了:$_" | |
} | |
catch { | |
CleanUp-Resource | |
throw | |
} | |
} | |
} | |
end { | |
CleanUp-Resource | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment