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
|
2020-10-22 14:01:35 -04:00
|
|
|
LogMsg "Attempting to start the ZeroTierOneService service..."
|
|
|
|
|
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 }
|
|
|
|
|
Catch { LogErr $_.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
|
|
|
|
|
{
|
|
|
|
|
LogMsg " ZeroTierOneService service is started"
|
|
|
|
|
$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
|
2020-10-22 14:01:35 -04:00
|
|
|
LogMsg "Joining ZeroTier network ${JoinNetwork}"
|
|
|
|
|
Try { Start-Process $ZTcli -ArgumentList "join ${JoinNetwork}" -Wait }
|
|
|
|
|
Catch { LogErr $_.Exception.Message }
|
|
|
|
|
}
|
|
|
|
|
Else { LogMsg "Could not find ""${ZTcli}""" }
|
2019-11-11 19:22:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
## If the service still isn't running, just error out
|
2020-10-22 14:01:35 -04:00
|
|
|
Else { LogMsg " 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
|
|
|
}
|
|
|
|
|
Else { LogMsg "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 )
|
|
|
|
|
{
|
|
|
|
|
LogMsg "Deleting Start Menu shortcut from ""${ShortcutPath}"""
|
|
|
|
|
Try { Remove-Item $ShortcutPath -Force }
|
|
|
|
|
Catch { LogErr $_.Exception.Message }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
Else { LogMsg "ZeroTier can only be installed on Windows 7 or higher operating systems" }
|