[CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [string]$CsvPath, [switch]$Recurse ) $ErrorActionPreference = 'Stop' function Get-XmlText { param( [xml]$Document, [string]$Tag ) $nodes = $Document.GetElementsByTagName($Tag) if ($nodes.Count -gt 0) { return [string]$nodes[0].InnerText } return '' } $resolved = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path $extensions = @('.docx', '.xlsx', '.pptx', '.docm', '.xlsm', '.pptm', '.odt', '.ods', '.odp') if (Test-Path -LiteralPath $resolved -PathType Leaf) { $files = @(Get-Item -LiteralPath $resolved) } else { $files = @(Get-ChildItem -LiteralPath $resolved -File -Recurse:$Recurse | Where-Object { $extensions -contains $_.Extension.ToLowerInvariant() }) } if ($files.Count -eq 0) { Write-Host 'Подходящие документы не найдены.' return } Add-Type -AssemblyName System.IO.Compression.FileSystem $results = foreach ($file in $files) { $record = [ordered]@{ File = $file.FullName Type = $file.Extension.ToLowerInvariant() SizeKB = [math]::Round($file.Length / 1KB, 1) Modified = $file.LastWriteTime.ToString('yyyy-MM-dd HH:mm') Title = '' Author = '' Subject = '' Keywords = '' Description = '' Created = '' LastModifiedBy = '' } $archive = $null try { $archive = [System.IO.Compression.ZipFile]::OpenRead($file.FullName) $entryName = if ($file.Extension -match '^\.(docx|xlsx|pptx|docm|xlsm|pptm)$') { 'docProps/core.xml' } else { 'meta.xml' } $entry = $archive.GetEntry($entryName) if ($entry) { $stream = $entry.Open() try { $reader = New-Object System.IO.StreamReader($stream) try { $content = $reader.ReadToEnd() } finally { $reader.Dispose() } } finally { $stream.Dispose() } [xml]$xml = $content $record['Title'] = Get-XmlText $xml 'title' $record['Author'] = Get-XmlText $xml 'creator' if (-not $record['Author']) { $record['Author'] = Get-XmlText $xml 'initial-creator' } $record['Subject'] = Get-XmlText $xml 'subject' $record['Keywords'] = Get-XmlText $xml 'keywords' $record['Description'] = Get-XmlText $xml 'description' $record['Created'] = Get-XmlText $xml 'created' if (-not $record['Created']) { $record['Created'] = Get-XmlText $xml 'creation-date' } $record['LastModifiedBy'] = Get-XmlText $xml 'lastModifiedBy' } } catch { $record['Title'] = 'ошибка чтения' } finally { if ($archive) { $archive.Dispose() } } [pscustomobject]$record } $results | Format-Table -AutoSize if ($CsvPath) { $results | Export-Csv -LiteralPath $CsvPath -NoTypeInformation -Encoding UTF8 Write-Host "Отчёт сохранён: $CsvPath" }