@@ -196,40 +196,62 @@ function Register-Agent {
196196
197197function Setup-WindowsService {
198198 param ([string ]$BinaryPath )
199-
199+
200200 Write-Info " "
201201 Write-Info " Setting up Windows scheduled task for auto-start..."
202-
202+
203203 # Remove existing task if exists
204204 $existingTask = Get-ScheduledTask - TaskName $TASK_NAME - ErrorAction SilentlyContinue
205205 if ($existingTask ) {
206206 Write-Info " Removing existing scheduled task..."
207207 Unregister-ScheduledTask - TaskName $TASK_NAME - Confirm:$false
208208 }
209-
209+
210+ # Capture the registering user's home directory so the task can find config/cache
211+ # even when running without an interactive session.
212+ $userHome = $env: USERPROFILE
213+ $configDir = Join-Path $userHome " .gpugo\config"
214+ $stateDir = Join-Path $userHome " .gpugo\state"
215+ $cacheDir = Join-Path $userHome " .gpugo\cache"
216+
217+ # Pass explicit paths so the agent resolves files under the registering user's
218+ # profile regardless of which account the scheduled task runs as.
219+ $agentArgs = " agent start --config-dir `" $configDir `" --state-dir `" $stateDir `" "
220+
210221 # Create scheduled task for auto-start at system startup
211- $action = New-ScheduledTaskAction - Execute $BinaryPath - Argument " agent start"
212-
222+ $action = New-ScheduledTaskAction - Execute $BinaryPath - Argument $agentArgs
223+
224+ # Environment variable so DownloadOrFindAccelerator and deps manager
225+ # locate cached libraries in the registering user's cache directory.
226+ $envSetting = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable - RestartCount 3 - RestartInterval (New-TimeSpan - Minutes 1 )
227+
213228 # Trigger at system startup
214229 $trigger = New-ScheduledTaskTrigger - AtStartup
215-
216- # Run as SYSTEM with highest privileges
217- $principal = New-ScheduledTaskPrincipal - UserId " SYSTEM " - LogonType ServiceAccount - RunLevel Highest
218-
219- # Settings
220- $settings = New-ScheduledTaskSettingsSet - AllowStartIfOnBatteries - DontStopIfGoingOnBatteries - StartWhenAvailable - RestartCount 3 - RestartInterval ( New-TimeSpan - Minutes 1 )
221-
230+
231+ # Run as the current (registering) user via S4U logon so the task can access
232+ # user-profile paths without requiring the user to be interactively logged in.
233+ # S4U does not store a password and still uses the user's profile environment.
234+ $currentUser = [ System.Security.Principal.WindowsIdentity ]::GetCurrent().Name
235+ $principal = New-ScheduledTaskPrincipal - UserId $currentUser - LogonType S4U - RunLevel Highest
236+
222237 # Register the task
223- Register-ScheduledTask - TaskName $TASK_NAME - Action $action - Trigger $trigger - Principal $principal - Settings $settings - Description " GPU Go Agent - GPU Sharing Service" | Out-Null
224-
238+ Register-ScheduledTask - TaskName $TASK_NAME - Action $action - Trigger $trigger - Principal $principal - Settings $envSetting - Description " GPU Go Agent - GPU Sharing Service" | Out-Null
239+
240+ # Persist GGO_CACHE_DIR as a machine-level environment variable so the
241+ # scheduled task can locate cached libraries when running without an
242+ # interactive session. Machine-level env vars are inherited by all processes.
243+ [Environment ]::SetEnvironmentVariable(" GGO_CACHE_DIR" , $cacheDir , " Machine" )
244+ $env: GGO_CACHE_DIR = $cacheDir
245+
225246 Write-Info " Scheduled task '$TASK_NAME ' created successfully!"
226-
247+
227248 # Start the task immediately
228249 Write-Info " Starting GPU Go agent..."
229250 Start-ScheduledTask - TaskName $TASK_NAME
230-
231- Start-Sleep - Seconds 2
232-
251+
252+ # Allow the agent a few seconds to initialise (library load + API handshake)
253+ Start-Sleep - Seconds 5
254+
233255 $taskInfo = Get-ScheduledTask - TaskName $TASK_NAME
234256 if ($taskInfo.State -eq " Running" ) {
235257 Write-Info " Agent started successfully!"
@@ -327,7 +349,7 @@ function Install-Ggo {
327349 # Add to PATH (system-wide if admin, user otherwise)
328350 if ($AddToPath ) {
329351 if (Test-Administrator ) {
330- Add-ToSystemPath - Directory $InstallDir
352+ $null = Add-ToSystemPath - Directory $InstallDir
331353 } else {
332354 $currentPath = [Environment ]::GetEnvironmentVariable(" PATH" , " User" )
333355 if ($currentPath -notlike " *$InstallDir *" ) {
@@ -360,10 +382,10 @@ function Install-Ggo {
360382 Write-Host " ==========================================" - ForegroundColor Yellow
361383
362384 # Register agent
363- Register-Agent - BinaryPath $destPath - AgentToken $Token
364-
385+ $null = Register-Agent - BinaryPath $destPath - AgentToken $Token
386+
365387 # Setup Windows service
366- Setup- WindowsService - BinaryPath $destPath
388+ $null = Setup- WindowsService - BinaryPath $destPath
367389
368390 Write-Host " "
369391 Write-Host " ==========================================" - ForegroundColor Green
0 commit comments