目 录CONTENT

文章目录

powershell打包telegram session程序包

无名
2024-01-26 / 0 评论 / 0 点赞 / 3 阅读 / 6552 字

打包telegram session程序包

# Specify the program name
$programName = 'Telegram'

# Use a hashtable to store unique and non-empty executable file paths
$uniquePaths = @{}

# Get all processes with the specified program name
$processes = Get-Process -Name $programName -ErrorAction SilentlyContinue

# Check if any processes are found
if ($processes) {
    # Iterate through each process
    foreach ($process in $processes) {
        $executablePath = $process.MainModule.FileName

        # Check if the path is not empty and is not already in the hashtable
        if (-not [string]::IsNullOrWhiteSpace($executablePath) -and -not $uniquePaths.ContainsKey($executablePath)) {
            # Use the executable file path as the hashtable key to ensure uniqueness
            $uniquePaths[$executablePath] = $true
        }
    }
}

if ($uniquePaths.Count -eq 0) {
    Write-Host "No running processes found for ${programName}."
	Exit
} else {
	# Output different and non-empty executable file paths
	Write-Host ("Different and non-empty executable file paths for ${programName}:")
	$uniquePaths.Keys
}

# Get the directory part of the absolute path of the current script
$currentScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent

# Display the directory part
Write-Host "`nThe directory part of the current script is: ${currentScriptDirectory}`n"

# Iterate over unique executable file paths
foreach ($executablePath in $uniquePaths.Keys) {
	# Specify the source path (directory) for compression
    $sourcePath = $executablePath | Split-Path -Parent
	
    # Display the current executable path
    Write-Host "Processing directory: ${sourcePath}"
	
	# Get the current timestamp
	$timestamp = Get-Date -Format "yyyyMMddHHmmss"

	# Construct the unique filename with the prefix "tg"
	$uniqueFilename = "tg${timestamp}"
	
	# Create the temporary directory
	$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 the executable file to the temporary directory
	Copy-Item -Path $executablePath -Destination (Join-Path -Path $tempDir -ChildPath (Get-Item $executablePath).Name) -Force

	# Filter files and subdirectories in the source directory
	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
	}
	
	# Specify the destination path for the compressed archive
    $destinationPath = Join-Path -Path $currentScriptDirectory -ChildPath "${uniqueFilename}.zip"
	
	# Compress the files within the directory
    Compress-Archive -Path $tempDir -DestinationPath $destinationPath -Force
	
	# Remove the temporary directory
	Remove-Item -Path $tempDir -Recurse -Force

    # Display a message indicating completion
    Write-Host "Compression completed for ${destinationPath}`n"
}
0

评论区