我有以下代码:
cls
$input = 'Hello World'
Write-Host '$input.GetType() = ' + $input.GetType()
function some-string-function($input)
{
Write-Host 'In the function now'
Write-Host '$input.GetType() = ' + $input.GetType()
Write-Host '$input (value) = ' + $input
return $input.length
}
$result = some-string-function $input
"`$result = $result"
我得到以下信息:
$input.GetType() = + System.String
In the function now
$input.GetType() = + System.Collections.ArrayList+ArrayListEnumeratorSimple
$input (value) = + System.Collections.ArrayList+ArrayListEnumeratorSimple
$result =
有人可以指出如何将字符串传递给 PowerShell 中的函数吗?
最佳答案
作为我的评论的澄清 $input
是一个保留的自动变量。
如果您在 some-string-function
中使用另一个命名变量更改它,您将获得预期的行为。
关于string - 将字符串类型传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13627871/