major logging overhaul, plus other fixes

This commit is contained in:
2020-10-22 14:01:35 -04:00
parent 77cf4d6276
commit 978373ebe2
27 changed files with 430 additions and 611 deletions
+42 -39
View File
@@ -1,77 +1,80 @@
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 = ''
param([string]$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)
If ( ($JoinNetwork = '') -and ($ZeroTierNetworkID) ) { $JoinNetwork = $ZeroTierNetworkID }
## Make sure we're running at least Windows 7
If ( $WinVersion -ge 6.1 ) {
If ( IsWindowsVersion -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
Install-Program "msiexec.exe" -Arguments "/i ${URL} /qn /norestart"
## Make sure there's a network to join before attempting to start the ZeroTierOneService or join a network
If ( $JoinNetwork ) {
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++ ) {
LogMsg "Attempting to start the ZeroTierOneService service..."
For ( $i = 1 ; $i -le 5 ; $i++ )
{
## Check to see if the service is running
If ( (Get-Service -Name ZeroTierOneService).Status -ne "Running" ) {
If ( (Get-Service -Name ZeroTierOneService).Status -ne "Running" )
{
## Attempt to stop/start the service
Restart-Service -Name ZeroTierOneService -Force
Try { Restart-Service -Name ZeroTierOneService -Force }
Catch { LogErr $_.Exception.Message }
## Wait a few seconds before checking again
Start-Sleep -Seconds 5
}
## If the service is running, break the loop
Else { $ServiceRunning = $true ; break }
Else
{
LogMsg " ZeroTierOneService service is started"
$ServiceRunning = $true ; break
}
}
## Make sure the service is running before attempting to join a network
If ( $ServiceRunning ) {
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 }
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 }
## Make sure the path actually exists
If ( Test-Path $ZTPath ) {
$ZTcli = "${ZTPath}\zerotier-cli.bat"
$ZTcli = "${ZTPath}\zerotier-cli.bat"
If ( Test-Path "${ZTcli}" )
{
## 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" }
LogMsg "Joining ZeroTier network ${JoinNetwork}"
Try { Start-Process $ZTcli -ArgumentList "join ${JoinNetwork}" -Wait }
Catch { LogErr $_.Exception.Message }
}
Else { LogMsg "Could not find ""${ZTcli}""" }
}
## If the service still isn't running, just error out
Else { Write-Host "ZeroTierOneService service could not be started" }
Else { LogMsg " ZeroTierOneService could not be started in a timely fashion" }
} Else { Write-Host "A network to join was not specified" }
}
Else { LogMsg "A ZeroTier network ID was not specified, will not join any network" }
## 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 }
If ( Test-Path $ShortcutPath )
{
LogMsg "Deleting Start Menu shortcut from ""${ShortcutPath}"""
Try { Remove-Item $ShortcutPath -Force }
Catch { LogErr $_.Exception.Message }
}
} Else { Write-Host "ZeroTier requires at least Windows 7 to function properly" }
}
Else { LogMsg "ZeroTier can only be installed on Windows 7 or higher operating systems" }