सोल्वेड:पाइप किए गए इनपुट से पढ़ने वाले पावरहेल फ़ंक्शन को आप कैसे लिखते हैं?
निम्नलिखित पाइप इनपुट का उपयोग करने वाले कार्यों/स्क्रिप्ट के सबसे सरल संभव उदाहरण हैं। प्रत्येक "गूंज" 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