사용자 지정 Powershell 함수를 사용하면 모든 함수 출력이 반환됩니다. 난 그냥 $ true 또는 $ false를 반환합니다 래퍼 함수를 작성하여 그 한계/혼란을 제거하려고합니다.Powershell - 동적 함수 호출
그러나 동적 함수 호출에 어려움을 겪고 있습니다. . . 구체적으로 인수를 전달합니다.
함수 이름과 함수 인수는 모두 "ExecBoolean"에 전달됩니다.
코드 샘플 :
# Simplifies calls to boolean functions in Powershells scripts
# Helps solve this problem -> http://www.vistax64.com/powershell/107328-how-does-return-work-powershell-functions.html
function Foo([string]$sTest, [int] $iTest)
{
Write-Host "sTest [" $sTest "]."
Write-Host "iTest [" $iTest "]."
if ($iTest -eq 1)
{
return $true
}
else
{
return $false
}
}
function ExecBoolean ([string] $sFunctionName, [array] $oArgs)
{
Write-Host "Array Length = " $oArgs.Length ", Items = [" $oArgs[0..($oArgs.Length - 1)] "]"
$oResult = & $sFunctionName $oArgs[0..($oArgs.Length - 1)]
# Powershell returns all function output in oResult, just get the last item in the array, if necessary.
if ($oResult.Length -gt 0)
{
return $oResult[$oResult.Length - 1]
}
else
{
return $oResult
}
}
$oResult = ExecBoolean "Foo" "String1", 1
Write-Host "Result = " $oResult
전류 출력 :
Array Length = 2 , Items = [ String1 1 ]
sTest [ String1 1 ].
iTest [ 0 ].
Result = False
원하는 출력 :
Array Length = 2 , Items = [ String1 1 ]
sTest [ String1 ].
iTest [ 1 ].
Result = True
는 파워 쉘 v1.0을이 수 있습니까?
감사합니다. 여기
감사합니다. 예, 무슨 일이 일어 났는지 이해합니다. 나는 $ oArgs [0 .. ($ oArgs.Length - 1)]가 배열로 간주되지 않고 0, 1, 2 등의 인수리스트로 생각할 것이라고 생각했다.// "out-null "트릭을하지만 개발자가 사용하지 않거나 어떤 명령문이 출력을 반환 하는지를 모르는 경우 발생하는 문제를 피하려고합니다. – MattH