Question : Hashtable Import/Merge/Export Powershell

This is something I've attempted to do before, but did not succeed.  Now I'm asking for something pretty interesting and I haven't seen much written about it on Google or EE.

Simply put, I want to make a hash table with values like "John Smith", "2" and "Jane Doe", "4".  It starts by taking a survey of all the exchange mailboxes and whoever is over a certain amount of MBs, flags that user name and adds it to a temporary list.  This can be an array since it's only a bunch of names.  No numbers yet.  Here's where it gets interesting.  There will be a text file or csv, whichever is easier in a location accessible by the script.  Let's call it "C:\HashTable.csv".  It imports all the values from Hashtable.csv into a hashtable.  Then it goes down the line and says "If a person exists in this hash table, increment their corresponding value by 1".  Otherwise, just add them to the list, and give the key a value of 1.  So the key is the persons name (because it's unique), and the value is how many days we've found them full on the server.

I tried to use a text file that outputs their names into it, and then counts it all up.  That works but after a while the text file gets so sloppy, it's hard to read it.  And it also pretty large after a few days.

Hopefully someone can cook something up.  The hardest part I'm having is exporting the hashtable.  Once I export it, I suppose it would be easy to import it.  And even easier to do a "Does key exist" on the table, then if they do, increment their value by 1.  Should be a cakewalk, right?

This has been something plaguing me for a few months and I shelved it because I got sidetracked with other projects.  So whoever can fix this will get my 2000 points + my undying gratitude free of charge.

-Dale Harris

Answer : Hashtable Import/Merge/Export Powershell

I think your best option would be a custom object. You can then use import/export-csv to get the info in and out.

Something like below... you will need to add your own logic for what ever checks you want.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
# Get old list of names from file
$oldnames =  import-csv oldlist.csv
 
# Collection to store custom objects
$myobjs = @()
 
foreach($name in $names)
{
   # Get old count
   $oldcount = $oldnames | ?{$_.name -eq $name} | %{$_.count}
 
   # create custom object
   $myobj = "" | Select Name,Count
   $myobj.name = $name
   $myobj.count = $oldcount + 1
}
 
$myobjs | export-csv oldlist.csv -noType
Open in New Window Select All
Random Solutions  
 
programming4us programming4us