【问题标题】:Post JSON object via Powershell通过 Powershell 发布 JSON 对象
【发布时间】:2023-04-06 07:59:02
【问题描述】:

我打算在运行 Powershell 脚本时得到以下输出

{
  "maintenance_window": {
    "type": "maintenance_window",
    "start_time": "2021-05-28T16:11:34.139Z",
    "end_time": "2021-05-28T16:26:34.139Z",
    "description": "Maintenance Window via Powershell",
    "services": [
      {
        "id": "XYZ",
        "type": "service_reference"
      }
    ]
  }
}

我的 .ps1 文件如下所示

Class service
{
    [String]$id
    [String]$type
}

Class maintenance_window
{
    [String]$type
    [String]$start_time
    [String]$end_time
    [String]$description
    [Collections.Generic.List[Service]] $services
}

Class maintenance_request
{ 
    [maintenance_window]$maintenance_window
}

Function Func()
{
    try
    {
        $uri = 'https://api.pagerduty.com/maintenance_windows'

        $start_date    = Get-Date 
        $start_time    = $start_date.ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffK")
        $end_time      = $start_date.AddMinutes(15).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss.fffK")

        $services      = New-Object Collections.Generic.List[service]
        $service       = New-Object service
        $service.id    = 'XYZ'
        $service.type  = 'service_reference' 

        $services.Add($service)

        $maintenance_window                =  New-Object maintenance_window
        $maintenance_window.type           = 'maintenance_window'
        $maintenance_window.start_time     =  $start_time 
        $maintenance_window.end_time       =  $end_time
        $maintenance_window.description    = 'Maintenance Window via Powershell'
        $maintenance_window.services       = $services

        # $maintenance_request                    = New-Object maintenance_request
        # $maintenance_request.maintenance_window  = $maintenance_window
        # $requestJson = $maintenance_request | ConvertTo-Json -Compress

        $requestJson = $maintenance_window | ConvertTo-Json -Compress

        Write-Host $requestJson
    }
    catch
    {
        Write-Host "Exception.`n $($_)"
    }
}

Func

这会产生输出

{
  "type": "maintenance_window",
  "start_time": "2021-05-28T16:37:28.519Z",
  "end_time": "2021-05-28T16:52:28.519Z",
  "description": "Maintenance Window via Powershell",
  "services": [
    {
      "id": "XYZ",
      "type": "service_reference"
    }
  ]
}

但是,当我在注释行下方取消注释时

 #$maintenance_request                    = New-Object maintenance_request
 #$maintenance_request.maintenance_window  = $maintenance_window
 #$requestJson = $maintenance_request | ConvertTo-Json -Compress

并在脚本中现有的行下方注释掉

$requestJson = $maintenance_window | ConvertTo-Json -Compress

我得到的输出看起来不正确

{
  "maintenance_window": {
    "type": "maintenance_window",
    "start_time": "2021-05-28T16:44:32.557Z",
    "end_time": "2021-05-28T16:59:32.557Z",
    "description": "Maintenance Window via Powershell",
    "services": [
      "service"
    ]
  }
}

谁能帮我理解为什么"services":["service"] 没有被序列化?

【问题讨论】:

  • 这是在哪个版本的 powershell 上运行的?内置 v5 和 pwsh 核心 v7+ 之间的一大区别是它处理 json 的能力
  • @Cpt.Whale 我使用的是 5.1.19041.906
  • 看来我找到了答案。我将不得不使用-Depth 标志。相同的默认值为 2
  • 很高兴你找到它!请注意,在版本 7 及更高版本中,默认值现在是 1024。版本 5.1 的硬限制为 -depth 101,但看起来这对您来说不是问题。

标签: powershell pagerduty


【解决方案1】:

添加-Depth 即可解决问题。

$requestJson = ($maintenance_window | ConvertTo-Json -Compress  -Depth 3)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-18
    • 2020-12-03
    • 1970-01-01
    相关资源
    最近更新 更多