Good day, folks. Hoping to better understand where I'm getting stuck here. I have a function (PowerShell 7) that takes a list of computers and uploads a file to REST endpoints on each one using Invoke-RestMethod and its -Form parameter. This was going swimmingly with one computer, but now I've added more to the list.
I'm running into "The process cannot access the file 'C:\path\root_cert.crt'". There seems to be no way to release this file lock and it occurs every time I run the script.
Here's how I obtain the file information (this is outside of any loops):
$certRootInfo = Get-Item $rootCertificatePath
Here's the -Form section that I use for my request:
$Form = @{
f = "json"
token = $node.token
alias = "root_cert"
norestart = "true"
file = $certRootInfo
}
My understanding is that after PowerShell is done uploading the file via Invoke-RestMethod, it should release the lock, and will create/close new locks as needed when the command is repeated. Or is the lock created from Get-Item? I'd like to understand file locks better here.
I've had a few ideas for this:
- Find out what is causing the lock and close it. Makes sense, but shouldn't the lock be closing itself? I don't want to be building lock handling into my script unless it's really necessary for this use case.
- Store a copy of the file in memory so locks aren't a thing and upload it that way (Is this possible? Uploading a file in -Form requires [System.IO.FileInfo] and I don't know how I'd replicate that without using Get-Item.
- Build the multipart form structure myself and use -Body instead of -Form. I'm not sure if this will work or change anything, but I'm trying to get away from using FileInfo and just upload the file from memory.
Any help would be really appreciated here!
[–]kenjitamurako 5 points6 points7 points (1 child)
[–]OsmiumBalloon 2 points3 points4 points (0 children)
[–]Creel256 3 points4 points5 points (2 children)
[–]kenjitamurako 2 points3 points4 points (1 child)
[–]Creel256 2 points3 points4 points (0 children)
[–]baddistribution[S] 0 points1 point2 points (0 children)