Search code examples
powershellinvoke-webrequest

Need to sort value from web request response


I need to sort specific value of BuildName: from web request response.

How can I store a particular response in a variable?

Trying with following command

Invoke-WebRequest -Uri https://s3.amazonaws.com/$url/env.js | select-object Content

I am getting the following response. I need to sort the values from there

Response will be like this

window.env = { // Hardcode environment variables in here but tokenize customer specific ones with #{}# BuildName: 'AppClient-develop-0716.4' GRAPH_QL_HOST: 'https://xyz.google.com/graphql' };

From her need to get value of BuildName:


Solution

  • You can convert the content to individual strings and parse those to get the individual values.

    Example

    # $Content = (Invoke-WebRequest -Uri https://s3.amazonaws.com/$url/env.js).Content
    $Content = @'
    window.env = { 
      // Hardcode environment variables in here but tokenize customer specific ones with #{}# 
      BuildName: 'AppClient-develop-0716.4' 
      GRAPH_QL_HOST: 'xyz.google.com/graphql' 
     };
    '@ 
    
    $Content -split "`r`n" | Select-String "\w+:" | % { ($_ -split ": ")[1]}