PSバージョン: 5.1
望ましい
私が望んでいたことはうまくいくだろう$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param([parameter(ValueFromPipeline=$true)]$x)
begin {write-host 'begin'}
process{write-host "$($this.con): $x"}
end {write-host 'end'}
}
1..5 | $obj.MyMethod
与える
begin
local: 1
local: 2
local: 3
local: 4
local: 5
end
しかし、エラーが発生します
At line:1 char:8
+ 1..5 | $obj.MyMethod
+ ~~~~~~~~~~~~~
Expressions are only allowed as the first element of a pipeline.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
試した
何がうまくいかないのか: idk, tl;dr 理由?1..5 | %{$obj.MyMethod($_)}
何が機能しないか: begin/end
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param($x)
write-host "$($this.con): $x"
}
1..5 | %{$obj.MyMethod($_)}
何が機能しないか: $this はもはや添付されていません
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
MyMethodSB = {
param()
begin {write-host 'begin'}
process{write-host "$($obj.con): $_"}
end {write-host 'end'}
}
}
1..5 | &$obj.MyMethodSB
うまくいかない理由: idk、ステッパーのスコープが時期尚早に終了する可能性があります
$obj = [PSCustomObject]@{
PSTypeName = 'MyObject'
con = 'local'
Stepper = $null
}
add-member -MemberType ScriptMethod -InputObject $obj -Name MyMethod -Value {
param($x)
if($null -eq $this.Stepper){
$t = $this
$sb = {
param($o)
begin {write-host 'begin'}
process{write-host "$($o.con): $_"}
end {write-host 'end'}
}
$this.Stepper = {&$sb $t}.GetSteppablePipeline()
$this.Stepper.Begin($true)
}
$this.Stepper.Process($x)
}
1..5 | %{$obj.MyMethod($_)}
$obj.Stepper.End()
$obj.Stepper = $null
もうすぐそこ
何がちょっと働いているのですか$Data = @{
ItemNo1 = @(
@{loc = 'WH'; qty = 20}
@{loc = 'DK'; qty = 0}
@{loc = 'ST1'; qty = 3}
@{loc = 'ST2'; qty = 2}
)
ItemNo2 = @(
@{loc = 'WH'; qty = 6}
@{loc = 'DK'; qty = 0}
)
ItemNo3 = @(
@{loc = 'WH'; qty = 100}
@{loc = 'ST1'; qty = 5}
@{loc = 'DK'; qty = 0}
)
ItemNo4 = @(
@{loc = 'WH'; qty = 0}
@{loc = 'DK'; qty = 0}
@{loc = 'ST2'; qty = 15}
)
ItemNo5 = @(
@{loc = 'WH'; qty = 0}
@{loc = 'DK'; qty = 15}
)
}
$QueryParameter = [PSCustomObject]@{Value = $null}
function MockDataQuery{
$Item = $QueryParameter.Value
foreach($iDat in $Data.$Item){
" $Item;$($iDat.loc);$($iDat.qty)"
}
}
$InvObj = [PSCustomObject]@{
PSTypeName = 'Data.Connection'
con = 'item.inventory'
}
add-member -MemberType ScriptMethod -InputObject $InvObj -Name FetchItemData -Value {
$p = $this
$qPipe = [PSCustomObject]@{
PSTypeName = 'Data.Connection.QueryPipe'
parent = $p
qHeader = 'Item;Location;Quantity'
query = 'select * from inventory where item = ?'
param = $null
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name begin -Value {
param([bool]$Header=$true)
write-host "connect to: $($this.parent.con)"
write-host "compile command: $($this.query)"
$this.param = $QueryParameter
write-host 'begin transaction'
if($Header){
write-host " $($this.qHeader)"
}
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name process -Value {
param($itm)
$this.param.Value = $itm
MockDataQuery | write-host
}
add-member -MemberType ScriptMethod -InputObject $qPipe -Name end -Value {
write-host 'end transaction'
write-host 'dispose command'
write-host 'close connection'
}
return $qPipe
}
1..5 | %{"ItemNo$_"} | &{
begin {$stp = $InvObj.FetchItemData();$stp.begin()}
process{$stp.process($_)}
end {$stp.end()}
}
これは機能モデルのように見えますが、接続オブジェクトのユーザーがオブジェクトのメソッドを使用するたびに開始/処理/終了スクリプトをコーディングする必要がないソリューションがあれば、より良いでしょう。 .