Files
management-scripts/Install-ZeroTier.ps1
T
2020-05-22 14:35:46 -04:00

78 lines
3.2 KiB
PowerShell

param(
[string]$JoinNetwork
)
## Only specify a network ID here if it isn't specified in an environment variable (RMM or endpoint) or you need to override that value:
$JoinNetwork = ''
## If $JoinNetwork is not specified at the command line, use the ZTNetworkID customer custom field if it exists
If ( (-not $JoinNetwork) -and ($ZeroTierNetworkID) ) { $JoinNetwork = $ZeroTierNetworkID }
## Get Windows version
$WinVersion = [double]("{0}.{1}" -f ([System.Environment]::OSVersion.Version).Major,([System.Environment]::OSVersion.Version).Minor)
## Make sure we're running at least Windows 7
If ( $WinVersion -ge 6.1 ) {
## Specify the ZeroTier One MSI installer URL
$URL = 'https://download.zerotier.com/dist/ZeroTier%20One.msi'
## Install ZeroTier One
Start-Process "msiexec.exe" -ArgumentList "/i ${URL} /qn /norestart" -Wait
## Make sure there's a network to join before attempting to start the ZeroTierOneService or join a network
If ( $JoinNetwork ) {
## 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
For ( $i = 1 ; $i -le 5 ; $i++ ) {
## Check to see if the service is running
If ( (Get-Service -Name ZeroTierOneService).Status -ne "Running" ) {
## Attempt to stop/start the service
Restart-Service -Name ZeroTierOneService -Force
## Wait a few seconds before checking again
Start-Sleep -Seconds 5
}
## If the service is running, break the loop
Else { $ServiceRunning = $true ; break }
}
## Make sure the service is running before attempting to join a network
If ( $ServiceRunning ) {
## Set the path to the ZeroTier installation directory
If ( Test-Path "${Env:ProgramFiles(x86)}" ) { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\WOW6432Node\ZeroTier, Inc.\ZeroTier One').Path }
Else { $ZTPath = (Get-ItemProperty -Path 'HKLM:\SOFTWARE\ZeroTier, Inc.\ZeroTier One').Path }
## Make sure the path actually exists
If ( Test-Path $ZTPath ) {
$ZTcli = "${ZTPath}\zerotier-cli.bat"
## Join the network, if $ZTcli exists
If ( Test-Path $ZTcli ) { Start-Process $ZTcli -ArgumentList "join ${JoinNetwork}" -Wait }
## Otherwise write an error message
Else { Write-Host "Could not find zerotier-cli" }
} Else { Write-Host "ZeroTier may not be installed correctly" }
}
## If the service still isn't running, just error out
Else { Write-Host "ZeroTierOneService service could not be started" }
} Else { Write-Host "A network to join was not specified" }
## Remove the ZeroTier One Start Menu shortcut
$ShortcutPath = "${ProgramData}\Microsoft\Windows\Start Menu\Programs\ZeroTier One.lnk"
If ( Test-Path $ShortcutPath ) { Remove-Item $ShortcutPath -Force }
} Else { Write-Host "ZeroTier requires at least Windows 7 to function properly" }