Last active
January 12, 2021 08:13
-
-
Save michaeljbailey/3acc3536e80f34b75526 to your computer and use it in GitHub Desktop.
Automation script for installing the Ubiquiti UniFi Controller as a Windows Service. This takes care of doing tedious things like setting JAVA_HOME or including it within your PATH variable. Use at your own risk!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Written by Mike Bantegui (Last Updated 2015-03-05) | |
# Use at your own caution! I take no responsibility for any damage you may cause on your system. | |
# Known to support: Windows Server 2012 R2 | |
function Request-ElevatedPrompt | |
{ | |
# Adapted from Benjamin Armstrong's "A self elevating PowerShell script" | |
# http://blogs.msdn.com/b/virtual_pc_guy/archive/2010/09/23/a-self-elevating-powershell-script.aspx | |
$identity = [System.Security.Principal.WindowsIdentity]::GetCurrent() | |
$principal = New-Object System.Security.Principal.WindowsPrincipal($identity) | |
$admin = [System.Security.Principal.WindowsBuiltInRole]::Administrator | |
if (!$principal.IsInRole($admin)) | |
{ | |
$process = New-Object System.Diagnostics.ProcessStartInfo "PowerShell.exe" | |
$process.Arguments = $MyInvocation.MyCommand.Definition | |
$process.Verb = "Runas" | |
[System.Diagnostics.Process]::Start($process) | |
Exit | |
} | |
Write-Host "Running as elevated command prompt" | |
} | |
function Get-JavaHome | |
{ | |
$architecture = (Get-WmiObject Win32_OperatingSystem).OSArchitecture | |
if ($achitecture -like "64-bit") | |
{ | |
$java32 = (Get-ChildItem -Path "C:\Program Files (x86)" -Recurse -Filter "java.exe" -ErrorAction SilentlyContinue | Select -First 1) | |
if ($java32 -eq $null) | |
{ | |
Write-Error "Must have the 32-bit JRE installed on x64 platforms. Please download and install the latest 32-bit Java from 'https://java.com/en/download/'" | |
Exit | |
} | |
else | |
{ | |
Write-Host "Found 32-bit Java ('$java32')." | |
} | |
} | |
$javaHome = (Get-ChildItem -Path "C:\" -Recurse -Filter "java.exe" -ErrorAction SilentlyContinue | Select -First 1).DirectoryName | |
if ($javaHome -eq $null) | |
{ | |
Write-Error "Could not find any Java installations. Please download and install the latest Java from 'https://java.com/en/download/'" | |
Exit | |
} | |
Write-Host "Found Java bin directory (using '$javaHome')." | |
return $javaHome | |
} | |
function Get-UnifiHome | |
{ | |
$unifiHome = (Get-ChildItem -Path "C:\Users" -Recurse -Filter "Ubiquiti UniFi" | Select -First 1).FullName | |
if ($unifiHome -eq $null) | |
{ | |
Write-Error "Could not find any UniFi controller installations. Please download and install the latest UniFi controller from 'https://www.ubnt.com/download/'" | |
Exit | |
} | |
Write-Host "Found UniFi installation directory (using '$unifi')." | |
return $unifiHome | |
} | |
function Grant-ControllerFirewallAccess | |
{ | |
$filter = Get-NetFirewallPortFilter -Protocol TCP | ? { $_.LocalPort -eq 8443 } | |
if ($filter) | |
{ | |
$matchingRules = $filter | Get-NetFirewallRule | |
$validRules = $matchingRules | ? { $_.Action -eq "Allow" -and $_.Enabled -eq "True" } | |
if ($validRules -ne $null -and $validRules.Length -eq $matchingRules.Length) | |
{ | |
Write-Host "No need to configure firewall" | |
Return | |
} | |
else | |
{ | |
Write-Warning "Warning: More than one rule was found for TCP Port 8443. Please verify that no rules will block inbound access" | |
Write-Warning "Matching Firewall rules for TCP 8443:" | |
$matchingRules | % { Write-Warning ("{0} ({1} - {2})" -f $_.DisplayName,$_.Action,$_.Enabled) } | |
} | |
} | |
Write-Host "Configuring Firewall Rule 'UniFi Controller' to allow inbound access on port 8443" | |
New-NetFirewallRule -Action Allow -Direction Inbound -DisplayName "UniFi Controller" -Enabled True -LocalPort 8443 -Protocol "TCP" | |
} | |
Request-ElevatedPrompt | |
$java = Get-JavaHome | |
$unifi = Get-UnifiHome | |
Write-Host "Setting JAVA_HOME environment variable" | |
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", $javaHome, "Machine") | |
$key = [Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true) | |
$path = $key.GetValue("Path", $null, "DoNotExpandEnvironmentNames") | |
if (!($path -Like "*%JAVA_HOME%*")) | |
{ | |
Write-Host "Adding JAVA_HOME to PATH environment variable" | |
if (!$path.EndsWith(";")) | |
{ | |
$path += ";" | |
} | |
$path += "%JAVA_HOME%" | |
$key.SetValue("Path", $path, "ExpandString") | |
} | |
else | |
{ | |
Write-Host "PATH already contains JAVA_HOME" | |
} | |
$key.Dispose() | |
Push-Location $unifi | |
Write-Host "Installing UniFi Controller as service" | |
& "java.exe" "-jar" ".\lib\ace.jar" "installsvc" | |
Start-Service -Name "UniFi" | |
Pop-Location | |
# Comment the following line if you do not need to grant firewall access | |
Grant-ControllerFirewallAccess |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment