0

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.

4
  • What is in $item? Try: $item.date.ToString().SubString(0,10) Commented Dec 5, 2023 at 7:06
  • $item is part of a foreach loop interrogating the results of '$req = Invoke-RestMethod -Uri $download_root -Headers $headers -UseBasicParsing' / $jsonObject = $req.Data. Basically it is a json element. Commented Dec 5, 2023 at 21:40
  • 1
    OK, so adding the ToString parameter in has removed the initial error. I now get "ERROR: Exception calling "ParseExact" with "3" argument(s): "String '12/5/2023 ' was not recognized as a valid DateTime." While my local execution of this returns a string of '2023-12-05', the Azure Function execution returns '12/5/2023 ', so I am assuming there are some regional settings that are affecting the returned date. Further investigation under way. Commented Dec 6, 2023 at 0:46
  • That is correct, the 3rd parameter in [DateTime]::ParseExact sets the format/culture provider but as you apparently have different cultures and $item.date is probably already a [date] type (what is: $item.date.GetType()?), I would just let the PowerShell Get-Date cmdlet figure it out: get-date '12/5/2023' <=> get-date '2023-12-05' Commented Dec 6, 2023 at 7:56

2 Answers 2

1

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"
Sign up to request clarification or add additional context in comments.

Comments

-1

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:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.