[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [switch]$Recurse, [ValidateRange(1, 5000)] [int]$MaxFiles = 500, [string]$Csv, [switch]$FailOnInvalid ) $ErrorActionPreference = 'Stop' try { $resolved = Resolve-Path -LiteralPath $Path -ErrorAction Stop } catch { Write-Error "Путь не найден: $Path" exit 2 } if (Test-Path -LiteralPath $resolved -PathType Leaf) { $files = @(Get-Item -LiteralPath $resolved) } else { $extensions = @('.exe', '.dll', '.sys', '.ps1', '.msi') $files = @(Get-ChildItem -LiteralPath $resolved -File -Recurse:$Recurse -ErrorAction SilentlyContinue | Where-Object { $extensions -contains $_.Extension.ToLower() } | Select-Object -First $MaxFiles) } if ($files.Count -eq 0) { Write-Host 'Файлы для проверки не найдены.' exit 0 } $results = @() foreach ($file in $files) { try { $signature = Get-AuthenticodeSignature -LiteralPath $file.FullName -ErrorAction Stop $signer = '' if ($signature.SignerCertificate) { $signer = $signature.SignerCertificate.Subject } $results += [pscustomobject]@{ File = $file.FullName Status = [string]$signature.Status Signer = $signer IsOSBinary = $signature.IsOSBinary } } catch { $results += [pscustomobject]@{ File = $file.FullName Status = 'Error' Signer = '' IsOSBinary = $false } } } $results | Format-Table File, Status, Signer -AutoSize $counts = $results | Group-Object Status | Select-Object Name, Count $counts | Format-Table -AutoSize $bad = @($results | Where-Object { $_.Status -ne 'Valid' }) Write-Host ("Проверено: {0}, требуют внимания: {1}" -f $results.Count, $bad.Count) if ($Csv) { $results | Export-Csv -LiteralPath $Csv -NoTypeInformation -Encoding UTF8 Write-Host "CSV сохранён: $Csv" } if ($FailOnInvalid -and $bad.Count -gt 0) { exit 1 } exit 0