I'm populating $dataTable this way:
# Use Invoke-WebRequest to send a GET request to the URL
try {
$response = Invoke-WebRequest -Uri $url -Method Get -UseBasicParsing
# Convert the JSON response to a PowerShell object
$jsonData = $response.Content | ConvertFrom-Json
# Create a DataTable to store the data
$dataTable = New-Object System.Data.DataTable
# Check if the JSON data is not empty and has elements
if ($jsonData -and $jsonData.count -gt 0) {
# Dynamically add columns based on the JSON object properties
foreach ($property in $jsonData[0].PSObject.Properties.Name) {
$col = New-Object System.Data.DataColumn $property, ([string])
$dataTable.Columns.Add($col)
}
# Add rows to the DataTable
foreach ($item in $jsonData) {
$row = $dataTable.NewRow()
foreach ($property in $jsonData[0].PSObject.Properties.Name) {
$row[$property] = $item.$property
}
$dataTable.Rows.Add($row)
}
}
I then attempt to sort them using the line below, but the columns "Truck", "DriverName", and "DeliveryDate" are empty in $sortedData, even though they are populated in $dataTable. See pics for example.
Sorting code:
$sortedData = $dataTable | Sort-Object WarehouseCode, Truck, DriverName, SalesOrderNo, LineNumber, DeliveryDate , pickstatus, picktime, pickcomplete_time, ItemCode, QuantityPicked, QuantityOrdered