Loading a PowerShell hashtable from a file?

前端 未结 6 1191
粉色の甜心
粉色の甜心 2020-12-11 14:45

I\'ve got a file containing some data in PowerShell Object Notation:

@{ X = \'x\'; Y = \'y\' }

I\'d like to load this into a variable from

6条回答
  •  余生分开走
    2020-12-11 15:31

    I ran into trouble using ConvertFrom-StringData as @Chad suggested. If you do:

    $hash = get-content .\test.txt | ConvertFrom-StringData
    

    I found I had an object array rather than a hash table. In fact, it appears that I had an array of hash tables, each with one entry. I confirmed with a:

    $hash.GetType()
    

    It looks like you need to join each line of the slurped input file to ensure that it forms a single string for ConvertFrom..'s use:

    $hash = ((get-content .\test.txt) -join '`n') | ConvertFrom-StringData
    

提交回复
热议问题