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 ?