Загрузка данных


# Устанавливаем кодировку и подгружаем системные инструменты
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Add-Type -AssemblyName System.Web

# Настройки
$url = "https://main.atb.su/normdocs/Lists/Docs/ByCategory.aspx?C=6&p_RegDate=20231219%2015%3a00%3a00&p_Created=20231225%2004%3a44%3a04&RootFolder=%2fnormdocs%2fLists%2fDocs%2fpub&PageFirstRow=1&FilterField1=DocCategoryId&FilterValue1=6&View={71C6C749-C001-4601-B766-36AFFD10DEE8}"
$dir = "Downloads_Bank_Docs"

if (!(Test-Path $dir)) { New-Item -ItemType Directory -Path $dir | Out-Null }

Write-Host "--- Подключаюсь к SharePoint ---" -ForegroundColor Cyan

try {
    $page = Invoke-WebRequest -Uri $url -UseDefaultCredentials -TimeoutSec 30
    # Ищем строки таблицы
    $rows = [regex]::Matches($page.Content, '(?is)<tr[^>]*>(.*?)</tr>')
    
    foreach ($row in $rows) {
        $html = $row.Groups[1].Value
        
        # Если документ "Действителен"
        if ($html -contains "Действителен") {
            # Ищем ссылку на файл
            if ($html -match 'href="([^"]+?\.(pdf|docx?|xlsx?|rtf))"') {
                $rawLink = $matches[1]
                $cleanLink = [System.Net.WebUtility]::HtmlDecode($rawLink)
                
                # Формируем полный URL
                $fullUrl = if ($cleanLink.StartsWith("http")) { $cleanLink } else { "https://main.atb.su$($cleanLink.StartsWith('/') ? '' : '/')$cleanLink" }
                
                $fileName = [System.Net.WebUtility]::UrlDecode($fullUrl.Split('/')[-1])
                $fileName = $fileName -replace '[\\\/\:\*\?\"\<\>\|]', '_'
                $path = Join-Path $dir $fileName

                if (!(Test-Path $path)) {
                    Write-Host "[+] Качаю: $fileName" -ForegroundColor Green
                    Invoke-WebRequest -Uri $fullUrl -OutFile $path -UseDefaultCredentials
                }
            }
        }
    }
    Write-Host "--- Готово! Проверь папку $dir ---" -ForegroundColor White
} catch {
    Write-Host " Ошибка: $($_.Exception.Message)" -ForegroundColor Red
}

Read-Host "Нажми Enter"