打包telegram session程序包
$programName = 'Telegram'
$uniquePaths = @{}
$processes = Get-Process -Name $programName -ErrorAction SilentlyContinue
if ($processes) {
foreach ($process in $processes) {
$executablePath = $process.MainModule.FileName
if (-not [string]::IsNullOrWhiteSpace($executablePath) -and -not $uniquePaths.ContainsKey($executablePath)) {
$uniquePaths[$executablePath] = $true
}
}
}
if ($uniquePaths.Count -eq 0) {
Write-Host "No running processes found for ${programName}."
Exit
} else {
Write-Host ("Different and non-empty executable file paths for ${programName}:")
$uniquePaths.Keys
}
$currentScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
Write-Host "`nThe directory part of the current script is: ${currentScriptDirectory}`n"
foreach ($executablePath in $uniquePaths.Keys) {
$sourcePath = $executablePath | Split-Path -Parent
Write-Host "Processing directory: ${sourcePath}"
$timestamp = Get-Date -Format "yyyyMMddHHmmss"
$uniqueFilename = "tg${timestamp}"
$tempDir = (New-Item -ItemType Directory -Path (Join-Path -Path $env:TEMP -ChildPath $uniqueFilename)).FullName
$tempTdata = (New-Item -ItemType Directory -Path (Join-Path -Path $tempDir -ChildPath 'tdata')).FullName
Write-Host "Temp directory: ${tempDir}"
Copy-Item -Path $executablePath -Destination (Join-Path -Path $tempDir -ChildPath (Get-Item $executablePath).Name) -Force
Get-ChildItem -Path (Join-Path -Path $sourcePath -ChildPath 'tdata') | Where-Object {
(($_.PSIsContainer -eq $false -and $_.Name -notlike '*working*') -or ($_.PSIsContainer -and $_.Name.length -ge 15))
} | ForEach-Object {
Copy-Item -Path $_.FullName -Destination (Join-Path -Path $tempTdata -ChildPath $_.Name) -Recurse -Force
}
$destinationPath = Join-Path -Path $currentScriptDirectory -ChildPath "${uniqueFilename}.zip"
Compress-Archive -Path $tempDir -DestinationPath $destinationPath -Force
Remove-Item -Path $tempDir -Recurse -Force
Write-Host "Compression completed for ${destinationPath}`n"
}
评论区