[CmdletBinding()] param( [ValidateSet('Save', 'Restore', 'List')] [string]$Action = 'List', [string]$File, [string]$Directory = (Join-Path $env:USERPROFILE 'Productivity\clipboard'), [switch]$Latest ) $ErrorActionPreference = 'Stop' if (-not (Test-Path -LiteralPath $Directory)) { New-Item -ItemType Directory -Path $Directory -Force | Out-Null } switch ($Action) { 'Save' { $text = Get-Clipboard -Raw if ([string]::IsNullOrEmpty($text)) { throw 'Буфер обмена пуст.' } $stamp = Get-Date -Format 'yyyy-MM-dd_HH-mm-ss' $target = Join-Path $Directory ("clipboard-$stamp.txt") $counter = 1 while (Test-Path -LiteralPath $target) { $target = Join-Path $Directory ("clipboard-$stamp-$counter.txt") $counter++ } Set-Content -LiteralPath $target -Value $text -Encoding UTF8 -NoNewline Write-Host "Сохранено: $target" } 'Restore' { if ($Latest -or [string]::IsNullOrWhiteSpace($File)) { $item = Get-ChildItem -LiteralPath $Directory -Filter 'clipboard-*.txt' -File -ErrorAction SilentlyContinue | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 if ($null -eq $item) { throw 'Нет сохранённых фрагментов.' } $File = $item.FullName } if (-not (Test-Path -LiteralPath $File -PathType Leaf)) { throw "Файл не найден: $File" } $text = Get-Content -LiteralPath $File -Raw -Encoding UTF8 Set-Clipboard -Value $text Write-Host "Восстановлено из: $File" } 'List' { $files = @(Get-ChildItem -LiteralPath $Directory -Filter 'clipboard-*.txt' -File -ErrorAction SilentlyContinue | Sort-Object -Property LastWriteTime -Descending) if ($files.Count -eq 0) { Write-Host 'Сохранений нет.' break } foreach ($item in $files) { Write-Host ("{0} {1,8:N0} Б {2}" -f $item.LastWriteTime.ToString('yyyy-MM-dd HH:mm'), $item.Length, $item.Name) } } }