Skip to content

Instantly share code, notes, and snippets.

@TomasBouda
Last active May 16, 2024 09:52
Show Gist options
  • Save TomasBouda/20512b6ca7a7c18b161967300bf631a7 to your computer and use it in GitHub Desktop.
Save TomasBouda/20512b6ca7a7c18b161967300bf631a7 to your computer and use it in GitHub Desktop.
param(
[Parameter(Mandatory = $true)]
[string]$Source,
[Parameter(Mandatory = $true)]
[string]$Destination
)
$global:appOfflineGistUrl = 'https://gist.githubusercontent.com/TomasBouda/176632ebcccaf684ea09231935f4d300/raw/ff6662b3131ed43fe206403b62ec2129fae81a83/App_offline.htm'
function Copy-WithProgress {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
$Source,
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
$Destination
)
begin {
$template = Invoke-RestMethod -Method GET -Uri $global:appOfflineGistUrl
# Create App_offline.htm to gracefully take the website offline
$template -replace '#progress#', 0 | Set-Content "$Destination\App_offline.htm"
}
process {
$filelist = Get-ChildItem $Source -Recurse
$total = $filelist.count
$position = 0
foreach ($file in $filelist) {
$filename = $file.Fullname.Replace($Source, '')
$destinationFile = $Destination + $filename
Copy-Item $file.FullName -Destination $destinationFile -Force
$position++
$progress = [Math]::Round((($position / $total) * 100), 0)
$template -replace '#progress#', $progress | Set-Content "$Destination\App_offline.htm"
Write-Progress -Activity "Copying data from $Source to $Destination" -Status "Copying File $filename" -PercentComplete $progress
}
}
end {
Write-Debug "Removing App_offline.htm"
# Remove App_offline.htm to finish the process and let users browse updated website
Remove-Item "$Destination\App_offline.htm" -Force -ErrorAction SilentlyContinue
Write-Debug "Copy-WithProgress"
}
}
Copy-WithProgress -Source $Source -Destination $Destination
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment