파이프 라인 입력을 허용하는 PowerShell 스크립트를 작성하려면 어떻게해야합니까?
파이프 라인 입력을받을 수있는 (그리고 그렇게 할 것으로 예상되는) PowerShell 스크립트를 작성하려고하지만 다음과 같이 시도합니다.
ForEach-Object {
# do something
}
다음과 같이 명령 줄에서 스크립트를 사용할 때 실제로 작동하지 않습니다.
1..20 | .\test.ps1
방법이 있습니까?
참고 : 기능과 필터에 대해 알고 있습니다. 이것은 내가 찾고있는 것이 아닙니다.
이것은 작동하며 아마도 다른 방법이 있습니다.
foreach ($i in $input) {
$i
}
17:12:42 PS> 1..20 | . \-cmd를 input.ps1
1
2
3
- 가위질 -
18
19
20
"powershell $ input 변수"를 검색하면 자세한 정보와 예제를 찾을 수 있습니다.
몇 가지 :
PowerShell 함수 및 필터 PowerShell Pro!
( "PowerShell 특수 변수"$ input "사용" "섹션 참조)
"스크립트, 함수 및 스크립트 블록은 모두 수신 파이프 라인의 요소에 대한 열거자를 제공하는 $ input 변수에 액세스 할 수 있습니다. "
또는
$ input gotchas«Dmitry의 PowerBlog PowerShell 및 그 이상
"... 기본적으로 보유한 파이프 라인에 대한 액세스를 제공하는 열거 자에 $ input."
PS 명령 줄의 경우
DOS 명령 줄이
아닌
Windows 명령 프로세서.
v2에서는 파이프 라인 입력 (propertyName 또는 byValue 기준)을 수락하고 매개 변수 별칭 등을 추가 할 수도 있습니다.
function Get-File{
param(
[Parameter(
Position=0,
Mandatory=$true,
ValueFromPipeline=$true,
ValueFromPipelineByPropertyName=$true)
]
[Alias('FullName')]
[String[]]$FilePath
)
process {
foreach($path in $FilePath)
{
Write-Host "file path is: $path"
}
}
}
# test ValueFromPipelineByPropertyName
dir | Get-File
# test ValueFromPipeline (byValue)
"D:\scripts\s1.txt","D:\scripts\s2.txt" | Get-File
- or -
dir *.txt | foreach {$_.fullname} | Get-File
다음과 같이 함수의 특별한 경우 인 필터를 작성할 수 있습니다.
filter SquareIt([int]$num) { $_ * $_ }
또는 다음과 같은 유사한 함수를 만들 수 있습니다.
function SquareIt([int]$num) {
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
$_ * $_
}
End {
# Executes once after last pipeline object is processed
}
}
위의 내용은 대화 형 함수 정의로 작동하거나 스크립트에서 전역 세션 (또는 다른 스크립트)에 점으로 들어갈 수있는 경우 작동합니다. 그러나 귀하의 예제는 스크립트를 원한다고 표시했기 때문에 여기에서 직접 사용할 수있는 스크립트에 있습니다 (도팅 필요 없음).
--- Contents of test.ps1 ---
param([int]$num)
Begin {
# Executes once before first item in pipeline is processed
}
Process {
# Executes once for each pipeline object
$_ * $_
}
End {
# Executes once after last pipeline object is processed
}
PowerShell V2에서는 컴파일 된 cmdlet에있는 것과 동일한 매개 변수 바인딩 기능이 포함 된 함수를 포함하는 "고급 함수"로 약간 변경됩니다. 차이점의 예는 이 블로그 게시물 을 참조하십시오 . 또한이 고급 함수의 경우 파이프 라인 개체에 액세스하는 데 $ _를 사용하지 않습니다. 고급 기능을 사용하면 파이프 라인 개체가 cmdlet에서와 마찬가지로 매개 변수에 바인딩됩니다.
다음은 파이프 입력을 사용하는 스크립트 / 함수의 가능한 가장 간단한 예입니다. 각각은 "echo"cmdlet에 대한 파이프와 동일하게 작동합니다.
스크립트로 :
# Echo-Pipe.ps1 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
}
# Echo-Pipe2.ps1
foreach ($i in $input) {
$i
}
기능으로 :
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
}
}
예
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
'IT TIP' 카테고리의 다른 글
Bootstrap 4 반응 형 테이블은 너비를 100 % 차지하지 않습니다. (0) | 2020.10.20 |
---|---|
자기를 사용합니다. (0) | 2020.10.20 |
MySQL은 전체 열에서 모든 공백을 제거합니다. (0) | 2020.10.20 |
두 시간 사이에 몇 분이 있는지 계산 (0) | 2020.10.20 |
D3로 SVG 요소 Z- 색인 업데이트 (0) | 2020.10.20 |