Powershell Azure-Function to generate and output a file

David G 21 Reputation points
2020-10-05T06:25:21.567+00:00

Hi,

I'm trying to generate a .ics file using Azure-Function with PowerShell, using HttpTrigger.
The code is relatively simple, however, I failed to have the file being returned

using namespace System.Net  
  
# Input bindings are passed in via param block.  
param($Request, $TriggerMetadata)  
  
# Write to the Azure Functions log stream.  
Write-Host "PowerShell HTTP trigger function processed a request."  
  
# Interact with query parameters or the body of the request.  
$Subject = $Request.Query.Subject  
  
$StartDate = [datetime]::parseexact($Request.Query.StartDate, 'dd-MMM-yyyy', $null)  
  
$icsDateFormat = "yyyyMMddTHHmmssZ"  
  
        $event = @"  
BEGIN:VCALENDAR  
VERSION:2.0  
METHOD:PUBLISH  
PRODID:-//JHP//We love PowerShell!//EN  
BEGIN:VEVENT  
UID: $([guid]::NewGuid())  
CREATED: $((Get-Date).ToUniversalTime().ToString($icsDateFormat))  
DTSTAMP: $((Get-Date).ToUniversalTime().ToString($icsDateFormat))  
LAST-MODIFIED: $((Get-Date).ToUniversalTime().ToString($icsDateFormat))  
CLASS:$Visibility  
CATEGORIES:$($Category -join ',')  
SEQUENCE:0  
DTSTART: $((Get-Date).ToUniversalTime().ToString($icsDateFormat))  
DTEND: $((Get-Date).Add('01:00:00').ToUniversalTime().ToString($icsDateFormat))  
DESCRIPTION: $StartDate  
SUMMARY: $Subject  
LOCATION: $Location  
TRANSP:$(if($ShowAs -eq 'Free') {'TRANSPARENT'})  
END:VEVENT  
END:VCALENDAR  
"@  
#Remove-Item event.ics  
  
$myFileOutput = $event | Out-File -FilePath event3.ics -Encoding utf8 -Force  
  
#(New-Object System.Net.Webclient).DownloadFile(event2.ics)  
  
# Associate values to output bindings by calling 'Push-OutputBinding'.  
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{  
    StatusCode = [HttpStatusCode]::OK  
    Body       = 'Done!'  
})  

How can I make the function return and download the file?

Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
4,540 questions
0 comments No comments
{count} votes

Accepted answer
  1. Pramod Valavala 20,606 Reputation points Microsoft Employee
    2020-10-06T07:17:04.607+00:00

    To have the browser handle the response as a file download, simply set the content type to application/octet-stream. So, even the following should trigger a download

    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
         StatusCode = [HttpStatusCode]::OK
         ContentType = "application/octet-stream"
         Body       = 'Done!'
     })
    

    In your case, you don't really need to save it to a file. Instead, simply return the $event in the body itself, like below

    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
      StatusCode  = [HttpStatusCode]::OK
      ContentType = "application/octet-stream"
      Headers     = @{
        'Content-Disposition' = 'attachment; filename="event.ics"' # To set the filename of the download
      }
      Body        = $event
    })
    

    But if you were just wondering how to return contents from a file, you would just have to read the file into a variable and return that instead, like below

    $data = Get-Content event.ics -Raw
    
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
      StatusCode  = [HttpStatusCode]::OK
      ContentType = "application/octet-stream"
      Headers     = @{
        'Content-Disposition' = 'attachment; filename="event.ics"' # To set the filename of the download
      }
      Body        = $data
    })
    

    For binary files, you would have to use something like this

    $data = [System.IO.File]::ReadAllBytes("Microsoft.VisualBasic.dll")
    
    Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
      StatusCode  = [HttpStatusCode]::OK
      ContentType = "application/octet-stream"
      Headers     = @{
        'Content-Disposition' = 'attachment; filename="Microsoft.VisualBasic.dll"' # To set the filename of the download
      }
      Body        = $data
    })
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful