Merging hashtables in PowerShell: how?

前端 未结 12 1910
猫巷女王i
猫巷女王i 2021-01-07 18:26

I am trying to merge two hashtables, overwriting key-value pairs in the first if the same key exists in the second.

To do this I wrote this function which first remo

12条回答
  •  别那么骄傲
    2021-01-07 19:05

    To 'inherit' key-values from parent hashtable ($htOld) to child hashtables($htNew), without modifying values of already existing keys in the child hashtables,

    function MergeHashtable($htOld, $htNew)
    {
        $htOld.Keys | %{
            if (!$htNew.ContainsKey($_)) {
                $htNew[$_] = $htOld[$_];
            }
        }
        return $htNew;
    }
    

    Please note that this will modify the $htNew object.

提交回复
热议问题