Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Requires -Version 5.0
- function ConvertTo-PSObject {
- <#
- .SYNOPSIS
- Converts an (array of) object of any type into a (deserialized) representation of those same objects.
- .DESCRIPTION
- The purpose of this function is to deserialize a set of objects. Deserializing an object "detaches" an object
- from its source such that the properties of the object can be changed without affecting the original. It basically
- makes a copy of the object detached from the original. This also means methods of the original object cannot be
- used.
- .PARAMETER InputObject
- The object or set of objects to deserialize.
- .PARAMETER Depth
- Specifies how many levels of contained objects are included in the XML representation. The default value is 2.
- .PARAMETER Skip
- Ignores the specified number of objects and then gets the remaining objects. Enter the number of objects to skip.
- .PARAMETER First
- Gets only the specified number of objects. Enter the number of objects to get.
- .EXAMPLE
- ConvertTo-PSObject -InputObject 'Value1', 'Value2'
- .EXAMPLE
- 'Value1', 'Value2' | ConvertTo-PSObject -First 1
- .INPUTS
- Any
- .OUTPUTS
- PSObject
- .NOTES
- Normally one might use ConvertTo-Json and ConvertFrom-Json in a pipeline to do the same thing this
- function does, but I've found there to be an issue where many times when trying to use JSON format that
- an error will result, complaining about duplicate properties. This gets around that issue.
- Author: Dale Thompson
- Website: none
- Twitter: @UnitedDale
- #>
- <#PSScriptInfo
- .VERSION 1.1
- .GUID 4c100417-816b-4b25-b0c4-a019f021e5e6
- .AUTHOR Dale Thompson
- .COMPANYNAME WSP
- .COPYRIGHT All rights reserved
- .TAGS Deserialize PSObject
- .RELEASENOTES
- #>
- [CmdletBinding()]
- [OutputType('PSObject')]
- param (
- [Parameter(Mandatory, ValueFromPipeline)][object[]]$InputObject,
- [int32]$Depth,
- [uint64]$Skip,
- [uint64]$First
- )
- BEGIN {
- [System.IO.FileInfo]$TempFile = New-TemporaryFile
- [hashtable]$ExportParams = @{ LiteralPath = $TempFile.FullName }
- 'Depth' | % {
- if ($PSBoundParameters[$_]) { $ExportParams[$_] = $PSBoundParameters[$_] }
- }
- [hashtable]$ImportParams = @{ LiteralPath = $TempFile.FullName }
- 'Skip', 'First' | % {
- if ($PSBoundParameters[$_]) { $ImportParams[$_] = $PSBoundParameters[$_] }
- }
- [System.Collections.Generic.List``1[object]]$Objects = @()
- }
- PROCESS {
- foreach ($Object in $InputObject) {
- [void]$Objects.Add($Object)
- }
- }
- END {
- $Objects | Export-Clixml @ExportParams
- Import-Clixml @ImportParams
- Remove-Item $TempFile
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement