0

I intend to have below output when I run Powershell script

{
  "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"
      }
    ]
  }
}

My .ps1 file looks like as below

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

This produces output as

{
  "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"
    }
  ]
}

However, when I uncomment below commented lines

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

and comment out below existing line in the script

$requestJson = $maintenance_window | ConvertTo-Json -Compress

I get output which does not looks correct

{
  "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"
    ]
  }
}

Can anyone help me here to understand why "services":["service"] is not getting serialized ?

sblive
  • 105
  • 9
  • What version of powershell is this running on? One of the big differences between the built-in v5 and pwsh core v7+ is how well it handles json – Cpt.Whale May 28 '21 at 17:17
  • @Cpt.Whale I am using 5.1.19041.906 – sblive May 28 '21 at 17:21
  • looks like I found the answer. I will have to use `-Depth` flag. Default value for the same is 2 – sblive May 28 '21 at 17:33
  • 1
    Glad you found it! Note that in version 7 and up, the default value is now 1024. Version 5.1 has a hard limit at `-depth 101`, but it doesn't look like that will be an issue for you here. – Cpt.Whale May 28 '21 at 18:43

1 Answers1

0

Adding -Depth solves the issue.

$requestJson = ($maintenance_window | ConvertTo-Json -Compress  -Depth 3)
sblive
  • 105
  • 9