PowerShell copy an array completely

后端 未结 5 715
别那么骄傲
别那么骄傲 2020-12-01 22:23

I\'m trying to create a complete copy of an existing array. Every time I try this it doesn\'t seem to work. The thing is that I\'m modifying the Object names inside the new

5条回答
  •  天涯浪人
    2020-12-01 23:03

    Since Powershell 3.0, same approach as Jaco's answer but using PSSerializer.
    It uses a CliXML format compatible with Export-Clixml & Import-Clixml and personally I find it easier to read.
    In theory, supports a nested hierarchy up to [int32]::MaxValue levels-deep

    #   Original data
    $FruitsOriginal     =    Get-Fruits
    #   Serialize and Deserialize data using PSSerializer:
    $_TempCliXMLString  =   [System.Management.Automation.PSSerializer]::Serialize($FruitsOriginal, [int32]::MaxValue)
    $FruitsNew          =   [System.Management.Automation.PSSerializer]::Deserialize($_TempCliXMLString)
    #   Deep copy done.
    

提交回复
热议问题