param( [Parameter(Mandatory = $true)][string]$Url, [Parameter(Mandatory = $true)][string]$Out, [Parameter(Mandatory = $true)][string]$Lock ) # Download notify.exe with curl to a temp file, then atomic-rename on success. # Always remove the lock dir at the end (success or failure). $tmp = $Out + ".downloading" try { & curl.exe -fsSL $Url -o $tmp if ((Test-Path -LiteralPath $tmp) -and ((Get-Item -LiteralPath $tmp).Length -gt 0)) { Move-Item -LiteralPath $tmp -Destination $Out -Force } } finally { Remove-Item -LiteralPath $tmp -Force -ErrorAction SilentlyContinue Remove-Item -LiteralPath $Lock -Recurse -Force -ErrorAction SilentlyContinue }