0

I am new to powershell and I am trying to load and validate json data from a web url. This url will give the connection status of different interfaces. I am trying to return "1" if any of the test connection status equals Fail. I am getting errors when trying to loop through the parsed json data. Appreciate your help on this subject

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$request = 'https://connecttestservices.net/connection-test'
Invoke-WebRequest $request |
ConvertFrom_Json |
foreach ($request in $x) {
    if ( $request.connectionTestStatus -match "FAIL" ) {    
        return 1               
    }   
}
Prdp
  • 47
  • 6

1 Answers1

0

I think there are a few mistakes in your code. For example in ConvertFrom-JSON and also you say foreach($request in $x){} but $x is never defined. I think the below code should work a lot better:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$request = 'https://connecttestservices.net/connection-test'
$response = Invoke-WebRequest -Uri $request
$response = $response | ConvertFrom-Json
if($response.connectionTestStatus -match "FAIL"){    
    return 1
}

Hope this helps

Bernard Moeskops
  • 1,203
  • 1
  • 12
  • 16
  • Thanks Bernard. This is an JSON array and how can I use loop and check the values on all elements. – Prdp Apr 24 '20 at 08:42
  • To loop through all the items you could check out this post: https://stackoverflow.com/a/33521853/9304296 If you would just type $response in your console (after you made the request) you can check what values it contains and which ones you want to use – Bernard Moeskops Apr 24 '20 at 09:00