-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneral.ps1
More file actions
310 lines (264 loc) · 8.7 KB
/
Copy pathGeneral.ps1
File metadata and controls
310 lines (264 loc) · 8.7 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# JSON Module Examples
# This file contains practical examples of using the Format-Json and Import-Json functions
# Import the module (if not already loaded)
# Import-Module Json
#region Basic Examples
# Example 1: Format a simple JSON string with spaces indentation
'Example 1: Format JSON with spaces (default)'
$jsonString = '{"a":1,"b":{"c":2}}'
$formatted = Format-Json -JsonString $jsonString -IndentationType Spaces -IndentationSize 2
$formatted
# Example 2: Format a PowerShell object as JSON with tabs
'Example 2: Format PowerShell object with tabs'
$obj = @{
user = 'Marius'
roles = @('admin', 'dev')
settings = @{
theme = 'dark'
notifications = $true
}
}
$formatted = Format-Json -InputObject $obj -IndentationType Tabs -IndentationSize 1
$formatted
# Example 3: Compact (minified) JSON output
'Example 3: Compact JSON output'
$jsonString = '{"a":1,"b":{"c":2}}'
$compact = Format-Json -JsonString $jsonString -Compact
$compact
#endregion
#region Advanced Examples
# Example 4: Complex nested object with different indentation sizes
'Example 4: Complex nested structure with 4-space indentation'
$complexObj = @{
metadata = @{
version = '1.0.0'
author = 'PowerShell Community'
created = Get-Date -Format 'yyyy-MM-dd'
}
data = @{
users = @(
@{ id = 1; name = 'Alice'; active = $true },
@{ id = 2; name = 'Bob'; active = $false }
)
configuration = @{
api = @{
baseUrl = 'https://api.example.com'
timeout = 30
retries = 3
}
features = @{
logging = $true
caching = $false
analytics = $true
}
}
}
}
$formatted = Format-Json -InputObject $complexObj -IndentationType Spaces -IndentationSize 4
$formatted
# Example 5: Pipeline usage - format multiple JSON strings
'Example 5: Pipeline usage with multiple JSON strings'
$jsonStrings = @(
'{"name":"John","age":30}',
'{"product":"Widget","price":9.99}',
'{"status":"active","count":42}'
)
$jsonStrings | ForEach-Object {
"Original: $_"
$formatted = $_ | Format-Json -IndentationType Spaces -IndentationSize 2
$formatted
''
}
('=' * 50 + "`n")
# Example 6: Error handling demonstration
'Example 6: Error handling with invalid JSON'
try {
$invalidJson = '{"invalid": json}'
Format-Json -JsonString $invalidJson
} catch {
Write-Warning "Caught expected error: $($_.Exception.Message)"
}
#endregion
#region Practical Use Cases
# Example 7: Format configuration file
'Example 7: Format a configuration file structure'
$config = @{
server = @{
host = 'localhost'
port = 8080
ssl = @{
enabled = $true
certificate = '/path/to/cert.pem'
key = '/path/to/key.pem'
}
}
database = @{
type = 'postgresql'
host = 'db.example.com'
port = 5432
name = 'myapp'
pool = @{
min = 5
max = 20
}
}
logging = @{
level = 'info'
file = '/var/log/app.log'
rotation = @{
enabled = $true
maxSize = '100MB'
maxFiles = 7
}
}
}
$formatted = Format-Json -InputObject $config -IndentationType Spaces -IndentationSize 2
$formatted
# Example 8: Compare compact vs formatted output
'Example 8: Compare compact vs formatted output'
$data = @{
items = @(1, 2, 3, 4, 5)
metadata = @{ total = 5; page = 1 }
}
'Compact version:'
$compact = Format-Json -InputObject $data -Compact
$compact
"`nFormatted version:"
$formatted = Format-Json -InputObject $data -IndentationType Spaces -IndentationSize 2
$formatted
#endregion
#endregion
#region Import-Json Examples
# Example 9: Import JSON from a single file
'Example 9: Import JSON from a single file'
# First, create a sample JSON file
$configData = @{
database = @{
host = 'localhost'
port = 5432
name = 'myapp'
ssl = $true
}
logging = @{
level = 'info'
file = '/var/log/app.log'
}
features = @{
caching = $true
analytics = $false
}
}
$configFile = '/tmp/config.json'
$configData | ConvertTo-Json -Depth 3 | Set-Content -Path $configFile
# Import the JSON file
$importedConfig = Import-Json -Path $configFile
$importedConfig
"Database host: $($importedConfig.database.host)"
"Source file: $($importedConfig._SourceFile)"
# Example 10: Import multiple JSON files using wildcards
'Example 10: Import multiple JSON files using wildcards'
# Create multiple JSON files
$userData = @{ name = 'Alice'; role = 'admin'; active = $true }
$settingsData = @{ theme = 'dark'; notifications = $true; language = 'en' }
$userFile = '/tmp/user-data.json'
$settingsFile = '/tmp/user-settings.json'
$userData | ConvertTo-Json | Set-Content -Path $userFile
$settingsData | ConvertTo-Json | Set-Content -Path $settingsFile
# Import all user-*.json files
$allUserData = Import-Json -Path '/tmp/user-*.json'
$allUserData | ForEach-Object {
"Imported from: $($_._SourceFile)"
$_ | Format-List
}
# Example 11: Pipeline usage with Import-Json
'Example 11: Pipeline usage with Import-Json'
$jsonFiles = @($configFile, $userFile, $settingsFile)
$allData = $jsonFiles | Import-Json
"Imported $($allData.Count) JSON files via pipeline"
# Example 12: Error handling with Import-Json
'Example 12: Error handling with Import-Json'
try {
Import-Json -Path '/tmp/nonexistent.json' -ErrorAction Stop
} catch {
Write-Warning "Caught expected error: $($_.Exception.Message)"
}
# Example 13: Combine Import-Json with Format-Json
'Example 13: Combine Import-Json with Format-Json'
$rawConfig = Import-Json -Path $configFile
$formattedConfig = Format-Json -InputObject $rawConfig -IndentationType Spaces -IndentationSize 2
'Formatted imported configuration:'
$formattedConfig
# Cleanup temporary files
Remove-Item -Path $configFile, $userFile, $settingsFile -ErrorAction SilentlyContinue
#endregion
#region Export-Json Examples
# Example 14: Export simple object to file
'Example 14: Export simple object to file'
$userObject = @{
name = 'John Doe'
age = 30
email = 'john.doe@example.com'
active = $true
roles = @('user', 'contributor')
}
$outputFile = '/tmp/user-export.json'
Export-Json -InputObject $userObject -Path $outputFile -IndentationType Spaces -IndentationSize 2
'Exported to file:'
Get-Content $outputFile
# Example 15: Export with compact formatting
'Example 15: Export with compact formatting'
$compactFile = '/tmp/user-compact.json'
Export-Json -InputObject $userObject -Path $compactFile -Compact
'Compact export:'
Get-Content $compactFile
# Example 16: Export multiple objects via pipeline to same file (last overwrites previous)
'Example 16: Export multiple objects via pipeline'
$users = @(
@{ id = 1; name = 'Alice'; department = 'Engineering' },
@{ id = 2; name = 'Bob'; department = 'Marketing' },
@{ id = 3; name = 'Carol'; department = 'Sales' }
)
$users | Export-Json -Path '/tmp/users-pipeline.json' -IndentationType Tabs -IndentationSize 1
'Pipeline export result (last object only):'
Get-Content '/tmp/users-pipeline.json'
# Example 17: Export JSON string to file
'Example 17: Export JSON string to file'
$jsonString = '{"service":"api","version":"1.2.3","endpoints":["/users","/products","/orders"]}'
$serviceFile = '/tmp/service-config.json'
Export-Json -JsonString $jsonString -Path $serviceFile -IndentationType Spaces -IndentationSize 4
'Formatted JSON string export:'
Get-Content $serviceFile
# Example 18: Roundtrip example - Import, modify, export
'Example 18: Roundtrip example - Import, modify, export'
# First create a JSON file to import
$originalConfig = @{
database = @{
host = 'localhost'
port = 5432
name = 'myapp'
}
features = @{
logging = $true
caching = $false
analytics = $true
}
}
$configFile = '/tmp/original-config.json'
Export-Json -InputObject $originalConfig -Path $configFile
# Import and modify
$config = Import-Json -Path $configFile
$config.database.host = 'production-db.example.com'
$config.features.caching = $true
$config.lastModified = Get-Date -Format 'yyyy-MM-ddTHH:mm:ss'
# Export the modified configuration
$modifiedFile = '/tmp/modified-config.json'
Export-Json -InputObject $config -Path $modifiedFile -IndentationType Spaces -IndentationSize 2
'Original config:'
Get-Content $configFile
''
'Modified config:'
Get-Content $modifiedFile
# Cleanup temporary files
Remove-Item -Path $outputFile, $compactFile, $serviceFile, $configFile, $modifiedFile, '/tmp/users-pipeline.json' -ErrorAction SilentlyContinue
#endregion
"`nAll examples completed!"