다음과 같은 파일을 다운로드하는 powershell 스크립트가 있습니다.
powershell -Command `$progressPreference = 'silentlyContinue'; Invoke-WebRequest https://ftp.ncep.noaa.gov/data/nccf/com/cfs/prod/cfs/cfs.$m2/$m21z/6hrly_grib_04/pgbf$m2h.04.$m2h.grb2 -OutFile C:\OpenGrADS-2.2\data\cfs\cfs001.grb2`
다운로드하기 전에 파일이 있는지 확인해서 파일이 있으면 다운로드하고, 없으면 일정 시간 기다린 후 다시 시도하는 것이 가능한가요?
답변1
다음 PowerShell 스크립트가 작업을 수행합니다.
게시물에서 복사되었습니다. 재시도 지원이 포함된 원격 파일 다운로드:
# $ErrorActionPreference = "Stop"
function DownloadWithRetry([string] $url, [string] $downloadLocation, [int] $retries)
{
while($true)
{
try
{
Invoke-WebRequest $url -OutFile $downloadLocation
break
}
catch
{
$exceptionMessage = $_.Exception.Message
Write-Host "Failed to download '$url': $exceptionMessage"
if ($retries -gt 0) {
$retries--
Write-Host "Waiting 10 seconds before retrying. Retries left: $retries"
Start-Sleep -Seconds 10
}
else
{
$exception = $_.Exception
throw $exception
}
}
}
}
#
# Usage
DownloadWithRetry -url "http://example.com/file.zip" -downloadLocation "C:\Downloads\file.zip" -retries 6