Устранение неполадок, связанных с регистрацией данных в Web Scrape - PullRequest
0 голосов
/ 04 мая 2020

Я пытаюсь написать многосерверный веб-скребок. Мне посоветовали очистить мой код с помощью ряда массивов и экземпляров с помощью циклов, но я недостаточно хорошо разбираюсь в Powershell. Существует также «сбой», когда отметка времени не отображается ни в одном из, возможно, 100 WebScrapes. Powershell не отображает ошибок с текущим кодом. Я был бы очень признателен за любую помощь, которая предлагается.

$server5 = Invoke-WebRequest -URI 'http://10.0.0.206'
$server6 = Invoke-WebRequest -URI 'http://10.0.0.232'
$server7 = Invoke-WebRequest -URI 'http://10.0.0.192'

$pocpath = "C:\Users\Capstone\Proof of Concept\"
$modfile4 = "Module 4.txt"
$modfile5 = "Module 5.txt"
$modfile6 = "Module 6.txt"
$modfile7 = "Module 7.txt"

while($server4) {
    $timestamp = Get-Date -Format "MM/dd/yyyy hh:mm tt"
    $lines_mod4 = Get-Content -Path "$pocpath\$modfile4" | Measure-Object -Line
    #$lines_mod5 = Get-Content -Path "$pocpath\$modfile5" | Measure-Object -Line
    #$lines_mod6 = Get-Content -Path "$pocpath\$modfile6" | Measure-Object -Line
    #$lines_mod7 = Get-Content -Path "$pocpath\$modfile7" | Measure-Object -Line

    if ($lines_mod4.Lines -gt 10085) {
        Clear-Content "$pocpath\$modfile4"
        Clear-Content "$pocpath\$modfile5"
        Clear-Content "$pocpath\$modfile6"
        Clear-Content "$pocpath\$modfile7"

        #Add two blank lines at the beginning of the file
        Add-Content -Path "$pocpath\$modfile4" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile5" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile6" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile7" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile4" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile5" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile6" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile7" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile4" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile5" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile6" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile7" -Value "$timestamp"-Encoding Ascii
        $server4 = Invoke-WebRequest -URI 'http://10.0.0.177'
        $server5 = Invoke-WebRequest -URI 'http://10.0.0.206'
        $server6 = Invoke-WebRequest -URI 'http://10.0.0.232'
        $server7 = Invoke-WebRequest -URI 'http://10.0.0.192'
        $server4.Content | Out-File -FilePath "$pocpath\$modfile4" -Encoding Ascii -Append
        $server5.Content | Out-File -FilePath "$pocpath\$modfile5" -Encoding Ascii -Append
        $server6.Content | Out-File -FilePath "$pocpath\$modfile6" -Encoding Ascii -Append
        $server7.Content | Out-File -FilePath "$pocpath\$modfile7" -Encoding Ascii -Append
        Start-Sleep -Seconds 60
    }
    elseif ((Get-Content "$pocpath\$modfile4") -eq $Null) {
        #Add two blank lines at the beginning of the file
        Add-Content -Path "$pocpath\$modfile4" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile5" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile6" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile7" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile4" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile5" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile6" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile7" -Value ''-Encoding ASCII
        Add-Content -Path "$pocpath\$modfile4" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile5" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile6" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile7" -Value "$timestamp"-Encoding Ascii
        $server4 = Invoke-WebRequest -URI 'http://10.0.0.177'
        $server5 = Invoke-WebRequest -URI 'http://10.0.0.206'
        $server6 = Invoke-WebRequest -URI 'http://10.0.0.232'
        $server7 = Invoke-WebRequest -URI 'http://10.0.0.192'
        $server4.Content | Out-File -FilePath "$pocpath\$modfile4" -Encoding Ascii -Append
        $server5.Content | Out-File -FilePath "$pocpath\$modfile5" -Encoding Ascii -Append
        $server6.Content | Out-File -FilePath "$pocpath\$modfile6" -Encoding Ascii -Append
        $server7.Content | Out-File -FilePath "$pocpath\$modfile7" -Encoding Ascii -Append
        Start-Sleep -Seconds 60
    } 
    else {
        Add-Content -Path "$pocpath\$modfile4" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile5" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile6" -Value "$timestamp"-Encoding Ascii
        Add-Content -Path "$pocpath\$modfile7" -Value "$timestamp"-Encoding Ascii
        $server4 = Invoke-WebRequest -URI 'http://10.0.0.177'
        $server5 = Invoke-WebRequest -URI 'http://10.0.0.206'
        $server6 = Invoke-WebRequest -URI 'http://10.0.0.232'
        $server7 = Invoke-WebRequest -URI 'http://10.0.0.192'
        $server4.Content | Out-File -FilePath "$pocpath\$modfile4" -Encoding Ascii -Append
        $server5.Content | Out-File -FilePath "$pocpath\$modfile5" -Encoding Ascii -Append
        $server6.Content | Out-File -FilePath "$pocpath\$modfile6" -Encoding Ascii -Append
        $server7.Content | Out-File -FilePath "$pocpath\$modfile7" -Encoding Ascii -Append
        Start-Sleep -Seconds 60
    }
}
...