Advertisement
Old-Lost

ConvertTo-Hashtable

Apr 7th, 2017
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function ConvertTo-Hashtable {
  2.     [CmdletBinding()][OutputType([hashtable])]
  3.     Param (
  4.         [Parameter(Mandatory, ValueFromPipeline)][System.Object]$InputObject
  5.     )
  6.     BEGIN {
  7.         [boolean]$IgnoreProcess = $false
  8.         [System.Collections.Generic.List``1[System.Object]]$List = @()
  9.         If ($PSBoundParameters.ContainsKey('InputObject')) {
  10.             [void]$List.AddRange(@($InputObject))
  11.             $IgnoreProcess = $true
  12.         }
  13.         $ProcessOneObject = {
  14.             $Object = $List[0]
  15.             [void]$List.RemoveAt(0)
  16.             #region Place code to handle one object in this region
  17.             $Object.PSObject.Properties | % -Begin { $h = @{ } } -Process { [void]$h.Add($_.Name, $_.Value) } -End { $h }
  18.             #endregion
  19.         }
  20.     }
  21.     PROCESS {
  22.         if ($IgnoreProcess) { return }
  23.         [void]$List.Add($InputObject)
  24.         . $ProcessOneObject
  25.     }
  26.     END {
  27.         try {
  28.             while ($List.Count -gt 0) { . $ProcessOneObject }
  29.         } catch {
  30.             throw
  31.         } finally {
  32.             trap { continue }
  33.             $ErrorActionPreference = 'SilentlyContinue'
  34.             #region Cleanup code
  35.             #endregion
  36.         }
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement