【问题标题】:Extracting some fields from an AWS JSON using Powershell使用 Powershell 从 AWS JSON 中提取一些字段
【发布时间】: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


    【解决方案1】:

    你可以这样做:

    $j = aws cloudformation describe-stack-events --stack-name some-stack-here | ConvertFrom-Json
    $j.StackEvents | % { "{0} {1} {2}" -f $_.Timestamp, $_.Resourcestatus, $_.ResourceType }
    

    使用ConvertFrom-Json cmdlet 将命令结果存储到powershell 对象中,然后您可以选择StackEvents 数组并循环它以选择所需的值。

    如果你想在一行中完成:

    (aws cloudformation describe-stack-events --stack-name some-stack-here | ConvertFrom-Json).StackEvents | %
    { "{0} {1} {2}" -f $_.Timestamp, $_.Resourcestatus, $_.ResourceType }
    

    【讨论】:

    • 太棒了,这就是我想要的。我知道 % 是 foreach,但是允许访问对象的语言功能是什么?我的意思是 {0}、-f 等。我不知道谷歌和 Powershell 在线文档有点稀疏/不是很清楚。 (好的,找到了,它是 -f 又名格式运算符:docs.microsoft.com/en-us/powershell/module/…)。酷,非常感谢,这就是我所缺少的!
    猜你喜欢
    • 1970-01-01
    • 2021-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 2021-12-28
    相关资源
    最近更新 更多