|
| 1 | +<powershell> |
| 2 | +# |
| 3 | +# Installs Tailscale, authenticates the device, and verifies the connection. |
| 4 | +# |
| 5 | +Start-Transcript -Path "$env:SystemRoot\Temp\tailscale-user-data.log" -Append |
| 6 | + |
| 7 | +Write-Host "`n#`n# Beginning Tailscale installation...`n#`n" |
| 8 | + |
| 9 | +# Create temp directory if it doesn't exist |
| 10 | +$tempPath = "$env:TEMP" |
| 11 | +if (-not (Test-Path $tempPath)) { |
| 12 | + New-Item -Path $tempPath -ItemType Directory -Force | Out-Null |
| 13 | +} |
| 14 | +$Installer = "$env:TEMP\tailscale.msi" |
| 15 | + |
| 16 | +# https://tailscale.com/docs/install/windows/msi |
| 17 | +$downloadTailscale = @{ |
| 18 | + Uri = "${tailscale_msi_url}" |
| 19 | + OutFile = $Installer |
| 20 | +} |
| 21 | +$downloadSuccess = $false |
| 22 | +for ($loop = 1; $loop -le 10; $loop++) { |
| 23 | + Write-Host "Downloading Tailscale attempt: [$loop / 10]" |
| 24 | + try { |
| 25 | + Invoke-WebRequest @downloadTailscale |
| 26 | + $downloadSuccess = $true |
| 27 | + break |
| 28 | + } |
| 29 | + catch { |
| 30 | + Write-Host "Download attempt $loop failed: $_" |
| 31 | + Start-Sleep -Seconds 2 |
| 32 | + } |
| 33 | +} |
| 34 | +if (-not $downloadSuccess) { |
| 35 | + Write-Host "Tailscale download failed. Exiting." |
| 36 | + exit 1 |
| 37 | +} |
| 38 | +Write-Host "Tailscale download successful." |
| 39 | + |
| 40 | +Write-Host "Installing $Installer" |
| 41 | +Start-Process msiexec.exe -ArgumentList "/i `"$Installer`"", "/qn", "/norestart" -Wait |
| 42 | +Write-Host "Install complete." |
| 43 | + |
| 44 | +Write-Host "Removing $Installer" |
| 45 | +Remove-Item $Installer -Force |
| 46 | + |
| 47 | +# Set the password for the local account below. schtasks needs the real, |
| 48 | +# current password for this account to create a task that runs at boot. |
| 49 | +Write-Host "Setting the password for local account [${username}]" |
| 50 | +net user "${username}" "${password}" /active:yes |
| 51 | + |
| 52 | +# Create a task to authenticate to the tailnet on boot, then run it now so |
| 53 | +# the device does not have to wait for a reboot to join the tailnet. |
| 54 | +Write-Host "`n#`n# Creating task to authenticate to tailnet on boot`n#`n" |
| 55 | +# /create = make a new task |
| 56 | +# /tn = task name |
| 57 | +# /tr = executable to run |
| 58 | +# /sc onstart = run on boot |
| 59 | +# /ru /rp = user and password to run the command as |
| 60 | +# /V1 /Z = delete the task after it has run |
| 61 | +schtasks /create /tn "TailscaleUpOnce" /tr "'C:\Program Files\Tailscale\tailscale.exe' up --unattended --hostname '${tailscale_hostname}' --auth-key '${tailscale_auth_key}'" /sc onstart /ru "${username}" /rp "${password}" /V1 /Z |
| 62 | +schtasks /run /tn "TailscaleUpOnce" |
| 63 | + |
| 64 | +Write-Host "Waiting for Tailscale to authenticate..." |
| 65 | +$connected = $false |
| 66 | +for ($loop = 1; $loop -le 30; $loop++) { |
| 67 | + & "C:\Program Files\Tailscale\tailscale.exe" status --peers=false *> $null |
| 68 | + if ($LASTEXITCODE -eq 0) { |
| 69 | + $connected = $true |
| 70 | + break |
| 71 | + } |
| 72 | + Start-Sleep -Seconds 2 |
| 73 | +} |
| 74 | +if ($connected) { |
| 75 | + Write-Host "`n#`n# Tailscale status: connected`n#`n" |
| 76 | +} else { |
| 77 | + Write-Host "`n#`n# Tailscale status: NOT connected`n#`n" |
| 78 | +} |
| 79 | + |
| 80 | +Write-Host "`n#`n# Complete.`n#`n" |
| 81 | + |
| 82 | +Stop-Transcript |
| 83 | +</powershell> |
0 commit comments