Guest User

Untitled

a guest
Dec 13th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.11 KB | None | 0 0
  1. function ConvertTo-OrderedDictionary
  2. {
  3. #requires -Version 2.0
  4.  
  5. [CmdletBinding()]
  6. param (
  7. [Parameter(Mandatory = $true, ValueFromPipeline = $true)]
  8. [hashtable]
  9. $InputObject,
  10.  
  11. [Type]
  12. $KeyType = [string]
  13. )
  14.  
  15. process
  16. {
  17. #$outputObject = New-Object "System.Collections.Generic.Dictionary[[$($KeyType.FullName)],[Object]]"
  18. $outputObject = New-Object "System.Collections.Specialized.OrderedDictionary"
  19.  
  20. foreach ($entry in $InputObject.GetEnumerator())
  21. {
  22. $newKey = $entry.Key -as $KeyType
  23.  
  24. if ($null -eq $newKey)
  25. {
  26. throw 'Could not convert key "{0}" of type "{1}" to type "{2}"' -f
  27. $entry.Key,
  28. $entry.Key.GetType().FullName,
  29. $KeyType.FullName
  30. }
  31. elseif ($outputObject.Contains($newKey))
  32. {
  33. throw "Duplicate key `"$newKey`" detected in input object."
  34. }
  35.  
  36. $outputObject.Add($newKey, $entry.Value)
  37. }
  38.  
  39. Write-Output $outputObject
  40. }
  41. }
Add Comment
Please, Sign In to add comment