Saving You 15 Minutes - Formatting JSON output from Oracle Cloud CLI with Powershell Part 2
Saving You 15 Minutes - Formatting JSON output from Oracle Cloud CLI with Powershell Part 2
By Phil Brown
On 05 July 2018

In Part 1, I gave some examples of how to use Powershell to extract some of the OCI information from JSON and format it. What you will find is that some of the information you can return from the cli will be nested and looking at the security rules is a good example to demonstrate this and how to extract that information.

To help us recap we will build this up in stages:

Firstly, running the command and converting from JSON gives you a not particularly helpful and unreadable output.

PS C:\Users\phili> oci network security-list get --security-list-id ocid1.securitylist.oc1.eu-frankfurt-1.XXX | convertfrom-json
data
----
@{compartment-id=ocid1.compartment.oc1.XXX; defined-tags=; display-name=sl-blah-blah; egress-security-rules=System.Object[];

To make it more readable you can echo the full(ish) output using $output.data

PS C:\Users\phili> echo $output.data


compartment-id        	: ocid1.compartment.oc1.XXXX
defined-tags          	:
display-name          	: sl-blah-blah-blah
egress-security-rules 	:{@{destination=10.1.1.1/32;icmp-options=;is-stateless=False;protocol=6;tcp-options=;udp-options=},@{destination=10.1.1.2/32;icmp-options=;is-stateless=False;protocol=6;tcp-options=;udp-options=},@{destination=10.1.1.3/32;icmp-options=;is-stateless=False;protocol=6;tcp-options=;udp-options=},
@{destination=10.1.1.3/32; icmp-options=; is-stateless=False; protocol=6; tcp-options=; udp-options=}...}
…
…
…
ingress-security-rules	:{@{destina

This is where it starts to get interesting; the egress-security-rules is truncated in the output as is the ingress-security-rules; to expand this we can append our echo command with additional output. echo $output.data.’egress-security-rules’

PS C:\Users\phili> echo $output.data.’egress-security-rules’


destination  : 10.1.1.1/32
icmp-options :
is-stateless : False
protocol     : 6
tcp-options  : @{destination-port-range=@{max=22; min=22}; source-port-range=}
udp-options  :

This is ok but it’s just going to be a long list of information and what I want is a table, if I run this command I should get it….

PS C:\Users\phili> $output=oci network security-list get --security-list-id ocid1.securitylist.oc1.XXX | convertfrom-json
PS C:\Users\phili> $output.data.'egress-security-rules' | select destination,protocol,'tcp-options'
10.1.1.1/32    6        @{destination-port-range=; source-port-range=}
10.1.1.2/32    6        @{destination-port-range=; source-port-range=}
10.1.1.3/32    6        @{destination-port-range=; source-port-range=}

Here the nested json is an issue you can’t see the port information; therefore what you can do (this is a hack of course) is use the following command:

PS C:\Users\phili> $output=oci network security-list get --security-list-id ocid1.securitylist.oc1.eu-frankfurt-1.XXX | convertfrom-json | convertto-json -depth 4 | convertfrom-json

10.1.1.1/32  6        @{destination-port-range=@{max=22; min=22}; source-port-range=}
10.1.1.1/32  6        @{destination-port-range=@{max=22; min=22}; source-port-range=}
10.1.1.1/32  6        @{destination-port-range=@{max=22; min=22}; source-port-range=}

Not clean; not pretty; but just a bit of fun when I’ve been looking at Powershell; OCI and JSON.

Get in touch

We always put our customers first. Contact us by using the form below, and we will get back to you as soon as we can.

Please make sure to use a valid company email.
Please make sure to use a valid email format.

Data Intensity Solutions you are interested in (Select one that applies):

Submit
Please make sure all the mandatory fields marked red are filled.

Clicking this button tells us that you also agree to our privacy policy and cookie policy. You can opt out of these communications at any time.