forked from jutlag/CommonFunctions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-Hashtable.ps1
More file actions
63 lines (51 loc) · 4.42 KB
/
ConvertTo-Hashtable.ps1
File metadata and controls
63 lines (51 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function ConvertTo-HashTable {
<#
.SYNOPSIS
Converts an object to HashTable
.DESCRIPTION
This function is written to convert an object such as PsObject to a valid HashTable.
Since the function works recursively, it can convert nested objects to a valid Hashtable.
It is particularly useful to read JSON files, ARM Templates, PsObjects to Hashtable to easily
process and manage large structures in for loops etc. Usage will be demonstrated and explained
through some of the other examples in this repository.
The function supports values from pipelines.
.NOTES
Credits: The original function was shared by Dave Wyatt in 2015 and I do not know if he was the
original developer. However I improvised his code to replace Write-output and use
return instead as the write-output was interfering with my unit test code and some other
code. Also I prefer using return over using write-output, write-host etc as return values
from my functions.
Write-output with -Noenumerate gives the output data without enumerating values and simple
return $collection in below code will return array values instead of the cobject, therefore
I introduced the trick in the below line. There could be other ways but this one just works
fine. Let me know if you have better ideas.
Author: Unknown, Updated by Gurpreet Singh Jutla
.EXAMPLE
C:\PS> Get-Content -path .\test.json | ConvertFrom-JSON | ConvertTo-HashTable
C:\PS> ConvertTo-HashTable -InputObject <ValidJSONObject>
#>
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process {
if ($null -eq $InputObject) { return $null }
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string]) {
$collection = @(
foreach ($object in $InputObject) { ConvertTo-Hashtable $object }
)
return @(,$collection)
}
elseif ($InputObject -is [psobject]) {
$hash = @{ }
foreach ($property in $InputObject.PSObject.Properties) {
$hash[$property.Name] = ConvertTo-Hashtable $property.Value
}
return $hash
}
else {
return $InputObject
}
}
}