我们使用PowerShell脚本,有人将其从Microsoft下载新的Windows Defender病毒定义到网络位置。有四个文件可供下载。问题是,有时一个或多个下载因网络或网站问题而失败。所以,我希望任何下载都无法自动重试,直到成功。下面是脚本的相关部分。任何帮助将不胜感激。
# Source Addresses - Defender for Windows 10, 8.1 ################################
$sourceAVx86 = "http://go.microsoft.com/fwlink/?LinkID=121721&arch=x86"
$sourceNISx86 = "http://go.microsoft.com/fwlink/?LinkID=187316&arch=x86&nri=true"
$sourceAVx64 = "http://go.microsoft.com/fwlink/?LinkID=121721&arch=x64"
$sourceNISx64 = "http://go.microsoft.com/fwlink/?LinkID=187316&arch=x64&nri=true"
# Web client #####################################################################
$wc = New-Object System.Net.WebClient
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
# x86 AV #########################################################################
$Dest = "$Destination\x86\" + 'mpam-fe.exe'
$wc.DownloadFile($sourceAVx86, $Dest)
# x86 NIS ########################################################################
$Dest = "$Destination\x86\" + 'nis_full.exe'
$wc.DownloadFile($sourceNISx86, $Dest)
# x64 AV #########################################################################
$Dest = "$Destination\x64\" + 'mpam-fe.exe'
$wc.DownloadFile($sourceAVx64, $Dest)
# x64 NIS ########################################################################
$Dest = "$Destination\x64\" + 'nis_full.exe'
$wc.DownloadFile($sourceNISx64, $Dest)
答案 0 :(得分:2)
一种方法是测试目的地是否存在文件。 如果该文件不存在,则重试下载。我输入一个变量来设置在继续之前尝试下载的次数。
$maxAttempts = 5 #set the maximum number of attempts in case the download will never succeed.
$sourceAVx86 = "http://go.microsoft.com/fwlink/?LinkID=121721&arch=x86"
$sourceNISx86 = "http://go.microsoft.com/fwlink/?LinkID=187316&arch=x86&nri=true"
$sourceAVx64 = "http://go.microsoft.com/fwlink/?LinkID=121721&arch=x64"
$sourceNISx64 = "http://go.microsoft.com/fwlink/?LinkID=187316&arch=x64&nri=true"
$wc = New-Object System.Net.WebClient
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
#Define destination file paths:
$DestAVx86 = "$Destination\x86\" + 'mpam-fe.exe'
$DestNISx86 = "$Destination\x86\" + 'nis_full.exe'
$DestAVx64 = "$Destination\x64\" + 'mpam-fe.exe'
$DestNISx64 = "$Destination\x64\" + 'nis_full.exe'
#Delete old versions of the files if they exist
if (Test-Path $DestAVx86) {
Remove-Item $DestAVx86
}
if (Test-Path $DestNISx86) {
Remove-Item $DestNISx86
}
if (Test-Path $DestAVx64) {
Remove-Item $DestAVx64
}
if (Test-Path $DestNISx64) {
Remove-Item $DestNISx64
}
$attemptCount = 0
Do {
$attemptCount++
$wc.DownloadFile($sourceAVx86, $Dest)
} while (((Test-Path $DestAVx86) -eq $false) -and ($attemptCount -le $maxAttempts))
$attemptCount = 0
Do {
$attemptCount++
$wc.DownloadFile($sourceNISx86, $Dest)
} while (((Test-Path $DestNISx86) -eq $false) -and ($attemptCount -le $maxAttempts))
$attemptCount = 0
Do {
$attemptCount++
$wc.DownloadFile($sourceAVx64, $Dest)
} while (((Test-Path $DestAVx64) -eq $false) -and ($attemptCount -le $maxAttempts))
$attemptCount = 0
Do {
$attemptCount++
$wc.DownloadFile($sourceNISx64, $Dest)
} while (((Test-Path $DestNISx64) -eq $false) -and ($attemptCount -le $maxAttempts))