-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathWorking-with-JSON-Objects.ps1
30 lines (20 loc) · 1.03 KB
/
Working-with-JSON-Objects.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
# Generate JSON
systeminfo /fo CSV | ConvertFrom-Csv | convertto-json | Out-File "$($Location)\ComputerInfo.json"
# Load JSON
Get-Content -Path "$($Location)\ComputerInfo.json" | ConvertFrom-JSON
# Load JSON into a Grid View
Get-Content -Path "$($Location)\ComputerInfo.json" | ConvertFrom-JSON | Out-GridView
# Populate Variable with JSON and use Values
$jsonObject = Get-Content -Path "$($Location)\ComputerInfo.json" | ConvertFrom-JSON
$jsonObject.'Host Name'
$jsonObject.'Windows Directory'
# Manually Creating JSON
$jsonObject = @{}
$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add(@{"Name" = "Reid"; "Surname" = "Randolph"; "Gender" = "M"; })
$arrayList.Add(@{"Name" = "Scott"; "Surname" = "Best"; "Gender" = "M"; })
$arrayList.Add(@{"Name" = "Isabel"; "Surname" = "Mays"; "Gender" = "F"; })
$arrayList.Add(@{"Name" = "Marcia"; "Surname" = "Clark"; "Gender" = "F"; })
$employees = @{"Employees" = $arrayList; }
$jsonObject.Add("Data", $employees)
$jsonObject | ConvertTo-Json -Depth 10