Extract specific object from JSON array as a comma separated string via PowerShell

# Invoke-RestMethod automatically parses the JSON result into
# a [pscustomobject] graph.
# In a manner of speaking, *ConvertFrom-Json is built in*.
$Results = Invoke-RestMethod -Uri "https://mywebsite.com/portscan.php"

# Get the objects representing open ports.
# Note: `Where-Object Status -eq open` is the equivalent of:
#       `Where-Object { $_.Status -eq 'open' }`
$OpenPorts = $Results | Where-Object Status -eq open

# Synthesize the output string.
$Message = "Open ports = " + ($OpenPorts.Port -join ', ')

# Output (echo) it.
# Output that isn't redirected or captured is *implicitly* echoed in PowerShell.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top