Skip to content

Instantly share code, notes, and snippets.

@aetos382
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aetos382/380826e24a2672e27944 to your computer and use it in GitHub Desktop.
Save aetos382/380826e24a2672e27944 to your computer and use it in GitHub Desktop.
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 ほげ
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
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