Powershell - Hash Table Escape Quotes

时间:2013-07-31 08:36:15

标签: powershell hash hashtable powershell-v2.0 powershell-v3.0

我想检索与哈希表TEST中的特定项相关联的值 但它不起作用。你能帮帮我吗?

$TEST= 
@{"Monday" = "A"; "Tuesday" = "B"; "Wednesday" = "C"; "Thursday" = "D"; "Friday" = "F"}

$Date = Get-Date
$a = $TEST.Get_Item($Date.get_DayOfWeek())

Write-Host $a

也尝试:

$TEST= 
@{"Monday" = "A"; "Tuesday" = "B"; "Wednesday" = "C"; "Thursday" = "D"; "Friday" = "F"}

$Date = Get-Date
$a = $Date.get_DayOfWeek()
$b = TEST.Get_Item($a)

$TEST= 
@{Monday = "A"; Tuesday = "B"; Wednesday = "C"; Thursday = "D"; Friday = "F"}

$Date = Get-Date
$a = $TEST.Get_Item($Date.get_DayOfWeek())

Write-Host $a

非常感谢!

2 个答案:

答案 0 :(得分:2)

DayOfWeek是一个不是字符串的对象,试试这个:

$TEST[(Get-Date).DayOfWeek.ToString()]

答案 1 :(得分:1)

问题是你没有将字符串传递给你的哈希表尝试这样的事情:

$TEST= 
@{"Monday" = "A"; "Tuesday" = "B"; "Wednesday" = "C"; "Thursday" = "D"; "Friday" = "F"}

$Date = Get-Date
$a = $TEST[""+$Date.get_DayOfWeek()]

Write-Host $a