Skip to content

Instantly share code, notes, and snippets.

@MartinMiles
Created May 10, 2024 22:28
Show Gist options
  • Save MartinMiles/252dc2998a6a8538a9c26016862045e4 to your computer and use it in GitHub Desktop.
Save MartinMiles/252dc2998a6a8538a9c26016862045e4 to your computer and use it in GitHub Desktop.
Downloads Sitecore web folder (or subfolder) in a zip archive and then cleans up after itself, leaving no traces of download.
# Define the path to the Sitecore bin directory
$folderName = "App_Config";
$websiteName = "you_site_name"
$binPath = "C:\inetpub\wwwroot\$websiteName\$folderName"
# Define the destination for the temporary zip file
$zipFilePath = "C:\inetpub\wwwroot\$websiteName\temp\$folderName.zip"
# Define exclusions
$excludedDirectories = @('C:\inetpub\wwwroot\$websiteName\App_Data', 'C:\inetpub\wwwroot\$websiteName\temp') # Example directory to exclude
$excludedExtensions = @('.pdb', '.txt') # Example extensions to exclude
# Ensure the output directory exists
$zipFileDir = Split-Path -Path $zipFilePath -Parent
if (-Not (Test-Path $zipFileDir)) {
New-Item -Path $zipFileDir -ItemType Directory -Force | Out-Null
}
# Check if zip file already exists and delete if it does
if (Test-Path $zipFilePath) {
Remove-Item $zipFilePath -Force | Out-Null
}
# Create an empty temporary directory for storing files to be zipped
$tempDir = Join-Path $zipFileDir "tempForZip"
if (Test-Path $tempDir) {
Remove-Item $tempDir -Recurse -Force | Out-Null
}
New-Item -Path $tempDir -ItemType Directory -Force | Out-Null
# Copy files to the temporary directory, excluding specified paths and extensions
Get-ChildItem -Path $binPath -Recurse | Where-Object {
$include = $true
# Check for directory exclusions
foreach ($dir in $excludedDirectories) {
if ($_.FullName.StartsWith($dir)) {
$include = $false
break
}
}
# Check for file extensions exclusions
if ($include -and $_.PSIsContainer -eq $false) {
foreach ($ext in $excludedExtensions) {
if ($_.Extension -eq $ext) {
$include = $false
break
}
}
}
$include
} | ForEach-Object {
$destination = $_.FullName.Replace($binPath, $tempDir)
if ($_.PSIsContainer) {
New-Item -Path $destination -ItemType Directory -Force | Out-Null
} else {
Copy-Item -Path $_.FullName -Destination $destination | Out-Null
}
}
# Create a zip archive of the temporary directory
Add-Type -AssemblyName System.IO.Compression.FileSystem
[System.IO.Compression.ZipFile]::CreateFromDirectory($tempDir, $zipFilePath)
# Trigger the download of the zip file
Get-Item $zipFilePath | Out-Download | Out-Null
# Cleanup the temporary folder after use
Remove-Item $tempDir -Recurse -Force | Out-Null
Start-Sleep -Milliseconds 600
# Clean downloaded zip after use
if (Test-Path $zipFilePath) {
Remove-Item $zipFilePath -Force | Out-Null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment