Talked about this recently here, this is creating an object and looping to run a command:
Convertfrom-csv - PowerShell Help - PowerShell Forums
As a note, a date is a string coming from a CSV or a manual object unless you cast or parse it to a DateTime. If the API requires the date to be in a specific format, need to make it a date time first to do date formatting, math, etc.
$etls = @'
Name,StartDate,EndDate
ETL1,3/5/2023 16:00,3/7/2023 18:00
ETL2,3/6/2023 13:00,3/7/2023 16:00
ETL3,3/7/2023 15:00,3/7/2023 19:00
'@ | ConvertFrom-Csv
$results = foreach ($etl in $etls) {
$body = @{
start_date = (Get-Date $etl.StartDate -Format "o")
end_date = (Get-Date $etl.EndDate -Format "o")
}
$invokeWebRequestSplat = @{
Uri = 'https://myapi/v1/starships'
Body = ($body | ConvertTo-Json)
Method = 'Post'
ContentType = 'application/json'
}
Invoke-WebRequest @invokeWebRequestSplat
}
$results