StackExchangeGuy
StackExchangeGuy

Reputation: 789

Requesting help to parse a string

I am using Invoke-WebRequest to get back an HtmlWebResponseObject object. I want to get the value of the ID field, when the description field matches a particular string (in this case, "server1"). There are hundreds of results returned by Invoke-WebRequest (so the string is super long) Given the sample below, how would I extract the ID?

{ "status": 200, "data": [ { "nextRecipient": 0, "clearSent": true, "lastSentNotificationOn": 0, "netscanVersion": "0", "suppressAlertClear": "false", "build": "19000", "lastSentNotificationOnLocal": "", "id": 6, "resendIval": 15, "watchdogUpdatedOn": "2016-04-11 10:28:02 MDT", "escalatingChainId": 6, "description": "domain\server1", "ackComment": "", "credential2": "", "updatedOn": 1460392096, "updatedOnLocal": "2016-04-11 10:28:16 MDT", "agentConf": "product.code={guid}\r\n# Installer version, Shall not be modified\r\ninstaller.version=0001\r\n\r\n# Generated by Agent Configuration Wizard\r\nserver=url\r\ncompany=company\r\nid=6\r\ncredential==cred\r\n\r\n# logger settings. Set logger.size to 0 to no limitation on logger size, otherwise, size limited to Mbytes specified by that\r\nlogger.output=console\r\nlogger.logfile=\r\nlogger.size=64\r\nlogger.level=info\r\n\r\n#watchdog log level\r\nlogger.watchdog=info\r\n# for each component, add more detailed control here\r\n# e.g. \r\n# logger.level.controller=debug\r\n#\r\n# if not set, it will use default log level, i.e. value of logger.level\r\n#\r\n\r\n#if agent shall watch watchdog, default to false\r\nagentmonitorwatchdog=true\r\n\r\n#whether watchdog upgrades agent, default to true\r\nagent.autoupgrade=true\r\n\r\n#service connection timeouts. Default to 5 seconds for connecting and 30 seconds for sending / reading feeds from server\r\nservice.connect_timeout=5\r\nservice.read_timeout=30\r\n\r\n#SSL & Proxy settings\r\nssl.enable=true\r\nproxy.enable=false\r\nproxy.host= \r\nproxy.port=\r\nproxy.user=\r\nproxy.pass=\r\nproxy.exclude=\r\n\r\n#sbproxy settings\r\nsbproxy.address=127.0.0.1\r\nsbproxy.port=72\r\nsbproxy.logsize=64\r\nsbproxy.restartOn102=false\r\nsbproxy.pdhNoAuthentication=false\r\n\r\n#sbproxy connection pool settings\r\nsbproxy.pool.connections=50\r

Thanks.

Upvotes: 1

Views: 112

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200203

Convert the response from a JSON string to an object and expand the relevant properties:

$response | ConvertFrom-Json |
  Select-Object -Expand data |
  Where-Object { $_.description -match 'server1' } |
  Select-Object -Expand id

Upvotes: 5

Related Questions