[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Set-Location $PSScriptRoot
$csvPath = "data.csv"
$downloadDir = "downloads"
$baseDomain = "https://main.atb.su"
# Взял этот путь из твоего скриншота!
$rootFolder = "/normdocs/Lists/Docs/pub"
if (!(Test-Path $downloadDir)) { New-Item -ItemType Directory -Path $downloadDir }
# Читаем CSV с учетом возможных разных разделителей
$data = Import-Csv -Path $csvPath -Delimiter ";" -Encoding Default
if ($data.Count -gt 0 -and -not $data[0]."Название") {
$data = Import-Csv -Path $csvPath -Delimiter "," -Encoding Default
}
# Маскируемся под твой Chrome (взято из твоих Headers на скриншоте)
$headers = @{
"User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36"
"Accept" = "*/*"
}
foreach ($row in $data) {
$title = $row."Название"
if (-not $title) { continue }
$title = $title.Trim()
Write-Host "`nЦель: $title" -ForegroundColor Cyan
$escTitle = [uri]::EscapeDataString($title)
$escFolder = [uri]::EscapeDataString($rootFolder)
# Собираем ссылку с учетом папки pub
$searchUrl = "$baseDomain/normdocs/Lists/Docs/ByCategory.aspx?RootFolder=$escFolder&FilterField1=Title&FilterValue1=$escTitle"
try {
# Запрашиваем страницу, прикидываясь браузером
$page = Invoke-WebRequest -Uri $searchUrl -UseDefaultCredentials -Headers $headers -TimeoutSec 20
# Улучшенный поиск ссылки: ищем и Attachments, и прямые ссылки на pdf/doc
if ($page.Content -match 'href="([^"]*?Attachments/[^"]+)"' -or $page.Content -match 'href="([^"]+?\.(pdf|docx?|xlsx?))"') {
$matchedUrl = $matches[1]
# Декодируем HTML-символы (типа &)
$matchedUrl = [System.Web.HttpUtility]::HtmlDecode($matchedUrl)
if ($matchedUrl -notmatch "^http") {
if (-not $matchedUrl.StartsWith("/")) { $matchedUrl = "/$matchedUrl" }
$fileUrl = "$baseDomain$matchedUrl"
} else {
$fileUrl = $matchedUrl
}
$rawFileName = $fileUrl.Split('/')[-1]
$fileName = [System.Web.HttpUtility]::UrlDecode($rawFileName)
$savePath = Join-Path $downloadDir $fileName
Write-Host " [+] ПОПАДАНИЕ! Скачиваю: $fileName" -ForegroundColor Green
Invoke-WebRequest -Uri $fileUrl -OutFile $savePath -UseDefaultCredentials -Headers $headers
} else {
Write-Host " [-] Мимо. SharePoint не отдал ссылку. Попробуй открыть в браузере: " -ForegroundColor Yellow
Write-Host " $searchUrl" -ForegroundColor Gray
}
} catch {
Write-Host " [!] Ошибка связи: $($_.Exception.Message)" -ForegroundColor Red
}
}
Write-Host "`n--- Операция завершена! ---" -ForegroundColor White
Read-Host "Нажмите Enter"