1

Is it possible to create and write to a local file in a Powershell Core Azure Function?

Here is an extract from the script I'm running (using the blob storage trigger).

param([byte[]] $dockerFile, $TriggerMetadata)

Write-Host "Processing updated DockerFile: $($TriggerMetadata.dockerFile)"

Write-Host "---------------------------------------------------------"
Write-Host "Create local Docker File using contents from blob storage"
New-Item -Name $TriggerMetadata.snapshotId -ItemType "file"
[io.file]::WriteAllBytes($TriggerMetadata.snapshotId, $dockerFile)
Get-Content $TriggerMetadata.snapshotId
Write-Host "---------------------------------------------------------"

Using New-Item throws this -> ERROR: Could not find file 'D:\home\site\wwwroot\<filename>'.

And Using [io.file]::WriteAllBytes throws this -> Exception calling "WriteAllBytes" with "2" argument(s): "Could not find file 'D:\home\site\wwwroot\<filename>'."

Do Azure Functions have permissions to create files locally?

Jamie Peacock
  • 372
  • 4
  • 15
  • 1
    Could you try to get the temp folder and write there? https://stackoverflow.com/a/34559554/1537195 `$parent = [System.IO.Path]::GetTempPath()` – silent Sep 06 '19 at 07:17
  • @silent This does work! (I changed it a little bit by using the `New-TemporaryFile` command and then passing the `FullName` into `WriteAllBytes` but I think that is a slightly more optimised version of the same answer! If you want to put an answer down I'll accept it, or I'll put my solution down if not – Jamie Peacock Sep 06 '19 at 08:15
  • glad that worked! see below – silent Sep 06 '19 at 08:29

1 Answers1

1

As discussed: Use the New-TemporaryFile command to create a new file in the temp location. Then you can write to it.

$TempFile = New-TemporaryFile
[io.file]::WriteAllBytes($TempFile.FullName, $dockerFile)
silent
  • 14,494
  • 4
  • 46
  • 86