# Set up target folder $targetDir = "C:\scripts" if (-not (Test-Path $targetDir)) { New-Item -Path $targetDir -ItemType Directory | Out-Null } # Always overwrite heartbeat.ps1 $source1 = Resolve-Path ".\scripts\heartbeat.ps1" $dest1 = "$targetDir\heartbeat.ps1" Copy-Item $source1 -Destination $dest1 -Force # Only copy config.json if it does not exist $source2 = Resolve-Path ".\scripts\config.json" $dest2 = "$targetDir\config.json" if (-not (Test-Path $dest2)) { Copy-Item $source2 -Destination $dest2 -Force } # Copy setup-ftp.ps1 as well (假設放在 scripts 資料夾) $source3 = Resolve-Path ".\scripts\setup-ftp.ps1" $dest3 = "$targetDir\setup-ftp.ps1" Copy-Item $source3 -Destination $dest3 -Force # 執行 setup-ftp.ps1,需系統管理員權限 Write-Host "開始安裝 IIS FTP 服務..." try { & powershell.exe -ExecutionPolicy Bypass -File "$dest3" Write-Host "FTP 服務安裝完成" -ForegroundColor Green } catch { Write-Host "FTP 服務安裝失敗:" $_.Exception.Message -ForegroundColor Red } # Set up scheduled task to run on system startup $taskName = "KtvHeartbeat" $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$targetDir\heartbeat.ps1`"" $trigger = New-ScheduledTaskTrigger -AtStartup $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName -Force Write-Host "ok Installation Complete: The script has been deployed and will run at system startup." -ForegroundColor Green