如何返回使用New-Variable创建的新变量的长度

时间:2015-04-17 19:53:08

标签: powershell

我正在编写一个脚本,根据需要创建新变量,我让它们正常工作。我现在需要的是返回那些变量的长度。

这当然会返回' 6'

的值
$strText = "Yellow"
$strText.length

但在这里,我遇到了麻烦。这将返回' 1.length'

$bar = 1
New-Variable "foo$bar" "hello"
write-host $foo$bar.length

然后返回' foo1.length'

$bar = 1
New-Variable "foo$bar" "hello"
write-host foo$bar.length

我显然正在寻找' 5'在这种情况下。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您可以使用Get-Variable cmdlet

write-host (Get-Variable foo$bar -Value).length

演示:

PS > $bar = 1
PS > New-Variable "foo$bar" "hello"
PS > write-host (Get-Variable foo$bar -Value).length
5
PS >