-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find-JsonElementValueByPropertyName.ps1
58 lines (51 loc) · 1.96 KB
/
Find-JsonElementValueByPropertyName.ps1
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
function Find-JsonElementValueByPropertyName {
<#
.SYNOPSIS
Returns the value from a json string for a specified full-stop delimited path.
.DESCRIPTION
Given a JSON object and a full-stop delimited path, the function will convert the
JSON object to a PSObject and will use a scriptblock to generate the dynamic
PowerShell to select down to the specified value.
.EXAMPLE
PS C:\> <example usage>
Explanation of what the example does
.INPUTS
Inputs (if any)
.OUTPUTS
Output (if any)
.NOTES
TODO: Comment based help...
#>
[CmdletBinding()]
param (
[Parameter(Mandatory,
ValueFromPipeline,
Position = 0,
HelpMessage = 'Pass in the object holding the json file contents.')]
[string] $JsonContents,
[Parameter(Mandatory,
ValueFromPipeline,
Position = 1,
HelpMessage = 'Enter a full-stop delimited location to the property e.g. "web.smtp.password".')]
[string] $JsonProperty
)
begin {
Write-Verbose 'Splitting JSON properties...'
$propertyCollection = $JsonProperty -split '\.'
Write-Verbose "Json Elements:`n$($propertyCollection | Out-String)"
}
process {
Write-Verbose 'Dynamically creating string script...'
$preScriptBlock = $propertyCollection |
ForEach-Object -Begin {
'ConvertFrom-Json -InputObject $JsonContents'
} -Process {
" |`n`tSelect-Object -ExpandProperty $_"
}
Write-Verbose "Pre ScriptBlock string created:`n$($preScriptBlock | Out-String)"
Write-Verbose 'Converting to scriptblock...'
$script = [scriptblock]::Create($preScriptBlock)
Write-Verbose "Executing scriptblock:`n$($script | Out-String)"
$script.InvokeReturnAsIs()
}
}