cchitsiang
11/25/2013 - 4:52 PM

Blue Green Deployment from http://davidduffett.net/post/4833657659/blue-green-deployment-to-iis-with-powershell

param
(
    $packageFile,
    $server,
    $siteName,
    $blueIISPath,
    $greenIISPath,
    $blueNetworkPath,
    $greenNetworkPath,
    $virtualDirName = $null
)
 
# Default paths to the "Blue" slice
$newIISPath = $blueIISPath
$newNetworkPath = $blueNetworkPath
 
# Check package file exists
if (!(Test-Path $packageFile)) {
    Write-Error "Package file [$packageFile] could not be found or accessed."
    exit 1
}
 
# Access IIS on remote machine
$packetPrivacy = 6
$query = "ServerComment = '" + $siteName + "'"
$webServer = Get-WMIObject -Class IIsWebServerSetting -Namespace "root\microsoftiisv2" -Filter $query -ComputerName $server -Authentication $packetPrivacy
if ($webServer -eq $null) {
    Write-Error "Could not find or access IIS on $server"
    exit 1
}
 
# Query current home directory path in IIS
$webDir = $null
$nameQuery = "Name = '" + $webServer.Name + "/root'"
 
# Modify query for virtual directories under site
if ($virtualDirName -ne $null -and $virtualDirName -ne "") {
    $nameQuery = "Name = '" + $webServer.Name + "/root/" + $virtualDirName + "'"
}
 
$webDir = Get-WMIObject -Class IIsWebVirtualDirSetting -Namespace "root\microsoftiisv2" -Filter $nameQuery -ComputerName $server -Authentication $packetPrivacy
if ($webDir -eq $null) {
    Write-Error "Could not find or access home directory"
    exit 1
}
Write-Host "Current site path is" $webDir.Path
 
# Are we on the blue slice? If so, switch to green
if ($webDir.Path -eq $blueIISPath) {
    $newIISPath = $greenIISPath
    $newNetworkPath = $greenNetworkPath
}
 
try {
    $error.Clear()
 
    # Clean folder
    Write-Host "Cleaning $newNetworkPath"
    Remove-Item "$newNetworkPath\*" -recurse -force
 
    # Unzip build file
    Write-Host "Updating $newNetworkPath to new version"
    $shell = New-Object -com shell.application 
    $zipFile = $shell.namespace($packageFile) 
    $destination = $shell.namespace($newNetworkPath) 
    $destination.Copyhere($zipFile.items(), 0x4)
 
    # Switch IIS path
    Write-Host "Switching path to $newIISPath"
    $webDir.Path = $newIISPath
    Set-WmiInstance -InputObject $webDir
 
    Write-Host "Successfully deployed."
} catch {
    Write-Error $error[0]
    exit 1
}
powershell -ExecutionPolicy unrestricted -command blue_green_deploy.ps1 
    -buildZipFile package.zip -server WEBSERVER -siteName WebSite 
    -blueIISPath D:\Web_Blue\ -blueNetworkPath \\WEBSERVER\Web_Blue\ 
    -greenIISPath D:\Web_Green\ -greenNetworkPath \\WEBSERVER\Web_Green\;