The following script does several jobs :
- Backup IIS Website Files To be upgraded
- Stop IIS Website Application Pool
- Copy Newest Website Files
- Start IIS Website Application Pool
| Also can download this script from Microsoft Technet Gallery. |
<#
.SYNOPSIS
Update IIS Website content files.
.DESCRIPTION
This is a usefull script which allow to do several thinks like :
1. Backup IIS Website Files To Be Upgrated
2. Stop IIS Application Pool
3. Copy Newest Files
4. Start IIS Application Pool
.PARAMETER service
Application Pool Service Name
.PARAMETER srcfolder
Folder with latest website content files
.PARAMETER tgtfolder
Folder with running Website files
.PARAMETER BackupFolder
Folder were modified files are backed up
.NOTES
Author: Giorgos Grammatikos
Version: 1.0.0
DateCreated: 01/12/2005
.LINK
http://grammatikosgx.wordpress.com
#>
# Clean Up Folder Backup
Remove-Item C:\New_Versions\Backup\* -Recurse
### Declarations Folders For Content Update ###
# IIS Application Pool
$service = "mywebsite"
$timeout = New-TimeSpan -Seconds 120
$sw = [diagnostics.stopwatch]::StartNew()
# Update, Target, Backup Folders
$srcFolder = "C:\New_Versions\New\"
$tgtFolder = "C:\inetpub\wwwroot\Website"
$BackupFolder = "C:\New_Versions\Backup\"
$updatePackage = get-ChildItem -File -Recurse -Path $srcFolder
$target = get-ChildItem -File -Recurse -Path $tgtFolder
[string]$srcPattern = $srcFolder.Replace("\","\\")
$srcList = Get-ChildItem -Path $srcFolder -Recurse -File
$srcList | Add-Member -NotePropertyName Status -NotePropertyValue Unknown
# Backup Files To Be Upgrated
# Initialize counters
$count = $newCount = $modCount = 0
try{
# Copy
foreach ($file in $srcList)
{
$count++
$tgtFileName = Join-Path -Path $BackupFolder ($file.FullName -Replace $srcPattern, "")
$difFileName = Join-Path -Path $BackupFolder ($file.FullName -Replace $srcPattern, "")
$exists = Test-Path -Path $tgtFileName
if ($exists)
{
$tgtFile = Get-ChildItem -Path $tgtFileName
if ( $file.Length -ne $tgtFile.Length -or
$file.LastWriteTimeUtc -gt $tgtFile.LastWriteTimeUtc )
{
$modCount++
$file.Status = "modified"
}
else
{
$file.Status = "same"
}
}
else
{
$newCount++
$file.Status = "new"
}
Write-Verbose "$tgtFileName is $($file.Status)"
# Copy files as needed to target and difference directories
# New-Item (touch) is required to build out dir structure where missing
if ($file.Status -eq "new")
{
# Copy with file structure
New-Item -ItemType File -Path $tgtFileName -Force | Out-Null
Copy-Item -Path $file.FullName -Destination $tgtFileName
}
elseif ($file.Status -eq "modified")
{
# Copy with file structure
New-Item -ItemType File -Path $difFileName -Force | Out-Null
Copy-Item -Path $file.FullName -Destination $difFileName
}
}
Write-Host "Processed $count files, copied $newCount new files, and $modCount changed files"
}
catch
{
Write-Host "ERROR: $($Error[0].Exception)"
}
try
{
# Wait until Application Pool Stop
While ((Get-WebAppPoolState -name $service).value -eq "Started") {
Stop-WebAppPool $service
Write-Progress "Waiting For Application Pool To Stop"
if ($sw.Elapsed -gt $timeout)
{
Throw "Time Out"
}
}
write-Host "Application Pool: '$service' Stopped !!! "
}
catch
{
Write-Host "ERROR: $($Error[0].Exception)"
}
try
{
# Initialize counters
$count = $newCount = $modCount = 0
# Copy
foreach ($file in $srcList)
{
$count++
$tgtFileName = Join-Path -Path $tgtFolder ($file.FullName -Replace $srcPattern, "")
$difFileName = Join-Path -Path $tgtFolder ($file.FullName -Replace $srcPattern, "")
$exists = Test-Path -Path $tgtFileName
if ($exists)
{
$tgtFile = Get-ChildItem -Path $tgtFileName
if ( $file.Length -ne $tgtFile.Length -or
$file.LastWriteTimeUtc -gt $tgtFile.LastWriteTimeUtc )
{
$modCount++
$file.Status = "modified"
}
else
{
$file.Status = "same"
}
}
else
{
$newCount++
$file.Status = "new"
}
Write-Verbose "$tgtFileName is $($file.Status)"
# Copy files as needed to target and difference directories
# New-Item (touch) is required to build out dir structure where missing
if ($file.Status -eq "new")
{
# Copy with file structure
New-Item -ItemType File -Path $tgtFileName -Force | Out-Null
Copy-Item -Path $file.FullName -Destination $tgtFileName
}
elseif ($file.Status -eq "modified")
{
# Copy with file structure
New-Item -ItemType File -Path $difFileName -Force | Out-Null
Copy-Item -Path $file.FullName -Destination $difFileName
}
}
Write-Host "Processed $count files, copied $newCount new files, and $modCount changed files"
}
catch
{
Write-Host "ERROR: $($Error[0].Exception)"
}
try
{
while ((Get-WebAppPoolState -name $service).value -eq "Stopped"){
Start-WebAppPool $service
Write-Host "Application Pool: '$service' Started !!! "
if ($sw.Elapsed -gt $timeout)
{
Throw "Time Out"
}
}
}
catch
{
Write-Host "ERROR: $($Error[0].Exception)"
}
