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


# Устанавливаем кодировку и подгружаем системные инструменты
[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 "`n--- Подключаюсь к 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 -match "Действителен") {
            # Ищем ссылку на файл
            if ($html -match 'href="([^"]+?\.(pdf|docx?|xlsx?|rtf))"') {
                $rawLink = $matches[1]
                $cleanLink = [System.Net.WebUtility]::HtmlDecode($rawLink)
                
                # Формируем полный URL (исправлено для старого PowerShell)
                if ($cleanLink.StartsWith("http")) {
                    $fullUrl = $cleanLink
                } else {
                    $prefix = "https://main.atb.su"
                    if ($cleanLink.StartsWith("/")) {
                        $fullUrl = $prefix + $cleanLink
                    } else {
                        $fullUrl = $prefix + "/" + $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 "`n--- Готово! Проверь папку $dir ---" -ForegroundColor White
} catch {
    Write-Host "`n[!] Ошибка: $($_.Exception.Message)" -ForegroundColor Red
}

Read-Host "Нажми Enter, чтобы закрыть"