Files
management-scripts/Install-ZeroTier.ps1
T

81 lines
3.4 KiB
PowerShell
Raw Normal View History

2020-10-22 14:01:35 -04:00
param([string]$JoinNetwork = '')
2019-11-11 19:22:39 -05:00
2020-05-22 14:35:46 -04:00
## If $JoinNetwork is not specified at the command line, use the ZTNetworkID customer custom field if it exists
2020-10-22 14:01:35 -04:00
If ( ($JoinNetwork = '') -and ($ZeroTierNetworkID) ) { $JoinNetwork = $ZeroTierNetworkID }
2019-11-11 19:22:39 -05:00
## Make sure we're running at least Windows 7
2020-10-22 14:01:35 -04:00
If ( IsWindowsVersion -ge "6.1" )
{
2019-11-11 19:22:39 -05:00
## Specify the ZeroTier One MSI installer URL
$URL = 'https://download.zerotier.com/dist/ZeroTier%20One.msi'
## Install ZeroTier One
2020-10-22 14:01:35 -04:00
Install-Program "msiexec.exe" -Arguments "/i ${URL} /qn /norestart"
2019-11-11 19:22:39 -05:00
## Make sure there's a network to join before attempting to start the ZeroTierOneService or join a network
2020-10-22 14:01:35 -04:00
If ( $JoinNetwork )
{
2019-11-11 19:22:39 -05:00
## This will be used to determine whether or not the service was successfully started
$ServiceRunning = $false
## Check to make sure the ZeroTierOneService has started
## We'll only loop through this for a few seconds before failing
2023-10-05 13:11:38 -04:00
Write-Output "Attempting to start the ZeroTierOneService service..."
2020-10-22 14:01:35 -04:00
For ( $i = 1 ; $i -le 5 ; $i++ )
{
2019-11-11 19:22:39 -05:00
## Check to see if the service is running
2020-10-22 14:01:35 -04:00
If ( (Get-Service -Name ZeroTierOneService).Status -ne "Running" )
{
2019-11-11 19:22:39 -05:00
## Attempt to stop/start the service
2020-10-22 14:01:35 -04:00
Try { Restart-Service -Name ZeroTierOneService -Force }
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_.Exception.Message }
2019-11-11 19:22:39 -05:00
## Wait a few seconds before checking again
Start-Sleep -Seconds 5
}
## If the service is running, break the loop
2020-10-22 14:01:35 -04:00
Else
{
2023-10-05 13:11:38 -04:00
Write-Output " ZeroTierOneService service is started"
2020-10-22 14:01:35 -04:00
$ServiceRunning = $true ; break
}
2019-11-11 19:22:39 -05:00
}
## Make sure the service is running before attempting to join a network
2020-10-22 14:01:35 -04:00
If ( $ServiceRunning )
{
2019-11-11 19:22:39 -05:00
## Set the path to the ZeroTier installation directory
2020-10-22 14:01:35 -04:00
If ( Is64bit ) { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\ZeroTier, Inc.\ZeroTier One').Path }
Else { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\ZeroTier, Inc.\ZeroTier One').Path }
2019-11-11 19:22:39 -05:00
## Make sure the path actually exists
2020-10-22 14:01:35 -04:00
$ZTcli = "${ZTPath}\zerotier-cli.bat"
If ( Test-Path "${ZTcli}" )
{
2019-11-11 19:22:39 -05:00
## Join the network, if $ZTcli exists
2023-10-05 13:11:38 -04:00
Write-Output "Joining ZeroTier network ${JoinNetwork}"
2020-10-22 14:01:35 -04:00
Try { Start-Process $ZTcli -ArgumentList "join ${JoinNetwork}" -Wait }
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_.Exception.Message }
2020-10-22 14:01:35 -04:00
}
2023-10-05 13:11:38 -04:00
Else { Write-Output "Could not find ""${ZTcli}""" }
2019-11-11 19:22:39 -05:00
}
## If the service still isn't running, just error out
2023-10-05 13:11:38 -04:00
Else { Write-Output " ZeroTierOneService could not be started in a timely fashion" }
2019-11-11 19:22:39 -05:00
2020-10-22 14:01:35 -04:00
}
2023-10-05 13:11:38 -04:00
Else { Write-Output "A ZeroTier network ID was not specified, will not join any network" }
2019-11-11 19:22:39 -05:00
## Remove the ZeroTier One Start Menu shortcut
$ShortcutPath = "${ProgramData}\Microsoft\Windows\Start Menu\Programs\ZeroTier One.lnk"
2020-10-22 14:01:35 -04:00
If ( Test-Path $ShortcutPath )
{
2023-10-05 13:11:38 -04:00
Write-Output "Deleting Start Menu shortcut from ""${ShortcutPath}"""
2020-10-22 14:01:35 -04:00
Try { Remove-Item $ShortcutPath -Force }
2023-10-05 13:17:18 -04:00
Catch { Write-Error $_.Exception.Message }
2020-10-22 14:01:35 -04:00
}
}
2023-10-05 13:11:38 -04:00
Else { Write-Output "ZeroTier can only be installed on Windows 7 or higher operating systems" }