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
})