Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

powershell - How can I enumerate a hashtable as key-value pairs / filter a hashtable by a collection of key values

Editor's note: This question has a complicated history, but boils down to this:
* To learn how to enumerate the entries of a hashtable by its key-value pairs, see the accepted answer.
* To learn how to filter a hashtable by a collection of key values, see the other answer.


I think I fell into the X Y problem again, my initial question was about Filtering a hash table. I discovered it's easier to filter before creating the hash table. Question answered, right?

Nope, the Y problem was looping each Key and using the Values which @briantist helped me with.

My goal is to loop over the key names, which are timestamps, and schedule a task using the key name as the task name and trigger.

I'm creating a hash table from a CSV file using Group-Object -AsHashTable -AsString -edit, it's worth mentioning here that filtering the CSV before creating the HashTable only makes things easier down the Pipeline or script.

As an example:

Import-CSV (ls -path D: -Filter source*.csv | sort LastWriteTime | Select -Last 1).FullName |
 where {$_.TimeCorrected -ne 'ManualRebootServer'} |
 group TimeCorrected -AsHashTable -AsString

I'm trying to loop over the key names and able to display the key names using:

$var = Import-Csv csv123.csv | Group-Object Value1 -AsHashTable -AsString

foreach ($key in $var.Keys){"The key name is $key"}

#Create a scheduled task named and triggered based on the HashTable keyname
#test test test
foreach ($key in $var.keys){IF($key -ne 'ManualRebootServer'){"Register-ScheduledJob"}}

I'm just not sure how to get the values from the keys I am interested in.

I've found the following works, but only when I enter a Key name manually. I'm just unsure how to combine both loops.

($val.GetEnumerator() | Where {$_.key -eq '06-11-16 18:00'} | ForEach-Object { $_.value }).Server
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have some options here.

Enumerating through keys:

foreach ($key in $var.Keys) {
    $value = $var[$key]
    # or
    $value = $var.$key 
}

Enumerating key-value pairs (which you've discovered, but may not be using effectively):

foreach ($kvp in $var.GetEnumerator()) {
    $key = $kvp.Key
    $val = $kvp.Value
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...