Home > Enterprise >  PowerShell - Get only a string value
PowerShell - Get only a string value

Time:10-22

Hi how do I get only the value test-staging-100 in the following dictionary

{ 
   "clusters": [
       "test-staging-100", 
       "test-local-1", 
       "test-dev-50" 
     ] 
}

when I ran this command

$cluster = aws list-clusters | Select-String "test-staging"

I keep getting "test-staging-100", . I don't want the quotation and comma but only test-staging-100

CodePudding user response:

It looks like what the aws CLI returns is JSON text, so it's preferable to parse it into an object graph with ConvertFrom-Json and act on that:

$cluster = (aws list-clusters | ConvertFrom-Json).clusters -match 'test-staging'

If you really wanted to use Select-String to search the raw string output (which is ill-advised), you'd have to do something like:

$cluster = (aws list-clusters | Select-String 'test-staging[^"] ').Matches.Value
  • Related