我有以下代码示例。我只是想从哈希表键中打印值。
function New-Article()
{
param ($volume, $issue, $title)
$article = @{}
$article.volume = $volume
$article.issue = $issue
$article.title = $title
return $article
}
$article = New-Article(1, 2, "Article Title")
Write-Host "Article title: $article.title" # Output = Article title: System.Collections.Hashtable.title
Write-Host "Article title: $($article.title)" # Output = Article title:
Write-Host "Article volume: $($article.volume)" # Output = Article volume: 1 2 Article Title
$article = New-Article 1, 2, "Article Title"
Write-Host "Article title: $($article.title)" # Output = Article title:
编辑添加了一行代码来测试可能重复项中的内容(与属性有关,而不是哈希表)
编辑添加了更多基于评论和答案的示例
答案 0 :(得分:3)
您有2个问题。除了将哈希表属性访问表达式括在$()中之外,您还错误地调用了该函数。在powershell中,参数传递给一个用空格分隔的函数,不带括号:
function New-Article()
{
param ($volume, $issue, $title)
$article = @{}
$article.volume = $volume
$article.issue = $issue
$article.title = $title
return $article
}
$article = New-Article 1 2 "Article Title"
Write-Host "Article title: $($article.title)"