I have a problem in a PowerShell script:
我在PowerShell脚本中遇到问题:
When I want to pass a Hashtable to a function, this hashtable is not recognized as a hashtable.
当我想将Hashtable传递给函数时,此哈希表不会被识别为哈希表。
function getLength(){
param(
[hashtable]$input
)
$input.Length | Write-Output
}
$table = @{};
$obj = New-Object PSObject;$obj | Add-Member NoteProperty Size 2895 | Add-Member NoteProperty Count 5124587
$table["Test"] = $obj
$table.GetType() | Write-Output ` Hashtable
$tx_table = getLength $table `Unable to convert System.Collections.ArrayList+ArrayListEnumeratorSimple in System.Collections.Hashtable
Why?
2 个解决方案
#1
$Input
is an automatic variable that enumerates the input given.
$ Input是一个自动变量,用于枚举给定的输入。
Chose any other variable name and it'll work - although not necessarily as you might expect - to get the number of entries in a hashtable you need to inspect the Count
property:
选择任何其他变量名称,它将起作用 - 尽管不一定如您所料 - 要获取散列表中的条目数,您需要检查Count属性:
function Get-Length {
param(
[hashtable]$Table
)
$Table.Count
}
Write-Output
is implied when you just leave the $Table.Count
as is.
当您只保留$ Table.Count时,隐含写入输出。
Also, the ()
suffix in the function name is unnecessary syntactic sugar with zero meaning when you declare your parameters inline with Param()
- drop it
此外,当您使用Param()内联声明参数时,函数名称中的()后缀是不必要的语法糖,意义为零 - 删除它
#2
I'm not really sure what to comment here, it seems self-explanatory. If not, leave a comment and I'll clarify.
我不太确定在这里发表什么评论,这似乎是不言自明的。如果没有,请发表评论,我会澄清。
$ExampleHashTable = @{
"one" = "the loneliest number"
"two" = "just as bad as one"
}
Function PassingAHashtableToAFunctionTest {
param(
[hashtable] $PassedHashTable,
[string] $AHashTableElement
)
Write-Host "One is ... "
Write-Host $PassedHashTable["one"]
Write-Host "Two is ... "
Write-Host $AHashTableElement
}
PassingAHashtableToAFunctionTest -PassedHashTable $ExampleHashTable `
-AHashTableElement $ExampleHashTable["two"]
Output:
One is ...
the loneliest number
Two is ...
just as bad as one
#1
$Input
is an automatic variable that enumerates the input given.
$ Input是一个自动变量,用于枚举给定的输入。
Chose any other variable name and it'll work - although not necessarily as you might expect - to get the number of entries in a hashtable you need to inspect the Count
property:
选择任何其他变量名称,它将起作用 - 尽管不一定如您所料 - 要获取散列表中的条目数,您需要检查Count属性:
function Get-Length {
param(
[hashtable]$Table
)
$Table.Count
}
Write-Output
is implied when you just leave the $Table.Count
as is.
当您只保留$ Table.Count时,隐含写入输出。
Also, the ()
suffix in the function name is unnecessary syntactic sugar with zero meaning when you declare your parameters inline with Param()
- drop it
此外,当您使用Param()内联声明参数时,函数名称中的()后缀是不必要的语法糖,意义为零 - 删除它
#2
I'm not really sure what to comment here, it seems self-explanatory. If not, leave a comment and I'll clarify.
我不太确定在这里发表什么评论,这似乎是不言自明的。如果没有,请发表评论,我会澄清。
$ExampleHashTable = @{
"one" = "the loneliest number"
"two" = "just as bad as one"
}
Function PassingAHashtableToAFunctionTest {
param(
[hashtable] $PassedHashTable,
[string] $AHashTableElement
)
Write-Host "One is ... "
Write-Host $PassedHashTable["one"]
Write-Host "Two is ... "
Write-Host $AHashTableElement
}
PassingAHashtableToAFunctionTest -PassedHashTable $ExampleHashTable `
-AHashTableElement $ExampleHashTable["two"]
Output:
One is ...
the loneliest number
Two is ...
just as bad as one