加入 FTP 20250613

This commit is contained in:
allen.yan 2025-06-13 09:27:29 +08:00
parent 6d64ba0e85
commit 1d0a1b7f3c
4 changed files with 188 additions and 13 deletions

View File

@ -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
---
## ⚙️ 安裝步驟:
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

View File

@ -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`""

View File

@ -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
}

115
scripts/setup-ftp.ps1 Normal file
View File

@ -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