Repository navigation
Expand file tree
/
Copy pathADMjson.psm1
More file actions
122 lines (96 loc) · 3.13 KB
/
Copy pathADMjson.psm1
File metadata and controls
122 lines (96 loc) · 3.13 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
function Get-JsonFile {
param (
[Parameter(Mandatory)]
[string]$FileName
)
try {
$configFile = (Get-Content -Raw -Encoding UTF8 -path $fileName -ErrorAction Stop)
return ($configFile | ConvertFrom-Json)
}
catch {
Write-Error $_
exit 1
}
}
function Set-JsonFile {
param (
[Parameter(Mandatory)]
[string]$FileName,
[Parameter(Mandatory)]
[object]$JsonObject
)
try {
$json = $JsonObject | ConvertTo-Json
Set-Content -Encoding UTF8 -Path $FileName -Value $json | Out-Null
}
catch {
Write-Error $_
exit 1
}
}
function Get-PropertyValue {
param (
[Parameter(Mandatory)]
[string]$PropertyName,
[Parameter(Mandatory)]
[string]$PropertyExpression
)
if( -not($PropertyExpression.Contains('[') ) ) {
return $PropertyExpression
}
$expression = [regex]::Matches( $PropertyExpression, '\[[a-z0-9()\/=?.]*\]').Value
if( $null -ne $expression -and $expression.StartsWith('[') ) {
$expression = $expression.substring(1,$expression.Length-2)
$metadata_type = [regex]::Matches( $expression, '[a-z0-9/?]*\(').Value
$metadata_type = $metadata_type.substring(0,$metadata_type.Length-1)
$metadata_query = [regex]::Matches( $expression, '\([a-z0-9,=?]*\)').Value
$metadata_query = $metadata_query.substring(1,$metadata_query.Length-2)
$condition = $metadata_query.Split("=")
if( $condition.length -ne 2 ) {
Write-Error "Condition $metadata_query is not well formed."
}
try {
$metadata_file = Get-JsonFile -File "$metadata_type.json"
$metadata_parameter = $metadata_file
$metadata_parameter = $metadata_file | Where-Object {
$_.("$($condition[0])") -EQ "$($condition[1])"
}
$metadata_property = [regex]::Matches( $expression, '\.[a-z.]*').Value
$metadata_property = $metadata_property.substring(1,$metadata_property.Length-1)
$metadata_value = $metadata_parameter.($metadata_property)
return $metadata_value
}
catch {
Write-Error $_
exit 1
}
}
}
function Expand-JsonFile {
param (
[Parameter(Mandatory)]
[string]$SourceFile,
[string]$TargetFile = "out.json"
)
try {
$json = Get-JsonFile -FileName $SourceFile
$json | ForEach-Object {
$item = $_
$item.psobject.properties
| Where-Object { ($_.MemberType -eq "NoteProperty" `
-and $_.TypeNameOfValue -eq "System.String" `
-and $_.Value.StartsWith('[')
) }
| ForEach-Object {
$property_value = Get-PropertyValue -PropertyName $_.name -PropertyExpression $_.value
$_.value = $property_value
}
}
Set-JsonFile -FileName $TargetFile -JsonObject $json
# return $json
}
catch {
Write-Error $_
exit 1
}
}