# Run envd on a Windows desktop

Run `lightspeed-envd.exe` in a signed-in Windows session to make a workstation
or VM available to Lightspeed. Start it from PowerShell when you need it, or
set up a scheduled task to start it at sign-in. Commands run with that
account's Windows permissions, and GUI tools use its desktop.

## Choose an account and register the machine

1. Sign in to the Windows x64 machine with the account you want Lightspeed
   to use. For local development, this can be your usual account with access
   to the checkout and build tools. On a shared machine or test VM, you can
   use a dedicated standard account with access to the files and applications
   the work requires.
2. Obtain the Windows envd archive for your deployment's Lightspeed release
   (`lightspeed-envd-<version>-x86_64-pc-windows-msvc.tar.gz`). Extract
   `lightspeed-envd.exe` into `%LOCALAPPDATA%\Lightspeed\envd\bin`. Run
   `& "$env:LOCALAPPDATA\Lightspeed\envd\bin\lightspeed-envd.exe" --print-build`
   in PowerShell to check the build and protocol version.
3. In the intended Lightspeed universe, an **Operator** or **Owner** opens
   **Environments → Registration key**, chooses **Persistent** and mints a
   key. For a key dedicated to one machine, set the active environment limit
   to **1**. Save the secret
   alone as UTF-8 without a BOM in
   `%LOCALAPPDATA%\Lightspeed\envd\registration-key.txt` under the chosen account.
   Get the deployment's exact outbound gateway URL from the operator.

## Start envd in PowerShell

Save the launcher as `%LOCALAPPDATA%\Lightspeed\envd\Start-Envd.ps1` and
replace the example gateway URL with the one supplied by the operator.
By default, the environment appears under the computer's name and uses a
new `workspace` directory. You can set `$name` to a more descriptive name
and `$workspace` to an existing checkout. Keep private state and the
registration key outside that checkout. If the machine is already registered,
use its existing state directory to reconnect to the same environment.

```powershell
$ErrorActionPreference = 'Stop'
$base = Join-Path $env:LOCALAPPDATA 'Lightspeed\envd'
$name = $env:COMPUTERNAME
$workspace = Join-Path $base 'workspace'
$keyFile = Join-Path $base 'registration-key.txt'
New-Item -ItemType Directory -Force -Path $workspace | Out-Null

$envdArgs = @(
    '--gateway-url', 'wss://lightspeed.example.com/environments/connect'
    '--registration-name', $name
    '--registration-receipt', (Join-Path $base 'receipt.json')
    '--cwd', $workspace
    '--fs-root', $workspace
    '--state-dir', (Join-Path $base 'state')
)
if (Test-Path -LiteralPath $keyFile) {
    $envdArgs += @('--registration-key-file', $keyFile)
}

& (Join-Path $base 'bin\lightspeed-envd.exe') @envdArgs
exit $LASTEXITCODE
```

Run the launcher from PowerShell and keep the terminal open.

```powershell
& "$env:LOCALAPPDATA\Lightspeed\envd\Start-Envd.ps1"
```

When your machine appears ready in **Environments**, the daemon has
written its environment ID to `receipt.json`. Remove the admission key file
from the machine and retain the `state` directory. The launcher uses the
saved daemon identity on subsequent starts.

## Verify the environment

In a Lightspeed session, open **Session settings**, enable **Environments**,
attach the environment you registered with **Run commands** access, select it as the
**Active environment**, and choose **Apply setup**. Ask the agent to run
`whoami` and `Get-Location` in PowerShell. The results should match the
account running envd and the directory you set in `$workspace`.

If your work needs a GUI, verify one action in the application you intend
to use. For example, on a Citrix test VM, launch a published application
through Lightspeed and have your automation tool verify a known window title
or visible result.

## Optional startup at sign-in

Press **Ctrl+C** in the foreground terminal before starting the scheduled
copy. Open **Task Scheduler → Create Task** and configure the task.

| Tab      | Setting                                                                                                                                                                                                           |
| -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| General  | Name the task `Lightspeed envd`, select the account that ran the launcher, and choose **Run only when user is logged on**. Leave **Run with highest privileges** unchecked.                                       |
| Triggers | Choose **At log on → Specific user** and select that same account.                                                                                                                                                |
| Actions  | Choose **Start a program**. Use `powershell.exe` with arguments `-NoProfile -File "C:\Users\YOURNAME\AppData\Local\Lightspeed\envd\Start-Envd.ps1"`. Substitute the actual absolute script path for this account. |
| Settings | Clear **Stop the task if it runs longer than**. If the task is already running, choose **Do not start a new instance**.                                                                                           |

While signed in as that user, choose **Run** on the task. Confirm that the
same environment reconnects and repeat the `whoami` check. To diagnose a startup
failure, end the task and run the same launcher from that account's terminal
to see envd's error output.

Signing out ends envd and disconnects the environment. After reboot, it
reconnects at that account's next sign-in. If GUI automation must continue
after you close a Remote Desktop (RDP) client, verify the application again
with RDP closed. A disconnected or locked desktop can prevent screen capture
and input even while envd is reachable.
