Advertisement
Old-Lost

ConvertTo-PSObject

Mar 23rd, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Requires -Version 5.0
  2. function ConvertTo-PSObject {
  3. <#
  4. .SYNOPSIS
  5.     Converts an (array of) object of any type into a (deserialized) representation of those same objects.
  6.  
  7. .DESCRIPTION
  8.     The purpose of this function is to deserialize a set of objects. Deserializing an object "detaches" an object
  9.     from its source such that the properties of the object can be changed without affecting the original. It basically
  10.     makes a copy of the object detached from the original. This also means methods of the original object cannot be
  11.     used.
  12.  
  13. .PARAMETER InputObject
  14.     The object or set of objects to deserialize.
  15.    
  16. .PARAMETER Depth
  17.     Specifies how many levels of contained objects are included in the XML representation. The default value is 2.
  18.  
  19. .PARAMETER Skip
  20.     Ignores the specified number of objects and then gets the remaining objects. Enter the number of objects to skip.
  21.    
  22. .PARAMETER First
  23.     Gets only the specified number of objects. Enter the number of objects to get.
  24.    
  25. .EXAMPLE
  26.      ConvertTo-PSObject -InputObject 'Value1', 'Value2'
  27.  
  28. .EXAMPLE
  29.      'Value1', 'Value2' | ConvertTo-PSObject -First 1
  30.  
  31. .INPUTS
  32.     Any
  33.  
  34. .OUTPUTS
  35.     PSObject
  36.  
  37. .NOTES
  38.     Normally one might use ConvertTo-Json and ConvertFrom-Json in a pipeline to do the same thing this
  39.     function does, but I've found there to be an issue where many times when trying to use JSON format that
  40.     an error will result, complaining about duplicate properties. This gets around that issue.
  41.    
  42.     Author:  Dale Thompson
  43.     Website: none
  44.     Twitter: @UnitedDale
  45. #>
  46. <#PSScriptInfo
  47.  
  48. .VERSION 1.1
  49.  
  50. .GUID 4c100417-816b-4b25-b0c4-a019f021e5e6
  51.  
  52. .AUTHOR Dale Thompson
  53.  
  54. .COMPANYNAME WSP
  55.  
  56. .COPYRIGHT All rights reserved
  57.  
  58. .TAGS Deserialize PSObject
  59.  
  60. .RELEASENOTES
  61.  
  62. #>
  63.     [CmdletBinding()]
  64.     [OutputType('PSObject')]
  65.     param (
  66.         [Parameter(Mandatory, ValueFromPipeline)][object[]]$InputObject,
  67.         [int32]$Depth,
  68.         [uint64]$Skip,
  69.         [uint64]$First
  70.     )
  71.     BEGIN {
  72.         [System.IO.FileInfo]$TempFile = New-TemporaryFile
  73.         [hashtable]$ExportParams = @{ LiteralPath = $TempFile.FullName }
  74.         'Depth' | % {
  75.             if ($PSBoundParameters[$_]) { $ExportParams[$_] = $PSBoundParameters[$_] }
  76.         }
  77.         [hashtable]$ImportParams = @{ LiteralPath = $TempFile.FullName }
  78.         'Skip', 'First' | % {
  79.             if ($PSBoundParameters[$_]) { $ImportParams[$_] = $PSBoundParameters[$_] }
  80.         }
  81.         [System.Collections.Generic.List``1[object]]$Objects = @()
  82.     }
  83.     PROCESS {
  84.         foreach ($Object in $InputObject) {
  85.             [void]$Objects.Add($Object)
  86.         }
  87.     }
  88.     END {
  89.         $Objects | Export-Clixml @ExportParams
  90.         Import-Clixml @ImportParams
  91.         Remove-Item $TempFile
  92.     }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement