Skip to content

Instantly share code, notes, and snippets.

@jonnii
Created December 12, 2012 20:45
Show Gist options
  • Save jonnii/4271442 to your computer and use it in GitHub Desktop.
Save jonnii/4271442 to your computer and use it in GitHub Desktop.
$workingDirectory = $args[0]
$machine = $args[1]
$environment = $args[2]
$configuration = $args[3]
Set-Location $workingDirectory
# read the version file, which indicates which version we're going to deploy
$versionFile = resolve-path ../Version
$version = Get-Content $versionFile
Write-Host "Deploying $version"
# copy the files up into the share
$applicationName = $configuration['applicationName']
$copyLocation = "\\$machine\$applicationName\$environment\ComponentName\$version"
robocopy .\..\targets\upload-location $copyLocation /MIR /Z /MT:88
Write-Host "Copying $applicationName/$environment/ComponentName/$version to $machine"
$remoteExecutableLocation = "c:\$applicationName\$environment\ComponentName\$version"
$serviceInstallerName = $configuration['serviceInstallerExecutable']
function InstallService ($p,$s,$e,$v) {
$stop = "$p\$s stop --environment=$e"
$uninstall = "$p\$s uninstall --environment=$e"
$install = "$p\$s install --environment=$e"
$start = "$p\$s start --environment=$e"
$serviceName = "YourApp.Service.Name$" + $e
$service = Get-WmiObject -Class Win32_Service -Filter "Name='$serviceName'"
if ($service) {
stop-service $serviceName
$servicePath = "HKLM:\System\CurrentControlSet\services\YourApp.Service.Name`$$e\"
$description = "YourApp.Service.Name $e / $v"
$path = "`"$p\$s`" -instance:$e -displayname `"YourApp.Service.Name (Instance: $e)`" -servicename:YourApp.Service.Name"
Set-ItemProperty -path $servicePath -name Description -value "$description"
Set-ItemProperty -path $servicePath -name ImagePath -value "$path"
start-service $serviceName
}
else {
invoke-expression $install
invoke-expression $start
}
}
if($machine -eq 'localhost') {
InstallService $remoteExecutableLocation $serviceInstallerName $environment $version
} else {
invoke-command `
-ScriptBlock ${function:InstallService} `
-computername $machine `
-ArgumentList $remoteExecutableLocation, $serviceInstallerName, $environment, $version
}
#clean up any old deployed versions
$numVersions = (ls \\$machine\$applicationName\$environment\ComponentName).length
$maxVersions = $configuration['numPreviousCopiesToKeep']
if($numVersions -gt $maxVersions){
$numVersionsToRemove = $numVersions - $maxVersions
Write-Host "Cleaning up deployment directory, there are $numVersions versions available. Removing $numVersionsToRemove versions."
gci \\$machine\$applicationName\$environment\ComponentName | where {$_.Name -ne $version } | sort { $_.Name -as [Version] } | select -first $numVersionsToRemove | ri -r
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment