既然 Powershell 是开源且跨平台的(带有 Powershell Core),我想我会再试一次。
\n\n我过去曾使用过,并且在某些时候已经弄清楚了管道是如何工作的,但它并不是非常直观。应该是,但事实并非如此,所以我有点卡住了......
\n\n手头的任务:解析并打印命令的 JSON 输出中的多个字段。我可能可以用老式的方式来做到这一点,使用外部命令和字符串处理,\xc3\xa0 la bash,但我想学习如何以Powershell方式\xe2\x84\xa2来做到这一点。
\n\n澄清一下,我想在管道中以交互方式进行。我不想编写脚本,我想学习如何在一行(或最多 2 行,但基本上使用 Powershell 作为 REPL,而不是作为脚本工具)中进行这种处理。
\n\naws cloudformation describe-stack-events --stack-name some-stack-here | jq ".StackEvents[] | [.Timestamp, .ResourceStatus, .ResourceType, .ResourceStatusReason] | join(\\" \\")"
它的作用是获取输入 JSON 并仅打印我感兴趣的 3 个字段。
\n\n{\n "StackEvents": [\n {\n "StackId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",\n "EventId": "some-event-id-here",\n "ResourceStatus": "UPDATE_COMPLETE",\n "ResourceType": "AWS::CloudFormation::Stack",\n "Timestamp": "some-date-here",\n "StackName": "some-stack-here",\n "PhysicalResourceId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",\n "LogicalResourceId": "some-stack-here"\n },\n {\n "StackId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",\n "EventId": "some-event-id-here",\n "ResourceStatus": "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS",\n "ResourceType": "AWS::CloudFormation::Stack",\n "Timestamp": "some-date-here",\n "StackName": "some-stack-here",\n "PhysicalResourceId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",\n "LogicalResourceId": "some-stack-here"\n }\n ]\n}\nRun Code Online (Sandbox Code Playgroud)\n\n"some-date-here UPDATE_COMPLETE AWS::CloudFormation::Stack "\n"some-date-here UPDATE_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack "\nRun Code Online (Sandbox Code Playgroud)\n\n(我认为这应该放在 Stackoverflow 上,因为该主题涉及对 Powershell 概念的深入理解,包括 .NET 对象,更接近于编程而不是系统管理,即超级用户等。)
\n你可以做:
$j = aws cloudformation describe-stack-events --stack-name some-stack-here | ConvertFrom-Json
$j.StackEvents | % { "{0} {1} {2}" -f $_.Timestamp, $_.Resourcestatus, $_.ResourceType }
Run Code Online (Sandbox Code Playgroud)
使用ConvertFrom-Jsoncmdlet 将命令结果存储到 powershell 对象中,然后您可以选择数组StackEvents并循环它以选择所需的值。
如果你想用一行来做:
(aws cloudformation describe-stack-events --stack-name some-stack-here | ConvertFrom-Json).StackEvents | %
{ "{0} {1} {2}" -f $_.Timestamp, $_.Resourcestatus, $_.ResourceType }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
789 次 |
| 最近记录: |