【发布时间】:2018-06-01 17:07:54
【问题描述】:
既然 Powershell 是开源和跨平台的(使用 Powershell Core),我想我会再试一次。
我过去曾使用过,并且在某些时候已经弄清楚管道是如何工作的,但它并不是超级直观。应该是,但不是,所以我有点卡住了......
手头的任务:从命令的 JSON 输出中解析和打印几个字段。我可能可以用老式的方式来做,用外部命令和字符串处理,à la bash,但我想学习如何做到这一点Powershell 方式™。
澄清一下,我想在管道中以交互方式进行。我不想编写脚本,我想学习如何在一个单独的过程中进行这种处理—— liner(或最多 2 个,但基本上使用 Powershell 作为 REPL,而不是作为脚本工具)。
Bash 命令:
aws cloudformation describe-stack-events --stack-name some-stack-here | jq ".StackEvents[] | [.Timestamp, .ResourceStatus, .ResourceType, .ResourceStatusReason] | join(\" \")"
它的作用是获取输入 JSON 并仅打印我感兴趣的 3 个字段。
输入 JSON:
{
"StackEvents": [
{
"StackId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",
"EventId": "some-event-id-here",
"ResourceStatus": "UPDATE_COMPLETE",
"ResourceType": "AWS::CloudFormation::Stack",
"Timestamp": "some-date-here",
"StackName": "some-stack-here",
"PhysicalResourceId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",
"LogicalResourceId": "some-stack-here"
},
{
"StackId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",
"EventId": "some-event-id-here",
"ResourceStatus": "UPDATE_COMPLETE_CLEANUP_IN_PROGRESS",
"ResourceType": "AWS::CloudFormation::Stack",
"Timestamp": "some-date-here",
"StackName": "some-stack-here",
"PhysicalResourceId": "arn:aws:cloudformation:some-region-here:some-number-here:stack/some-stack-here/some-id-here",
"LogicalResourceId": "some-stack-here"
}
]
}
命令输出:
"some-date-here UPDATE_COMPLETE AWS::CloudFormation::Stack "
"some-date-here UPDATE_COMPLETE_CLEANUP_IN_PROGRESS AWS::CloudFormation::Stack "
(我认为这应该在 Stackoverflow 上,因为该主题涉及对 Powershell 概念的非常扎实的理解,包括 .NET 对象,更接近于编程而不是系统管理,即 SuperUser 左右。)
【问题讨论】:
标签: json powershell