1

我想检索与哈希表 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

非常感谢!

4

2 回答 2

2

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

$TEST[(Get-Date).DayOfWeek.ToString()]
于 2013-07-31T08:52:06.010 回答
1

问题是您没有将字符串传递给哈希表,请尝试以下操作:

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

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

Write-Host $a
于 2013-07-31T08:47:49.187 回答