從 Get-Content 管道傳輸所有內容的正確型別是什么?
我的腳本:
param(
[Parameter(ValueFromPipeline)]
[???]$content
)
Write-Output $content
根據 PowerShell 5.1 的檔案,Get-Content 回傳“物件集合”,但我不確定如何在 PowerShell 中指定它。如果不為 指定型別,則[???]僅輸出檔案的最后一行。
uj5u.com熱心網友回復:
關于你最后的評論:
當我指定
[string]or[string[]]時,它仍然只列印出檔案的最后一行。這與缺少行程塊有關嗎?
這是正確的,否則你的函式、腳本塊或腳本在end塊中執行,如果你希望它們能夠處理來自管道的輸入,你必須在process塊中添加你的邏輯。
請注意,這假設您要使用ValueFromPipeline實際上將其轉換為高級功能的功能。
如果您希望它能夠處理來自管道的輸入,但也與您將使用的位置系結和命名引數[string[]]以及回圈兼容:
param(
[Parameter(ValueFromPipeline)]
[string[]] $Content
)
process {
foreach($line in $Content) {
$line
}
}
然后,假設呼叫了上述內容myScript.ps1,您將能夠:
Get-Content .\test.txt | .\myScript.ps1
并且:
.\myScript.ps1 (Get-Content .\test.txt)
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/521947.html
標籤:电源外壳管道
