6
해결 방법 :파이프 입력에서 읽는 powershell 함수는 어떻게 작성합니까?
다음은 파이프 입력을 사용하는 함수/스크립트의 가장 간단한 가능한 예입니다. 각각은 "echo"cmdlet으로 파이핑하는 것과 동일하게 작동합니다.
으로 기능 : 스크립트로
Function Echo-Pipe {
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
echo $_
}
End {
# Executes once after last pipeline object is processed
}
}
Function Echo-Pipe2 {
foreach ($i in $input) {
$i
}
}
:
# 에코 Pipe.ps1Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
echo $_
}
End {
# Executes once after last pipeline object is processed
}
# 에코 Pipe2.ps1
foreach ($i in $input) {
$i
}
일예
function set-something {
param(
[Parameter(ValueFromPipeline=$true)]
$piped
)
# do something with $piped
}
하나의 매개 변수가 파이프 라인 입력에 직접 결합 될 수 있음을 명백해야한다 :
PS > . theFileThatContainsTheFunctions.ps1 # This includes the functions into your session
PS > echo "hello world" | Echo-Pipe
hello world
PS > cat aFileWithThreeTestLines.txt | Echo-Pipe2
The first test line
The second test line
The third test line