Wednesday, January 22, 2014

Comparing hashtables in powershell

Problem
I have two sets of strings specifying tablenames coming from two sources; one in an excel spreadsheet, the other in the code. I'm basically verifying that the tables match. This checks if the keys in one hashtable belong in the other hashtable, and vice versa. It will output whatever tables don't match.

Solution
$excelHash = @{"Table1"=1; "Table2"=1;}
$codeHash = @{"Table1"=1;"Table3"=1;}

foreach ($h in $excelHash.GetEnumerator())
{
    if (!$codeHash.ContainsKey($h.key))
    {
     "Table in excel but not in the code: $($h.key)" | ft
    }
}
foreach($h in $codeHash.GetEnumerator())
{
    if (!$excelHash.ContainsKey($h.key))
    {
     "Table in code but not in the excel: $($h.key)" | ft
    }
}

No comments:

Post a Comment

There was an error in this gadget