PowerShell V2의 -verbose 스위치에서 Verbose 출력 캡처
-
21-12-2019 - |
문제
나는 오늘 흥미로운 문제로 이루어졌습니다.상세한 스트림에서 출력을 캡처하고 stdout에 씁니다.
이 작업을 수행 할 수 있습니다 :
# Create a PowerShell Command
$pscmd = [PowerShell]::Create()
# Add a Script to change the verbose preference.
# Since I want to make sure this change stays around after I run the command I set UseLocalScope to $false.
# Also note that since AddScript returns the PowerShell command, I can simply call Invoke on what came back.
# I set the return value to $null to suppress any output
$null = $psCmd.AddScript({$VerbosePreference = "Continue"},$false).Invoke()
# If I added more commands, I'd be adding them to the pipeline, so I want to clear the pipeline
$psCmd.Commands.Clear()
# Now that I've cleared the pipeline, I'll add another script that writes out 100 messages to the Verbose stream
$null = $psCmd.AddScript({Write-verbose "hello World" }).Invoke()
# Finally, I'll output the stream
$psCmd.Streams.Verbose
.
이제 흥미로운 부분은 generacodicicetagcode라는 함수를 만들고 Hello-World
를 사용하여 -verbose 스위치를 상속하는 경우 더 이상 출력을 캡처 할 수 없습니다.
Function Hello-World {
[CmdletBinding()]
Param()
Write-Verbose "hello world"
}
...
$null = $psCmd.AddScript({Hello-World -Verbose }).Invoke()
...
.
저는 그 기능이 자체 자리한 흐름이 주어지고 스트림에 대한 가시성이 손실되지만 긍정적이지는 않습니다.이것은 [CmdletBinding()]
와 관련이 있습니까?
성적 증명서를 피하고이 작업을 수행하는 방법이 있습니까?
감사합니다!
해결책
고맙습니다 @jasonmorgan, 아래의 솔루션은 아래에있는 솔루션입니다.나는 내가 만든 pscmd에서 함수를 선언해야했습니다 :
$pscmd = [PowerShell]::Create()
$null = $psCmd.AddScript({$VerbosePreference = "Continue"},$false).Invoke()
$null = $psCmd.AddScript({
function Hello-World {
[CmdletBinding()]
Param()
Write-Verbose "hello world"
}
}, $false).Invoke()
$psCmd.Commands.Clear()
$null = $psCmd.AddScript({Hello-World -Verbose }).Invoke()
$psCmd.Streams.Verbose
. 제휴하지 않습니다 StackOverflow