Q:

Powershell Post request unexpected errors

This is my fist time doing a post request in Powershell. I have a fairly large request with a lot of variables, I am getting some errors when I execute.
Here is my formula
 function PostCustomerUpdate($cust){
 $phone = -join($cust.AreaCode,$cust.Phone)
 Invoke-WebRequest -Uri myAPIURL -ContentType "application/json" -Method POST -Body {
"Options": 2,
 "ModifiedCustomer": {
 "CustomerID": $cust.Id,
 "ADObjectGuid": $cust.ADGUID,
    "AppID": $cust.AppID,
    "AppCustomerID": $cust.AppCustomerID,
    "OriginatingSystemID": $cust.OriginatingSystemID,
    "OriginatingSystemDescription": $cust.OriginatingSystemDescription,
    "Action": $cust.Action,
    "Title": $cust.Title,
    "Suffix": $cust.Suffix,
    "Gender": $cust.Gender,
    "FirstName": $cust.FirstName,
    "LastName": $cust.LastName,
    "CustomerTypeDescription": $cust.CustomerTypeDescription,
    "CustomerStatusTypeExtendedDescription": $cust.CustomerStatusTypeExtendedDescription,
    "CustomerStatusTypeDescription": $cust.CustomerStatusTypeDescription,
    "CustomerStatusReasonTypeDescription": $cust.CustomerStatusReasonTypeDescription,
    "PreferredLanguage": $cust.PreferredLanguage,
    "Applications":[
         {
            "ApplicationCustomerLoginId": $cust.EmailAddress,
            "ApplicationCustomerID": $phone,
            "Action": 1,
         }
     ]
     }
   }
} 

I get the following errors when I try to run
 At line:45 char:14
+     "Options": 2,
+              ~
Unexpected token ':' in expression or statement.
At line:47 char:21
+     "CustomerID": $cust.Id,
+                     ~
Unexpected token ':' in expression or statement.
At line:66 char:35
+             "ApplicationCustomerLoginId": $cust.EmailAddress,
+                                         ~
Unexpected token ':' in expression or statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnexpectedToken 

I have tried pretty much everything i know and I am at a loss.

A:

You can create JSON bodies with PSObjects using hashtables and lists. A list in JSON can be replicated in PowerShell with @() and a dictionary is done with @{}. Finally, you can convert it with ConvertTo-JSON but it is not mandatory.
$body = [PSCustomObject] @{
    Options = 2
    ...
    ModifiedCustomer = @{
        CustomerID = $cust.Id
        ADObjectGuid = $cust.ADGUID
        AppID = $cust.AppID
        Applications = @(
            @{
                ApplicationCustomerLoginId = $cust.EmailAddress
                ApplicationCustomerID = $phone
                Action = 1
            }
        )
    }
} | ConvertTo-Json -Depth 3

In the WebRequest you use as usual.
Invoke-WebRequest -Uri myAPIURL -ContentType "application/json" -Method POST -Body $body

