Author Posts

May 31, 2017 at 12:52 pm

Team,

I am working on a reporting script and i need to extract lot of information from Azure PS cmdlet "Get-azurermnetworkinterface" follwoing is a sample of the output this cmdlet returns for a single NIC.

now I have several NICs and i am unable to find a way to extract properties like PrivateIPAddress, PrivateIPAddressAllocationMethod etc. basically i face issues with extracting nested properties. i want to send them to a excel sheet which pops on the screen right after i run the script. i have done this for Name, ResourceGrupName, Location etc.
plesae suggest how can i extract nested properties.

Name : dhk-sdwec
ResourceGroupName : HKDR-RG01
Location : eastasia
Id : /subscriptions//resourceGroups//providers/Microsoft.Network/networkInterfaces/sdedhk-csha1c1sql581dfe
Etag : W/"9c2b13ff-5a2c-4d54-9760-1bce7aea475d"
ResourceGuid : 359a46d8-15c7-4d94-b4d6-sdfawesvasde08098
ProvisioningState : Succeeded
Tags :
VirtualMachine : {
"Id": "/subscriptions/53d36695-014d-4471-b844-ef2204ae5868/resourceGroups/HKDR-RG01/providers/Microsoft.Compute/virtualMachines/DHK-CSHA1C1SQL"
}
IpConfigurations : [
{
"Name": "ipconfig1",
"Etag": "W/\""",
"Id": "IDhere"
/ipConfigurations/ipconfig1",
"PrivateIpAddress": "10.X.X.X",
"PrivateIpAllocationMethod": "Static",
"Subnet": {
"Id": "",
"ResourceNavigationLinks": []
},
"ProvisioningState": "Succeeded",
"PrivateIpAddressVersion": "IPv4",
"LoadBalancerBackendAddressPools": [],
"LoadBalancerInboundNatRules": [],
"Primary": true,
"ApplicationGatewayBackendAddressPools": []
}
]
DnsSettings : {
"DnsServers": [],
"AppliedDnsServers": [],
"InternalDomainNameSuffix":
}
EnableIPForwarding : False
EnableAcceleratedNetworking : False
NetworkSecurityGroup : {
"Id":
"/subscriptions/53d36695-sdfe-4471-b844-ef2204aeeref/resourceGroups/HKfedDR-ffRG01/providers/Microsoft.Network/networkSecurityGroups/DHKdeva-sdfeCSHA1C1SQL-nsg"
}
Primary : True

June 1, 2017 at 10:18 am

Hi Alok,

Try referencing the nested property's name. Sort of like this

$NIC.IPConfigurations['PrivateIPAddress']
$NIC.IPConfigurations['PrivateIPAllocationMethod']

Liam

June 1, 2017 at 3:06 pm

Is that.....JSON? I've never seen a cmdlet return JSON as it's default output before. Let me know if I am correct on that output type, and if I am, I have an idea!

June 1, 2017 at 3:13 pm

Hello Stephen,

yes it is JSON, Many Azure Cmdlet return JSON.

June 1, 2017 at 3:21 pm

Hello Lian
I tried what you suggested, unfortunately didnt work.

just to explain here is what i am trying to do.

Get-AzureRmNetworkInterface | foreach {
$inventoryws.Cells.Item($row, $Column) = $_.ID, $column++
$inventoryws.Cells.Item($row, $Column) = $_.Name, $column++
$inventoryws.Cells.Item($row, $Column) = $_.ResourceGroupName, $column++
$inventoryws.Cells.Item($row, $Column) = $_.IPConfigurations['PrivateIPAddress'], $column++
}

I also Tried follwoign to check if suggested method works.

$IP = get-azurermnetworkinterface
$IP.IPConfigurations['PrivateIPAddress']

No Luck!!

June 1, 2017 at 11:34 pm

Yeah, I haven't tried it with a json string sorry.
You might want to try using convertfrom-json on the result first, then select what you need from that

June 2, 2017 at 7:44 am

Hello Liam,

I tried that this morning, i get "Invalid JSON primitive" error.

PS C:\users\Alok.Maheshwari\Desktop\test> ConvertFrom-Json -InputObject $in
ConvertFrom-Json : Invalid JSON primitive: Name.
At line:1 char:1
+ ConvertFrom-Json -InputObject $in
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

PS C:\users\Alok.Maheshwari\Desktop\test> ConvertFrom-Json -InputObject $in[6]
ConvertFrom-Json : Invalid JSON primitive: m.
At line:1 char:1
+ ConvertFrom-Json -InputObject $in[6]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand

June 2, 2017 at 10:05 am

here is what i find out more,

output 'Type' of Get-azurermnetworkinterface is not JSON, here is what i get when i do Get-azurermnetworkinterface | GM

TypeName: Microsoft.Azure.Commands.Network.Models.PSNetworkInterface

maybe that's why convertfrom-JSON not working.