# Define variables
$websiteName = "abc.example.info"
$rootFolder = "D:\root-folder-of-website"
$backupFolderBase = "D:\Backups\abc.example.com"
$date = Get-Date -Format "dd-MM-yyyy"
$retryCount = 1
# Prompt for the build zip file
$zipFilePath = Read-Host "Enter the path to the build zip file (e.g., C:\path\to\
build.zip)"
# Check if the zip file exists
if (-not (Test-Path -Path $zipFilePath)) {
Write-Host "The specified zip file does not exist. Please check the path and
try again."
exit
}
# Create a unique backup folder with date and retry count
do {
$backupFolder = "$backupFolderBase\bkp-$date-$retryCount"
$retryCount++
} while (Test-Path -Path $backupFolder)
New-Item -ItemType Directory -Path $backupFolder -Force | Out-Null
# Stop the IIS website and app pool
Import-Module WebAdministration
Stop-WebAppPool -Name $websiteName
Stop-Website -Name $websiteName
Write-Host "Stopped IIS website and application pool: $websiteName"
# Backup the website root folder
Copy-Item -Path "$rootFolder\*" -Destination $backupFolder -Recurse -Force
Write-Host "Backup completed: $backupFolder"
# Extract the zip file to a temporary folder
$tempExtractFolder = "$env:TEMP\build"
Expand-Archive -Path $zipFilePath -DestinationPath $tempExtractFolder -Force
Write-Host "Extracted $zipFilePath to $tempExtractFolder"
# Replace the files in the root folder with the unzipped contents
Remove-Item -Path "$rootFolder\*" -Recurse -Force
Copy-Item -Path "$tempExtractFolder\build\*" -Destination $rootFolder -Recurse -
Force
Write-Host "Replaced files in $rootFolder with contents of the build"
# Clean up temporary folder
Remove-Item -Path $tempExtractFolder -Recurse -Force
# Start the IIS website and app pool
Start-WebAppPool -Name $websiteName
Start-Website -Name $websiteName
Write-Host "Started IIS website and application pool: $websiteName"
Write-Host "Deployment completed successfully."