Save hash table in PowerShell object notation (PSON)

二次信任 提交于 2019-11-26 16:45:23

Try the *-CliXml cmdlets. To save the object:

@{            
 "name" = "report 0"            
 "parameters" = @(
    @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4},
    @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'}
    )            
} | Export-Clixml -Path c:\hash.xml

To read it back:

Import-Clixml c:\hash.xml

After 5 years, the cmdlet I had pasted in the original answer has undergone so many updates that it has become completely outdated. Therefore I have replaced the code and the ReadMe with a link to the latest version.

Besides the support for more object types and better formatting, it now outputs an expression (ScriptBlock), which default display type is still a String and allows for direct invocation using the call operator &:

$Object = &($Object | ConverTo-Expression)

ConvertTo-Expression

The ConvertTo-Expression cmdlet can be download from PowerShell Gallery using the command:

Install-Script -Name ConvertTo-Expression

ReadMe

The full ReadMe (and source code) is available from GitHub:
https://github.com/iRon7/ConvertTo-Expression

Installation

After downloading (Install-Script -Name ConvertTo-Expression), the script can simply be invoked by dot sourcing:

. .\ConvertTo-Expression.ps1

You might also consider to convert the script to a PowerShell module by renaming it to a PowerShell module (.psm1) file and moving it to a one of the module folders defined in $env:PSModulePath. For more details see: How to Write a PowerShell Script Module.

Answer

Below are some possible options to serialize the specific example (assigned to $Craig) in the question:

ConvertTo-Expression $Craig
@{
        'name' = 'report 0'
        'parameters' =
                @{
                        'values' =
                                1,
                                2,
                                3,
                                4
                        'default' = 1
                        'name' = 'parameter 0'
                },
                @{
                        'values' =
                                'A',
                                'B',
                                'C'
                        'default' = 'A'
                        'name' = 'parameter 1'
                }
}

To limit the tree view expansion:
(Expand -0 will output a single line and Expand -1 will remove also the unnecessary spaces)

ConvertTo-Expression $Craig -expand 3
@{
        'name' = 'report 0'
        'parameters' =
                @{'values' = 1, 2, 3, 4; 'default' = 1; 'name' = 'parameter 0'},
                @{'values' = 'A', 'B', 'C'; 'default' = 'A'; 'name' = 'parameter 1'}
}

Preserving the explicit types (strong typed):

ConvertTo-Expression $Craig -expand 3 -Strong
[hashtable]@{
        'name' = [string]'report 0'
        'parameters' = [array](
                [hashtable]@{'values' = [array]([int]1, [int]2, [int]3, [int]4); 'default' = [int]1; 'name' = [string]'parameter 0'},
                [hashtable]@{'values' = [array]([string]'A', [string]'B', [string]'C'); 'default' = [string]'A'; 'name' = [string]'parameter 1'}
        )
}

(Note: As per PowerShell design, HashTables are not in order, but if required you might use the [Ordered] type instead.)

One way would be to put the hashtable definition in a scriptblock:

$hashtable = {
  @{            
    "name" = "report 0"            
    "parameters" = @(
        @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4},
        @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'}
        )            
    }
}

$hashtable.tostring()

@{
"name" = "report 0"
"parameters" = @( @{"name" = "parameter 0"; "default" = 1; "values"=1,2,3,4}, @{"name" = "parameter 1"; "default" = 'A'; "values" = 'A','B','C'} )
}

Within the script, you'd need to invoke the script block to instantiate the hashtable:

$hash = .$hashtable

How to use a shorthand "object notation" to generate an object in PowerShell:

$object = New-Object -TypeName PSObject -Property @{name="foo";age=21}

DISCLAIMER: I know this does not answer OP's question directly but it might help folks like me searching for a very similar issue and landing here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!