Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Powershell发布JSON对象_Powershell_Pagerduty - Fatal编程技术网

通过Powershell发布JSON对象

通过Powershell发布JSON对象,powershell,pagerduty,Powershell,Pagerduty,我打算在运行Powershell脚本时获得以下输出 { "maintenance_window": { "type": "maintenance_window", "start_time": "2021-05-28T16:11:34.139Z", "end_time": "2021-05-28T16:26:34.139Z",

我打算在运行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"
    ]
  }
}

这里有谁能帮我理解为什么
“服务”:[“服务”]
没有被序列化?

添加
-Depth
解决了这个问题

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

它运行在哪个版本的powershell上?内置v5和pwsh core v7+之间的一个重大区别是它的处理能力json@Cpt.Whale我正在使用5.1.19041.906书籍,就像我找到答案一样。我必须使用
-Depth
标志。相同的默认值为2Glad,您可以找到它!请注意,在版本7及更高版本中,默认值现在是1024。5.1版在深度101上有一个硬限制,但在这里,这似乎不是您的问题。