Powershell 检查特定文本的url,如果匹配则下载,如果无法解析名称,则继续下一步

Powershell 检查特定文本的url,如果匹配则下载,如果无法解析名称,则继续下一步,powershell,error-handling,Powershell,Error Handling,我需要此脚本来执行以下操作: 验证$url的某些字符串或正则表达式,以验证我没有被重定向,例如有效、无效并重定向到主页。在下载之前, 即使无法解析远程名称,也要继续处理sources.txt:如果我正在尝试下载阻止列表,其中一些会定期受到DDoS攻击。 当前行为:脚本正在处理5个$URL中的3个。第三个错误是故意发到假地址的,第四个和第五个也是。但是,第四次和第五次尝试从未尝试过 try { import-Csv $intelSource | ForEach-Object {

我需要此脚本来执行以下操作:

验证$url的某些字符串或正则表达式,以验证我没有被重定向,例如有效、无效并重定向到主页。在下载之前, 即使无法解析远程名称,也要继续处理sources.txt:如果我正在尝试下载阻止列表,其中一些会定期受到DDoS攻击。 当前行为:脚本正在处理5个$URL中的3个。第三个错误是故意发到假地址的,第四个和第五个也是。但是,第四次和第五次尝试从未尝试过

try {
    import-Csv $intelSource | ForEach-Object {
         $storageDir = "$($intelDir)\$($_.threatType) $($_.threatSubtype)"
         $storageName = (Get-Culture).TextInfo
         $threat = $_.threatType + "_" + $_.threatSubtype    # Set this variable to save headaches passing it later
         $storageFile = "$($storageDir)\$($threat)_$($(get-date -f MM-dd-yy)).csv"    # Filename specified by sources.csv fields and today's date
         $url = $_.threatLocation
         if((Test-Path $storageDir) -eq 0) {
            mkdir $storageName.ToTitleCase($storageDir.ToLower());
            }
         # Begin Error Logging
         try {
            $intelCount++
            $webclient.DownloadFile($url,$storageFile)
         }
         catch {
            $intelErrorCount++
            $intelError = "" | Select "Time","Item","Message"
            $intelError.time = get-date -f G
            $intelError.item = $threat
            $intelError.message = $_.Exception.Message
            $errorLog += $intelError
            $intelError = $null
            echo $_.Exception|format-list -force | Out-File $intelDir\$threat"_ErrorLog_"$(get-date -f MM-dd-yy).txt -Append
            Write-Host $_.Exception
            Continue
         }
         # End Error Logging
         # Cleanup
         finally {
            $webclient.Dispose()
         }
         # Throttling (mainly future use with multiple sources)
         Start-Sleep 5
     }
}
finally { 
    if ($intelErrorCount -gt 0) {
        # Table Generation
        $errorLogTable = $errorLog | New-HTMLTable
        $HTML = New-HTMLHead
        $HTML += "<h3>Threat Intel Feed - Errors</h3>"
        $HTML += $errorLogTable | Close-HTML
        $email.Item('Subject') += "$($intelErrorCount) Error(s)"
        $email.Item('Body') += "$($intelErrorCount) of $($intelCount) sources failed. See attached for verbose log(s) `r`n`r`n $($HTML)"
        Get-ChildItem -Path $intelDir | Where {$_.Name -match "$($(get-date -f MM-dd-yy))"} | foreach{$_.fullname} | Send-MailMessage @email -bodyashtml
    }
    else {
        $email.Item('Subject') += "Success"
        $email.Item('Body') += "No errors for $($intelCount) sources."
        Send-MailMessage @email
    }
    $intelErrorCount = $null
    $intelCount = $null
    $errorLogTable = $null
    $errorLog = $null
}

问题在于Continue语句。Continue和Break在foreach对象脚本块中无法正常工作。更改为foreach语句:

foreach ($_ in (import-Csv $intelSource) {
  # Your existing code will probably work as is here
  # But you might want to rename $_ since it is an automatic variable
}

我很惊讶它不止一次有效。为什么每次尝试后都要处理$WebClient对象?@mathias-r-jessen我对编写这方面的脚本比较陌生,更不用说在powershell中了。在下载文件片段中看到了它,所以我将其合并。将它移动到当前保存报告元素的finally{}中,并不会改变它在发生错误后的行为方式,我实际上也刚刚解决了这个问题。我通过删除$ErrorActionPreference=Continue来设置它,显然Continue是默认的EAP值。然后我从Catch块中删除了Continue语句。你知道我走这条路线而不是通过你的建议可能会引起什么问题吗?我猜这意味着你的捕猎块不会再被执行了。如果是这样,则会跳过错误记录逻辑。我能看到的唯一的另一个区别是,如果您将Continue保留在原位,则始终执行start sleep而不是跳过。catch块仍在执行,它仍在生成文件和html项。好的,那么睡眠将是我想到的唯一区别。我很好奇,当您将EAP设置为Continue时,与其他一些值相比,有什么区别。好吧,Continue是默认值,只要Catch块中不存在Continue,它就会执行您认为的操作,因为它引用了一个我不必切换到的内部循环。将其设置为Stop显然会停止对任何终止错误和非终止错误的处理。我相信,静默继续会很有趣,因为我通过Catch块进行了错误处理,但应该可以防止任何错误被记录到$error中。