Hash Powershell-无法打印哈希值

Hash Powershell-无法打印哈希值,hash,parameters,powershell-2.0,Hash,Parameters,Powershell 2.0,我正在编写一个简单的脚本,以便更熟悉powershell 此脚本将输入参数读入散列 $states = @($args) $states write-host Color is $states.color 在命令行上,我设置了以下值 $shape = 'circle'; $color = 'pink'; $size = 'large' 然后用以下命令调用该程序 .\shapes_n_colors.ps1 $shape $size $color 我得到以下输出: circle larg

我正在编写一个简单的脚本,以便更熟悉powershell

此脚本将输入参数读入散列

$states = @($args)

$states

write-host Color is $states.color
在命令行上,我设置了以下值

$shape = 'circle'; $color = 'pink'; $size = 'large'
然后用以下命令调用该程序

.\shapes_n_colors.ps1  $shape $size $color
我得到以下输出:

circle
large
pink
Color is
我无法理解为什么$states.color为空。我期待输出“颜色是粉红色”

我在关注这篇文章


我哪里出错了???

不知道从哪里开始

首先-您在任何时候都不会创建哈希
@($args)
没有任何作用:$args已经是一个数组,
@()
只用于确保表达式将生成一个数组。。。散列文字是
@{}

下一步:您的脚本将不知道传递给它的变量使用了什么名称。它将看到三条线。我建议使用
param()
获取命名参数(默认情况下也是位置参数,因此调用脚本不会有太大变化):

当您尝试使用您的语法时,它将产生预期的结果。但是等等,还有更多使用此选项,您无需记住参数顺序即可运行脚本:

.\shapes_n_colors.ps1 -Color White -Shape Circle -Size Small
更不用说,这将为您完成此命名参数

.\shapes_n_colors.ps1 -Color White -Shape Circle -Size Small