I'm trying to call Invoke-RestMethod
(and running into another unrelated problem) and keep getting errors when trying to pass it the result of a sub-command. Consider:
Invoke-RestMethod -Header @{"Iso-Date"=(Get-Date -Format "r")}
I expect this to run Get-Date -Format "r"
, assign its output string (the ISO-format date) to the hashtable value for Iso-Date
, then use that hashtable as the "Header" argument for the commandlet.
What actually happens is I get an error message
Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.String'.
This still happens if I assign it to a variable:
$date = (Get-Date -Format "r")
$date.GetType() # -- outputs "String"
Invoke-RestMethod -Header @{"Iso-Date"=$date) # -- Same error message.
If I set $date
to a fixed string value, I don't get the error anymore:
$date = "Fri, 20 Mar 2015 16:00:00 GMT"
$date.GetType() # -- outputs "String" as well (!!!)
Invoke-RestMethod -Header @{"Iso-Date"=$date) # -- works fine
What am I doing wrong?