Powershell - 动态函数调用
对于自定义 Powershell 函数,所有函数输出都会返回。我试图通过编写一个仅返回 $true 或 $false 的包装函数来消除该限制/混乱。
但是,我正在努力处理动态函数调用。 。 。专门传递参数。
请注意,函数名称和函数参数都传递给“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
这在 Powershell v1.0 中可能吗?
谢谢。
With custom Powershell functions all function output is returned. I'm attempting to remove that limitation/confusion by writing a wrapper function that will return just $true or $false.
However, I'm struggling with the dynamic function call . . . specifically passing the arguments.
Note that both the function name and the function arguments are passed to "ExecBoolean".
Code Sample:
# 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
Current Output:
Array Length = 2 , Items = [ String1 1 ]
sTest [ String1 1 ].
iTest [ 0 ].
Result = False
Desired Output:
Array Length = 2 , Items = [ String1 1 ]
sTest [ String1 ].
iTest [ 1 ].
Result = True
Is this possible in Powershell v1.0?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会像这样使用
Invoke-Expression
:请注意:
$args
,恕我直言,它更舒服,因为您不需要将参数作为数组传递。但是,您也可以毫无问题地使用$oArgs
(唯一重要的行是带有Invoke-Expression
的行)。FileInfo
,因为对象在$($args -join ' ') 中转换为字符串
。&
运算符调用 Foo。,由于其局限性,我不会推荐这种方法(但可能其他人会提出更好的解决方案)。
如果您只是想确保 Foo 不返回任何输出,您可以创建一个脚本来解析调用 Foo 的其他脚本,并检查对“Foo”的每次调用都必须以
$null =
开头。如果您切换到PowerShell V2,有更好的方法称为splatting。有关详细信息,请参阅 如何以及为何使用 Splatting(传递 [switch] 参数)
I would use
Invoke-Expression
like this:Note that:
$args
which is imho more comfortable, because you don't to pass the arguments as a array. However, you could use your$oArgs
as well with no problem (the only line that matters is the one withInvoke-Expression
).FileInfo
, because the objects are converted to strings in$($args -join ' ')
.ExecBoolean Foo @{sTest="String1"; iTest=1}
. So you would pass parameters in a hashtable. You would need to alter Foo to accept hashtable of course. In this case you could pass any type in, because you could call Foo using&
operator.I would not recommend this approach, because of the limitations (but it can happen that maybe others will come up with better solution).
If you just want to ensure that no output is returned from Foo, you might create a script that parses other scripts that call Foo and checks that every call to "Foo" must be prepended with
$null =
.If you switch to PowerShell V2, there is better way called splatting. For more info look at How and Why to Use Splatting (passing [switch] parameters)
在这里,您只向函数传递一个参数(一个数组),因此
Foo
中的第二个参数默认为0
。如果您只想丢弃语句/表达式的输出,有多种方法可以做到这一点,例如通过管道将其传递到
Out-Null
或“强制转换”为void
。Here you're passing the function only one argument —an array— so the second parameter in
Foo
defaults to0
.If you just want to discard output from a statement/expression, there are ways to do so, like piping it to
Out-Null
or "casting" tovoid
.就我个人而言,我只会使用:
这会将函数的结果强制转换为布尔值。
稍后它会更具可读性。
希望这有帮助
Personally, I would just use:
This will coerce the results of the function into a boolean.
It's much more readable later on.
Hope this helps