diff --git a/README.md b/README.md index 647f376..ddda688 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,54 @@ -⚙️ 安裝步驟: - 1. 準備:將 heartbeat.ps1 與 config.json 放入 scripts 資料夾 - 2. 打包:整個 KtvSetup 資料夾複製到每台設備 - 3. 安裝:右鍵以「系統管理員身份」執行 install.ps1 +# KTV Heartbeat + FTP 自動安裝工具 -⸻ +這個工具包會在機器上自動部署: +1. 開機自動上報心跳至後台 API +2. 安裝並設定 IIS FTP Server -📌 注意事項: - • 安裝後,腳本會每次開機自動執行並回傳心跳。 - • 若 config.json 中有變更,只需更新該檔案即可。 - • 若需卸載,只要刪除工作排程即可: - Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false \ No newline at end of file +--- + +## ⚙️ 安裝步驟: + +1. 準備: + - 將 `KtvSetup` 整包複製到每台設備上 +2. 安裝: + - 右鍵以「系統管理員身份」執行 `install.ps1` + +--- + +## 📁 結構說明 +``` +KtvSetup/ +├── install.ps1 +├── scripts/ +│ ├── heartbeat.ps1 +│ ├── config.json +│ └── setup-ftp.ps1 +``` + +## 🧾 安裝內容: + +- 自動部署下列腳本至 `C:\scripts\` + - `heartbeat.ps1`:每分鐘上傳設備狀態 + - `config.json`:設備設定值(API 路徑、FTP 帳密等) + - `setup-ftp.ps1`:FTP 伺服器自動安裝與設定 + +- 建立工作排程於開機時執行 `heartbeat.ps1` + +- 自動安裝: + - IIS Web Server + - FTP Server 與 FTP Extensibility + - IIS 管理主控台 + - 建立指定的 FTP 使用者 + - 配置 FTP Site(使用電腦名稱) + - 設定防火牆開放 FTP Port + +--- + +## 📌 注意事項: + +- 安裝後會在每次開機時,自動執行心跳腳本。 +- 若要修改設定,只需編輯 `C:\scripts\config.json`。 +- 若需卸載,請移除排程工作: + +```powershell +Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false \ No newline at end of file diff --git a/install.ps1 b/install.ps1 index e1a4560..7249677 100644 --- a/install.ps1 +++ b/install.ps1 @@ -16,6 +16,20 @@ 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`"" @@ -24,4 +38,4 @@ $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 +Write-Host "ok Installation Complete: The script has been deployed and will run at system startup." -ForegroundColor Green \ No newline at end of file diff --git a/scripts/config.json b/scripts/config.json index 1308956..2fe5802 100644 --- a/scripts/config.json +++ b/scripts/config.json @@ -1,6 +1,10 @@ { - "apiUrl": "https://superstar.dnsnet.cc", + "apiUrl": "http://superstar.dnsnet.cc", "branchName": "測試", "email": "MachineKTV@gmail.com", - "password": "aa147258-" + "password": "aa147258-", + "ftpUser": "MachineKTV", + "ftpPassword": "aa147258-", + "ftpRoot": "D:\\", + "ftpPort": 21 } \ No newline at end of file diff --git a/scripts/setup-ftp.ps1 b/scripts/setup-ftp.ps1 new file mode 100644 index 0000000..5f59315 --- /dev/null +++ b/scripts/setup-ftp.ps1 @@ -0,0 +1,115 @@ +# setup-ftp.ps1 +# 必須以系統管理員執行 + +# 使用相對於腳本的設定檔路徑 +$configPath = Join-Path $PSScriptRoot "config.json" + +if (-not (Test-Path $configPath)) { + Write-Error "找不到設定檔:$configPath" + exit 1 +} + +try { + $configContent = Get-Content $configPath -Raw | ConvertFrom-Json +} catch { + Write-Error "讀取設定檔失敗:$($_.Exception.Message)" + exit 1 +} + +# 取得設定值與預設值處理 +$ftpUser = $configContent.ftpUser ?: "ftpuser" +$ftpPassword = $configContent.ftpPassword ?: "P@ssw0rd123" +$ftpRoot = $configContent.ftpRoot ?: "D:\" +$ftpPort = $configContent.ftpPort ?: 21 +$ftpSiteName = $env:COMPUTERNAME + +Write-Host "開始安裝 IIS 與 FTP Server..." + +# 啟用必要功能 +$features = @( + "IIS-WebServerRole", + "IIS-FTPServer", + "IIS-FTPExtensibility", + "IIS-ManagementConsole" +) +foreach ($feature in $features) { + Write-Host "啟用 Windows 功能:$feature" + Enable-WindowsOptionalFeature -Online -FeatureName $feature -All -NoRestart -ErrorAction SilentlyContinue +} + +# 重啟 IIS +Write-Host "重啟 IIS..." +iisreset + +# 建立 FTP 根目錄 +if (-not (Test-Path $ftpRoot)) { + Write-Host "建立 FTP 根目錄:$ftpRoot" + New-Item -Path $ftpRoot -ItemType Directory | Out-Null +} else { + Write-Host "FTP 根目錄已存在:$ftpRoot" +} + +# 建立本機使用者 +if (-not (Get-LocalUser -Name $ftpUser -ErrorAction SilentlyContinue)) { + Write-Host "建立 FTP 使用者帳號:$ftpUser" + $securePass = ConvertTo-SecureString $ftpPassword -AsPlainText -Force + New-LocalUser -Name $ftpUser -Password $securePass -FullName "FTP User" -Description "FTP專用帳號" -PasswordNeverExpires +} else { + Write-Host "FTP 使用者帳號已存在:$ftpUser" +} + +# 加入 Users 群組 +Add-LocalGroupMember -Group "Users" -Member $ftpUser -ErrorAction SilentlyContinue + +# 設定權限 +$acl = Get-Acl $ftpRoot +$rule = New-Object System.Security.AccessControl.FileSystemAccessRule("$env:COMPUTERNAME\$ftpUser", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow") +$acl.AddAccessRule($rule) +Set-Acl $ftpRoot $acl +Write-Host "設定使用者對 FTP 根目錄的權限完成" + +# 匯入 WebAdministration 模組 +Import-Module WebAdministration + +# 建立 FTP Site +$bindingInformation = "*:$ftpPort:" +if (Get-Website -Name $ftpSiteName -ErrorAction SilentlyContinue) { + Write-Host "已有同名 FTP Site,刪除:$ftpSiteName" + Remove-Website -Name $ftpSiteName +} + +Write-Host "建立 FTP Site:$ftpSiteName" +New-Item "IIS:\Sites\$ftpSiteName" -bindings @{protocol="ftp";bindingInformation=$bindingInformation} -physicalPath $ftpRoot + +# 設定驗證方式 +Set-ItemProperty "IIS:\Sites\$ftpSiteName" -Name ftpServer.security.authentication.anonymousAuthentication.enabled -Value $false +Set-ItemProperty "IIS:\Sites\$ftpSiteName" -Name ftpServer.security.authentication.basicAuthentication.enabled -Value $true + +# 授權使用者 +Remove-WebConfiguration -Filter "system.ftpServer/security/authorization/authorizationRules" -PSPath "IIS:\Sites\$ftpSiteName" -ErrorAction SilentlyContinue +Add-WebConfiguration -PSPath "IIS:\Sites\$ftpSiteName" -Filter "system.ftpServer/security/authorization/authorizationRules" -Value @{ + accessType = "Allow" + users = $ftpUser + permissions = "Read, Write" +} + +# 設定被動模式端口範圍 +Set-ItemProperty "IIS:\Sites\$ftpSiteName" -Name ftpServer.firewallSupport.passivePortRange -Value "50000-51000" + +# 開防火牆例外 +if (-not (Get-NetFirewallRule -DisplayName "FTP Server ($ftpPort)" -ErrorAction SilentlyContinue)) { + New-NetFirewallRule -DisplayName "FTP Server ($ftpPort)" -Direction Inbound -Protocol TCP -LocalPort $ftpPort -Action Allow -Profile Any +} +if (-not (Get-NetFirewallRule -DisplayName "FTP Passive Ports" -ErrorAction SilentlyContinue)) { + New-NetFirewallRule -DisplayName "FTP Passive Ports" -Direction Inbound -Protocol TCP -LocalPort 50000-51000 -Action Allow -Profile Any +} + +# 啟動 FTP 服務 +Start-Service ftpsvc -ErrorAction SilentlyContinue + +Write-Host "✅ IIS FTP 服務安裝與設定完成!" +Write-Host "帳號:" $ftpUser +Write-Host "密碼:" $ftpPassword +Write-Host "目錄:" $ftpRoot +Write-Host "站名:" $ftpSiteName +Write-Host "連接埠:" $ftpPort \ No newline at end of file