PowerShell执行aws list-clusters如何提取无引号逗号的指定集群字符串
推荐方案(最稳定)
aws list-clusters返回结果是标准JSON格式,直接用PowerShell的JSON解析能力结构化处理即可,完全避免字符串匹配带来的多余符号问题:
$cluster = (aws list-clusters | ConvertFrom-Json).clusters | Where-Object { $_ -match 'test-staging' }
执行后$cluster的值就是纯字符串test-staging-100,没有多余的引号、逗号,且不会受返回结果排版、空格、符号位置的影响,稳定性远高于字符串匹配方案。
适配现有命令的方案
如果你要保留现有Select-String的写法,只需对匹配结果做一次字符替换,剔除引号和逗号即可:
$cluster = (aws list-clusters | Select-String "test-staging").Line -replace '["",]', ''
内容的提问来源于stack exchange,提问作者doctore
相关产品推荐
相关产品推荐

