diff --git a/windows/setup.ps1 b/windows/setup.ps1 index de90b1c..3bb0c8f 100644 --- a/windows/setup.ps1 +++ b/windows/setup.ps1 @@ -27,6 +27,10 @@ Sync interval in minutes for the scheduled task. Default: 5 +.PARAMETER NonInteractive + Run without prompts. Auto-accepts detected BenjaminTeam folder. + Use this when running from scripts or scheduled tasks. + .EXAMPLE .\setup.ps1 @@ -39,7 +43,8 @@ param( [string]$ServerUser = "rclone-sync", [string]$ServerPath = "/data/jingtian/BenjaminTeam", [string]$LocalPath = "", - [int]$SyncInterval = 5 + [int]$SyncInterval = 5, + [switch]$NonInteractive ) # Configuration @@ -202,13 +207,22 @@ if ($LocalPath -and (Test-Path $LocalPath)) { if ($foundPath) { Write-Info "Found BenjaminTeam folder at: $foundPath" - $confirm = Read-Host " Use this path? (Y/n)" - if ($confirm -eq "" -or $confirm -match "^[Yy]") { + if ($NonInteractive) { + # Auto-accept in non-interactive mode $LocalPath = $foundPath + } else { + $confirm = Read-Host " Use this path? (Y/n)" + if ($confirm -eq "" -or $confirm -match "^[Yy]") { + $LocalPath = $foundPath + } } } if (-not $LocalPath -or -not (Test-Path $LocalPath)) { + if ($NonInteractive) { + Write-ErrorMsg "Could not auto-detect BenjaminTeam folder. Use -LocalPath parameter." + exit 1 + } Write-Info "Could not auto-detect BenjaminTeam folder." $LocalPath = Read-Host " Enter the full path to your BenjaminTeam folder" @@ -249,15 +263,29 @@ if (-not (Test-Path $RcloneKeySource)) { Copy-Item -Path $RcloneKeySource -Destination $RcloneKeyDest -Force Write-Success "SSH key copied to $RcloneKeyDest" -# Test SSH connection +# Test SSH connection (fully non-interactive) Write-Info "Testing SSH connection to $ServerHost..." -$sshTest = & ssh -o StrictHostKeyChecking=accept-new -o BatchMode=yes -i $RcloneKeyDest "$ServerUser@$ServerHost" "echo 'SSH_OK'" 2>&1 +$sshArgs = @( + "-o", "StrictHostKeyChecking=no", + "-o", "BatchMode=yes", + "-o", "ConnectTimeout=10", + "-o", "UserKnownHostsFile=$env:USERPROFILE\.ssh\known_hosts", + "-i", $RcloneKeyDest, + "$ServerUser@$ServerHost", + "echo 'SSH_OK'" +) -if ($sshTest -match "SSH_OK") { - Write-Success "SSH connection successful" -} else { - Write-ErrorMsg "SSH connection failed: $sshTest" - Write-Info "Please check the server is running and the key is correct." +try { + $sshTest = & ssh @sshArgs 2>&1 + if ($sshTest -match "SSH_OK") { + Write-Success "SSH connection successful" + } else { + Write-ErrorMsg "SSH connection failed: $sshTest" + Write-Info "Please check the server is running and the key is correct." + exit 1 + } +} catch { + Write-ErrorMsg "SSH test error: $_" exit 1 }