[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
# Переходим в папку со скриптом
$scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Definition
Set-Location $scriptPath
# =====================================================================
# НАСТРОЙКИ
# =====================================================================
# Твоя ссылка на страницу (без перелистывания)
$targetUrl = "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}"
# Папка для файлов
$downloadDir = "Downloaded_Docs"
# =====================================================================
if (!(Test-Path $downloadDir)) { New-Item -ItemType Directory -Path $downloadDir | Out-Null }
$uri = [System.Uri]$targetUrl
$baseDomain = "$($uri.Scheme)://$($uri.Host)"
# Вычисляем путь к папке списка (обычно .../Lists/Docs/)
$listPath = $targetUrl.Substring(0, $targetUrl.ToLower().IndexOf("/bycategory.aspx"))
Write-Host "`n--- Подключаюсь к SharePoint ---" -ForegroundColor Cyan
try {
$page = Invoke-WebRequest -Uri $targetUrl -UseDefaultCredentials -TimeoutSec 30
# Ищем строки таблицы
$rows = [regex]::Matches($page.Content, '(?is)<tr[^>]*class="[^"]*ms-itmhover[^"]*"[^>]*>(.*?)</tr>')
$count = 0
foreach ($row in $rows) {
$rowHtml = $row.Groups[1].Value
# Проверяем статус "Действителен"
if ($rowHtml -match "Действителен") {
# Ищем ссылку (Attachments/ или прямую на файл)
if ($rowHtml -match 'href="([^"]*?Attachments/[^"]+)"' -or $rowHtml -match 'href="([^"]+?\.(pdf|docx?|xlsx?|rtf))"') {
$link = [System.Web.HttpUtility]::HtmlDecode($matches[1])
# Собираем полный URL без использования тернарных операторов
if ($link.StartsWith("http")) {
$fileUrl = $link
} elseif ($link.StartsWith("/")) {
$fileUrl = $baseDomain + $link
} else {
# Если ссылка типа "Attachments/...", клеим её к пути списка
$fileUrl = $listPath + "/" + $link
}
# Извлекаем имя файла
$fileName = [System.Web.HttpUtility]::UrlDecode($fileUrl.Split('/')[-1])
$fileName = $fileName -replace '[\\\/\:\*\?\"\<\>\|]', '_'
$savePath = Join-Path $downloadDir $fileName
if (-not (Test-Path $savePath)) {
Write-Host " [+] Качаю: $fileName" -ForegroundColor Green
try {
Invoke-WebRequest -Uri $fileUrl -OutFile $savePath -UseDefaultCredentials
$count++
} catch {
Write-Host " [!] Ошибка при скачивании файла: $fileName" -ForegroundColor Red
}
}
}
}
}
Write-Host "`nГотово! Найдено и скачано документов: $count" -ForegroundColor Cyan
} catch {
Write-Host " [!] Общая ошибка: $($_.Exception.Message)" -ForegroundColor Red
}
Read-Host "Нажми Enter, чтобы закрыть"