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


$ErrorActionPreference = "SilentlyContinue"

Write-Host "`n=== ПОИСК REPORT.TMP ===" -ForegroundColor Cyan

$paths = @(
    "$env:LOCALAPPDATA\Microsoft\Windows\INetCache",
    "$env:LOCALAPPDATA\Temp",
    "$env:TEMP"
) | Select-Object -Unique

$files = foreach ($p in $paths) {
    if (Test-Path $p) {
        Get-ChildItem $p -Recurse -Force -File |
            Where-Object {
                $_.Name -ieq "report.tmp" -or
                ($_.Name -match "report" -and $_.Extension -eq ".tmp")
            }
    }
}

$f = $files |
    Sort-Object LastWriteTime -Descending |
    Select-Object -First 1

if (-not $f) {
    Write-Host "report.tmp НЕ НАЙДЕН." -ForegroundColor Red
    Write-Host "Не закрывай окно ошибки банка и повтори запуск скрипта." -ForegroundColor Yellow
    return
}

Write-Host "`n=== ФАЙЛ НАЙДЕН ===" -ForegroundColor Green
$f | Format-List FullName,Name,Length,CreationTime,LastWriteTime

Write-Host "=== SHA256 ===" -ForegroundColor Cyan
Get-FileHash $f.FullName -Algorithm SHA256 | Format-List

$b = [System.IO.File]::ReadAllBytes($f.FullName)

if ($b.Length -eq 0) {
    Write-Host "Файл ПУСТОЙ (0 байт)." -ForegroundColor Red
    return
}

$count = [Math]::Min(128, $b.Length)
$head  = $b[0..($count-1)]

Write-Host "=== ПЕРВЫЕ БАЙТЫ HEX ===" -ForegroundColor Cyan

for ($i = 0; $i -lt $head.Length; $i += 16) {
    $end = [Math]::Min($i + 15, $head.Length - 1)
    $chunk = $head[$i..$end]

    $hex = ($chunk | ForEach-Object { $_.ToString("X2") }) -join " "
    $ascii = -join ($chunk | ForEach-Object {
        if ($_ -ge 32 -and $_ -le 126) { [char]$_ } else { "." }
    })

    "{0:X8}  {1,-47}  {2}" -f $i,$hex,$ascii
}

Write-Host "`n=== ТЕКСТОВОЕ НАЧАЛО ===" -ForegroundColor Cyan

$previewCount = [Math]::Min(500, $b.Length)
$preview = [System.Text.Encoding]::UTF8.GetString($b,0,$previewCount)
$preview = $preview -replace "`0","." -replace "[^\x09\x0A\x0D\x20-\x7EА-Яа-яЁё]","."

Write-Host $preview

Write-Host "`n=== ОПРЕДЕЛЕНИЕ ФОРМАТА ===" -ForegroundColor Cyan

$asciiHead = [System.Text.Encoding]::ASCII.GetString(
    $b,
    0,
    [Math]::Min(1024,$b.Length)
)

$type = "НЕИЗВЕСТНО"

if ($asciiHead.StartsWith("%PDF-")) {
    $type = "PDF"
}
elseif ($asciiHead -match '^\{\\rtf') {
    $type = "RTF"
}
elseif ($asciiHead -match '(?i)<html|<!doctype|<head|<body') {
    $type = "HTML"
}
elseif ($b.Length -ge 4 -and
        $b[0] -eq 0x50 -and
        $b[1] -eq 0x4B -and
        $b[2] -eq 0x03 -and
        $b[3] -eq 0x04) {
    $type = "ZIP / DOCX / XLSX / другой Office Open XML"
}
elseif ($b.Length -ge 2 -and
        $b[0] -eq 0x4D -and
        $b[1] -eq 0x5A) {
    $type = "Windows EXE/DLL (PE)"
}
elseif ($b.Length -ge 4 -and
        $b[0] -eq 0x89 -and
        $b[1] -eq 0x50 -and
        $b[2] -eq 0x4E -and
        $b[3] -eq 0x47) {
    $type = "PNG"
}
elseif ($b.Length -ge 3 -and
        $b[0] -eq 0xFF -and
        $b[1] -eq 0xD8 -and
        $b[2] -eq 0xFF) {
    $type = "JPEG"
}
elseif ($b.Length -ge 4 -and (
        ($b[0] -eq 0x49 -and $b[1] -eq 0x49 -and $b[2] -eq 0x2A -and $b[3] -eq 0x00) -or
        ($b[0] -eq 0x4D -and $b[1] -eq 0x4D -and $b[2] -eq 0x00 -and $b[3] -eq 0x2A)
    )) {
    $type = "TIFF"
}

Write-Host "ОПРЕДЕЛЁННЫЙ ТИП: $type" -ForegroundColor Yellow

Write-Host "`n=== АССОЦИАЦИЯ .TMP В WINDOWS ===" -ForegroundColor Cyan
cmd /c "assoc .tmp" 2>&1

Write-Host "`n=== ИТОГ ===" -ForegroundColor Cyan

switch -Wildcard ($type) {
    "PDF" {
        Write-Host "Сервер сформировал нормальный PDF. Проблема почти наверняка на стороне клиента/IE Mode/SFT-компонента." -ForegroundColor Green
    }
    "RTF" {
        Write-Host "Сервер сформировал RTF. Проблема почти наверняка в клиентском механизме открытия отчёта." -ForegroundColor Green
    }
    "HTML" {
        Write-Host "Вместо документа получен HTML. Нужно смотреть содержимое: возможно, report.asp возвращает страницу ошибки." -ForegroundColor Yellow
    }
    "ZIP*" {
        Write-Host "Получен настоящий контейнер Office/ZIP. Проблема, вероятно, в клиентском механизме обработки временного файла." -ForegroundColor Green
    }
    default {
        Write-Host "Формат автоматически не определён. Нужны HEX и текстовое начало выше." -ForegroundColor Yellow
    }
}

Write-Host "`n=== ГОТОВО ===" -ForegroundColor Cyan
Write-Host "Сфотографируй/скопируй мне вывод начиная с 'ФАЙЛ НАЙДЕН' и до 'ИТОГ'." -ForegroundColor White