Updating hash table values in a 'foreach' loop in PowerShell

后端 未结 10 1681
清歌不尽
清歌不尽 2020-12-29 02:03

I\'m trying to loop through a hash table and set the value of each key to 5 and PowerShell gives an error:

$myHash = @{}
$myHash[\"a\"] = 1
$myHash[\"b\"] =          


        
10条回答
  •  北荒
    北荒 (楼主)
    2020-12-29 02:42

    As mentioned in a previous answer, clone is the way to go. I had a need to replace any null values in a hash with "Unknown" nd this one-liner does the job.

    ($record.Clone()).keys | %{if ($record.$_ -eq $null) {$record.$_ = "Unknown"}}
    

提交回复
热议问题