Skip to content

Instantly share code, notes, and snippets.

@crmckenzie
Created February 10, 2016 22:37
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save crmckenzie/cee7310973ee64bb414b to your computer and use it in GitHub Desktop.
Save crmckenzie/cee7310973ee64bb414b to your computer and use it in GitHub Desktop.
# https://tomcat.apache.org/tomcat-7.0-doc/windows-auth-howto.html
# https://shieldmaster.wordpress.com/2013/06/01/installing-apache-tomcat-v7-x-silently-with-command-line-options/
$defaultTomcatVersion = "8.0.30"
function Get-TomcatFilename(
[string] $version = $defaultTomcatVersion
)
{
return "apache-tomcat-$version.exe"
}
function Get-TomcatDownloadUrl(
[string] $version = $defaultTomcatVersion
)
{
$versionInfo = new-object System.Version $version
$major = $versionInfo.Major;
$filename = Get-TomcatFilename $version
$url = "http://mirrors.advancedhosters.com/apache/tomcat/tomcat-$major/v$version/bin/$filename"
return $url
}
function Get-TomcatDownloadedFileName(
[string] $version = $defaultTomcatVersion
)
{
$filename = Get-TomcatFileName $version
return Join-Path $env:TEMP $filename
}
function Get-TomcatInstallationDirectory(
[string] $version = $defaultTomcatVersion
)
{
$versionInfo = new-object System.Version $version
$major = $versionInfo.Major;
$minor = $versionInfo.Minor;
$versionStr = "$major.$minor";
$dir = Join-Path $env:ProgramFiles "Apache Software Foundation"
$dir = Join-Path $dir "Tomcat $versionStr"
return $dir
}
function Get-TomcatServiceName(
[string] $version = $defaultTomcatVersion
)
{
$versionInfo = new-object System.Version $version
$major = $versionInfo.Major;
$minor = $versionInfo.Minor;
$versionStr = "$major.$minor";
return "Apache Tomcat $versionStr Tomcat$major";
}
function Install-Tomcat(
[string] $version = $defaultTomcatVersion
)
{
$installationDir = Get-TomcatInstallationDirectory $version
$installed = Test-Path $installationDir
if ($installed -eq $true){
return;
}
$url = Get-TomcatDownloadUrl $version
$filename = Get-TomcatFilename $version
$downloadedFilename = Join-Path $env:TEMP $filename
if ((Test-Path -Path $downloadedFilename) -eq $false)
{
Invoke-WebRequest -Uri $url -OutFile $downloadedFilename
}
$process = Start-Process -FilePath $downloadedFilename `
-ArgumentList /S `
-NoNewWindow -Wait
}
function Invoke-MoveIISDefaultWebSitePort()
{
$binding = Get-WebBinding -Name "Default Web Site" -Port 80
if ($binding -ne $null)
{
Set-WebBinding -Name "Default Web Site" -BindingInformation "*:80:" -PropertyName Port -Value 1080
}
}
function Set-CatalinaHome(
[string] $version = $defaultTomcatVersion
)
{
$installationDirectory = Get-TomcatInstallationDirectory
[Environment]::SetEnvironmentVariable('CATALINA_HOME', $catalinaHome, 'Machine')
}
function Start-Tomcat(
[string] $version = $defaultTomcatVersion
)
{
$serviceName = Get-TomcatServiceName $version
$service = Get-service $serviceName
if ($service -eq $null)
{
throw "Could not find an installed service named $serviceName"
}
$status = $service.Status;
if ($status -ne "Stopped")
{
write-host "Service $serviceName was not started because its current status is $Status" -ForegroundColor Yellow
}
Start-Service $serviceName
}
try
{
write-host "Verifying that IIS does not own port 80"
Invoke-MoveIISDefaultWebSitePort
write-host "Installing Tomcat"
Install-Tomcat
write-host "Setting Catalinia Home"
Set-CatalinaHome
write-host "Starting Tomcat"
Start-Tomcat
}
catch
{
write-error $_.Exception.Message
exit -1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment