Reputation: 35
I have a PowerShell script that executes perfectly locally via VS Code, but when I deploy it to an Azure Function it fails.
The code in question is as follows:
$creation_date = $item.date.SubString(0,10)
$parsedDate = [DateTime]::ParseExact($creation_date, 'yyyy-MM-dd', $null)
When I run it locally, I receive the following output
2023-12-04
04/12/2023 12:00:00 AM
When I run it as an Azure Function, I get the following error
ERROR: Method invocation failed because [System.DateTime] does not contain a method named 'Substring'. Exception : Type : System.Management.Automation.RuntimeException ErrorRecord : Exception : Type :System.Management.Automation.ParentContainsErrorRecordException Message : Method invocation failed because [System.DateTime] does not contain a method named 'Substring'. HResult : -2146233087 CategoryInfo : InvalidOperation: (:) [], ParentContainsErrorRecordException FullyQualifiedErrorId : MethodNotFound InvocationInfo : ScriptLineNumber : 68 OffsetInLine : 1 HistoryId : -1 ScriptName : C:\home\site\wwwroot\TimerTrigger1\run.ps1 Line : $creation_date = $item.date.Substring(0,10) PositionMessage : At C:\home\site\wwwroot\TimerTrigger1\run.ps1:68 char:1 + $creation_date = $item.date.Substring(0,10) +
From VS Code, I am executing the script in PowerShell Core 7.3.10
In Azure Functions, my runtime stack is shown as PowerShell - 7.2
I'm assuming that there is a runtime incompatibility, but I'm unsure where to head next. Any advice would be greatly appreciated.
Upvotes: 0
Views: 327
Reputation: 35
My problem was definitely related to the Locale. I added some additional logging and found the format of the date value being returned, after which I added different code to achieve the result I was after.
$creation_date = $item.date.ToString("yyyy-MM-dd").Substring(0,10)
$parsedDate = [DateTime]::ParseExact($creation_date, 'yyyy-MM-dd', $null)
$creation_time = $item.date.ToString().Substring(10,8)
$creation_time = $creation_time.trim()
$creation_time_length = $creation_time.Length
If ($creation_time_length -eq 7)
{
$creation_time = "0" + $creation_time
}
$creation_time_format = $creation_time -replace ':','.'
$Filename = $creation_date + '_' + $creation_time_format + '_' + "$recording_id.mp3"
Upvotes: 1
Reputation: 11373
You can use below code to get the required result:
using namespace System.Net
param($Request, $TriggerMetadata)
Write-Host "PowerShell HTTP trigger function processed a request."
$rithitem = @{
date = '2023-12-03'
}
if ($rithitem.date -is [System.String]) {
$creation_date = $rithitem.date.Substring(0, 10)
} elseif ($rithitem.date -is [System.DateTime]) {
$creation_date = $rithitem.date.ToString('yyyy-MM-dd')
} else {
Write-Error "Unsupported type for $($rithitem.date)"
}
$parsedDate = [DateTime]::ParseExact($creation_date, 'yyyy-MM-dd', $null)
Write-Output "Rithwik, Original date is: $($rithitem.date)"
Write-Output "Rithwik, Parsed date: $parsedDate"
$body = "Hello, Rithwik Bojja. This HTTP triggered function executed successfully."
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = $body
})
After Deployed to Azure.
Output:
Upvotes: -1