Yep, first times are, well, you know. 8^}
One: Why are you using the same variable on each assignment?
Two: if the first one is working for you, why do you feel the need to try the other one?
Three: If you want this as that approach, then try it this way. Though this is not optimal, as you’ll see later.
# Define a variable to assign the output to. Get the object details first. Then get the subnet from the previous object
$network = (Invoke-RestMethod -uri $apiUrl) | Select-Object -ExpandProperty Subnet
Those parens, like math means do this first before doing anything else.
Which is what you did here:
$network = Invoke-RestMethod -uri $apiUrl
$SubNetwork= $network | Select-Object -ExpandProperty Subnet
The real question is why do it this way at all. You already have all you need from the first
Invoke-RestMethod -uri $apiUrl
I can’t do the above for obious reasons, but If I just take your JSON results. Just use the ConvertFrom-Json cmdlet.
# Get parameters, examples, full and Online help for a cmdlet or function
(Get-Command -Name ConvertFrom-Json).Parameters
Get-help -Name ConvertFrom-Json -Examples
Get-help -Name ConvertFrom-Json -Full
Get-help -Name ConvertFrom-Json -Online
Get-Help about_*
Get-Help about_Functions
# Find all cmdlets / functions with a target parameter
Get-Help * -Parameter Append
# All Help topics locations
explorer "$pshome\$($Host.CurrentCulture.Name)"
$ApiJson = @'
[
{
"Active": true,
"AlternateName": "sample string 2",
"EnvironmentInfoId": 3,
"InstallWeek": "2018-03-07T16:06:25.0430535-05:00",
"Notes": "sample string 4",
"RackSize": "sample string 5",
"Subnet": {
"SubnetId": 1,
"Network": "sample string 2",
"RangeStart": 3,
"RangeEnd": 4,
"Tunnel1": "sample string 5",
"Tunnel2": "sample string 6",
"Tunnel3": "sample string 7",
"Tunnel4": "sample string 8",
"Tunnel5": "sample string 9",
"Tunnel6": "sample string 10",
"GTechSRX": "sample string 11",
"CreatedBy": "sample string 12",
"LastModifiedBy": "sample string 13",
"CreatedDate": "2018-03-07T16:06:25.0430535-05:00",
"LastModifiedDate": "2018-03-07T16:06:25.0430535-05:00"
},
"SubnetId": 1,
"ThirdRegister": true,
"CreatedBy": "sample string 7",
"LastModifiedBy": "sample string 8",
"CreatedDate": "2018-03-07T16:06:25.0430535-05:00",
"LastModifiedDate": "2018-03-07T16:06:25.0430535-05:00"
},
{
"Active": true,
"AlternateName": "sample string 2",
"EnvironmentInfoId": 3,
"InstallWeek": "2018-03-07T16:06:25.0430535-05:00",
"Notes": "sample string 4",
"RackSize": "sample string 5",
"Subnet": {
"SubnetId": 1,
"Network": "sample string 2",
"RangeStart": 3,
"RangeEnd": 4,
"Tunnel1": "sample string 5",
"Tunnel2": "sample string 6",
"Tunnel3": "sample string 7",
"Tunnel4": "sample string 8",
"Tunnel5": "sample string 9",
"Tunnel6": "sample string 10",
"GTechSRX": "sample string 11",
"CreatedBy": "sample string 12",
"LastModifiedBy": "sample string 13",
"CreatedDate": "2018-03-07T16:06:25.0430535-05:00",
"LastModifiedDate": "2018-03-07T16:06:25.0430535-05:00"
},
"SubnetId": 1,
"ThirdRegister": true,
"CreatedBy": "sample string 7",
"LastModifiedBy": "sample string 8",
"CreatedDate": "2018-03-07T16:06:25.0430535-05:00",
"LastModifiedDate": "2018-03-07T16:06:25.0430535-05:00"
}
]
'@
$ApiJson | ConvertFrom-Json
Which gives you this…
Active : True
AlternateName : sample string 2
EnvironmentInfoId : 3
InstallWeek : 2018-03-07T16:06:25.0430535-05:00
Notes : sample string 4
RackSize : sample string 5
Subnet : @{SubnetId=1; Network=sample string 2; RangeStart=3; RangeEnd=4; Tunnel1=sample string 5; Tunnel2=sample string 6; Tunnel3=sample string 7; Tunnel4=sample string 8;
Tunnel5=sample string 9; Tunnel6=sample string 10; GTechSRX=sample string 11; CreatedBy=sample string 12; LastModifiedBy=sample string 13;
CreatedDate=2018-03-07T16:06:25.0430535-05:00; LastModifiedDate=2018-03-07T16:06:25.0430535-05:00}
SubnetId : 1
ThirdRegister : True
CreatedBy : sample string 7
LastModifiedBy : sample string 8
CreatedDate : 2018-03-07T16:06:25.0430535-05:00
LastModifiedDate : 2018-03-07T16:06:25.0430535-05:00
Active : True
AlternateName : sample string 2
EnvironmentInfoId : 3
InstallWeek : 2018-03-07T16:06:25.0430535-05:00
Notes : sample string 4
RackSize : sample string 5
Subnet : @{SubnetId=1; Network=sample string 2; RangeStart=3; RangeEnd=4; Tunnel1=sample string 5; Tunnel2=sample string 6; Tunnel3=sample string 7; Tunnel4=sample string 8;
Tunnel5=sample string 9; Tunnel6=sample string 10; GTechSRX=sample string 11; CreatedBy=sample string 12; LastModifiedBy=sample string 13;
CreatedDate=2018-03-07T16:06:25.0430535-05:00; LastModifiedDate=2018-03-07T16:06:25.0430535-05:00}
SubnetId : 1
ThirdRegister : True
CreatedBy : sample string 7
LastModifiedBy : sample string 8
CreatedDate : 2018-03-07T16:06:25.0430535-05:00
LastModifiedDate : 2018-03-07T16:06:25.0430535-05:00
… which I then can just do this
($ApiJson | ConvertFrom-Json).Subnet
Which gives this.
SubnetId : 1
Network : sample string 2
RangeStart : 3
RangeEnd : 4
Tunnel1 : sample string 5
Tunnel2 : sample string 6
Tunnel3 : sample string 7
Tunnel4 : sample string 8
Tunnel5 : sample string 9
Tunnel6 : sample string 10
GTechSRX : sample string 11
CreatedBy : sample string 12
LastModifiedBy : sample string 13
CreatedDate : 2018-03-07T16:06:25.0430535-05:00
LastModifiedDate : 2018-03-07T16:06:25.0430535-05:00
SubnetId : 1
Network : sample string 2
RangeStart : 3
RangeEnd : 4
Tunnel1 : sample string 5
Tunnel2 : sample string 6
Tunnel3 : sample string 7
Tunnel4 : sample string 8
Tunnel5 : sample string 9
Tunnel6 : sample string 10
GTechSRX : sample string 11
CreatedBy : sample string 12
LastModifiedBy : sample string 13
CreatedDate : 2018-03-07T16:06:25.0430535-05:00
LastModifiedDate : 2018-03-07T16:06:25.0430535-05:00
So, just, do
(Invoke-RestMethod -uri $apiUrl | ConvertFrom-Json).Subnet