0

In PowerShell 7.3.4, I'm making a web request, writing the response content to a file, then forwarding the file via another web request.

$response1 = Invoke-WebRequest @getParam
[io.file]::WriteAllBytes(($local + $filename), $response1.content)
$postParam.Form = @{
   'file'   = ($local + $filename)
}
$response2 = Invoke-WebRequest @postParam

Sporadically, I'm getting:

Exception calling "WriteAllBytes" with "2" argument(s): "The process cannot access the file '\\server\folder\File0002.dat' because it is being used by another process."

Is the [io.file]::WriteAllBytes keeping the file open for some reason? From what I read, it should be closed instantly to allow the next step. I'm not using the Create() method, so I don't think I need to worry about streams. Is there a better file writer that's more efficient?

This doesn't happen all the time, maybe 1 time in 20.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • It's version 7.3.4. There's no other code except `Try`/`Catch`, and the `Catch` is what's displaying the error. After the file writes to the local folder, it loads to the form, and there's no other interaction. I'm not doing anything after the second web request except logging results to a separate text file. – Jerkle Berry May 30 '23 at 20:06
  • _Something_ in your session prior to invocation could conceivably have the file open. You could try to narrow the problem down by checking whether it ever happens in a pristine new session, or if it happens when you call your code _repeatedly_. – mklement0 May 30 '23 at 20:11
  • 2
    Why not use the [`-OutFile`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.3#-outfile) parameter? Should be more efficient in terms of memory usage. – zett42 May 30 '23 at 21:04
  • 1
    Are you writing to an existing file, or a new file? If you’re overwriting a file that already exists it’s possible other background tasks (e.g. shonky antivirus products) have a temporary lock on the file… – mclayton May 30 '23 at 22:53
  • @mclayton Almost always new files. The ones generating the error are new. It runs on a server with SentinelOne. Maybe it's that plus network lag. – Jerkle Berry May 31 '23 at 00:40
  • Unless you need an actual copy of the content in a file (in that case, you might also consider to use [`Tee-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/tee-object)), there is no reason why not doing this all in memory. – iRon May 31 '23 at 06:31
  • @iRon I tried that, but [I was unable](https://stackoverflow.com/q/75992344/7448498) to convert the response's byte array to System.IO.FileInfo without having to write a file to disk. – Jerkle Berry May 31 '23 at 06:41
  • 1
    I see (need to check what you trying there as I still think it should be possible). Anyways, you might consider to use a *unique* file instead ([`New-TemporaryFile`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/new-temporaryfile)) and clean it up (if possible) afterwards. – iRon May 31 '23 at 07:01

0 Answers0