简体   繁体   中英

Powershell - Hash Table Escape Quotes

I would like to retrieve a value associated with a specific item in the hash table TEST but its not working. Could you help me?

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

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

Write-Host $a

also try:

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

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

and:

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

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

Write-Host $a

Thanks a lot!

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

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

The problem is that you not passing a string to your hash table try something like this:

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

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

Write-Host $a

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM