第一版完成測試 20250522
This commit is contained in:
parent
5e7b42c975
commit
626b8deaf7
20
install.ps1
20
install.ps1
@ -1,14 +1,22 @@
|
|||||||
# 建立目的資料夾
|
# Set up target folder
|
||||||
$targetDir = "C:\scripts"
|
$targetDir = "C:\scripts"
|
||||||
if (-not (Test-Path $targetDir)) {
|
if (-not (Test-Path $targetDir)) {
|
||||||
New-Item -Path $targetDir -ItemType Directory | Out-Null
|
New-Item -Path $targetDir -ItemType Directory | Out-Null
|
||||||
}
|
}
|
||||||
|
|
||||||
# 複製腳本與設定檔
|
# Always overwrite heartbeat.ps1
|
||||||
Copy-Item ".\scripts\heartbeat.ps1" -Destination "$targetDir\heartbeat.ps1" -Force
|
$source1 = Resolve-Path ".\scripts\heartbeat.ps1"
|
||||||
Copy-Item ".\scripts\config.json" -Destination "$targetDir\config.json" -Force
|
$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
|
||||||
|
}
|
||||||
|
|
||||||
|
# Set up scheduled task to run on system startup
|
||||||
$taskName = "KtvHeartbeat"
|
$taskName = "KtvHeartbeat"
|
||||||
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$targetDir\heartbeat.ps1`""
|
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-ExecutionPolicy Bypass -File `"$targetDir\heartbeat.ps1`""
|
||||||
$trigger = New-ScheduledTaskTrigger -AtStartup
|
$trigger = New-ScheduledTaskTrigger -AtStartup
|
||||||
@ -16,4 +24,4 @@ $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -RunLevel Highest
|
|||||||
|
|
||||||
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName -Force
|
Register-ScheduledTask -Action $action -Trigger $trigger -Principal $principal -TaskName $taskName -Force
|
||||||
|
|
||||||
Write-Host "✅ 安裝完成:腳本已部署並設為開機自動執行。" -ForegroundColor Green
|
Write-Host "ok Installation Complete: The script has been deployed and will run at system startup." -ForegroundColor Green
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
{
|
{
|
||||||
"apiUrl": "https://ktv.test",
|
"apiUrl": "https://superstar.dnsnet.cc",
|
||||||
"branchId": 1,
|
"branchId": 1,
|
||||||
"roomName": "102",
|
"roomName": "102",
|
||||||
"email": "MachineKTV@gmail.com",
|
"email": "MachineKTV@gmail.com",
|
||||||
|
@ -1,19 +1,29 @@
|
|||||||
# 讀取 config 檔
|
$logPath = "C:\scripts\heartbeat.log"
|
||||||
|
|
||||||
|
function Log($msg) {
|
||||||
|
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
|
||||||
|
Add-Content -Path $logPath -Value "[$timestamp] $msg"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Load config
|
||||||
$configPath = "C:\scripts\config.json"
|
$configPath = "C:\scripts\config.json"
|
||||||
if (!(Test-Path $configPath)) {
|
if (!(Test-Path $configPath)) {
|
||||||
Write-Error "❌ 找不到設定檔:$configPath"
|
Log "error Config file not found: $configPath"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
$config = Get-Content $configPath | ConvertFrom-Json
|
$config = Get-Content $configPath | ConvertFrom-Json
|
||||||
|
|
||||||
# 使用設定值
|
# Config values
|
||||||
$apiUrl = $config.apiUrl
|
$apiUrl = $config.apiUrl.TrimEnd('/') # 移除結尾的 /
|
||||||
$branchId = $config.branchId
|
$branchId = $config.branchId
|
||||||
$roomName = $config.roomName
|
$roomName = $config.roomName
|
||||||
$email = $config.email
|
$email = $config.email
|
||||||
$password = $config.password
|
$password = $config.password
|
||||||
|
|
||||||
# 取得本機 IP
|
Log "📡 Using API URL: $apiUrl"
|
||||||
|
|
||||||
|
# Get IP address
|
||||||
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
|
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
|
||||||
$_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*"
|
$_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*"
|
||||||
}).IPAddress
|
}).IPAddress
|
||||||
@ -22,18 +32,27 @@ function Get-Token {
|
|||||||
$body = @{
|
$body = @{
|
||||||
branch_id = $branchId
|
branch_id = $branchId
|
||||||
room_name = $roomName
|
room_name = $roomName
|
||||||
|
room_ip = $ip
|
||||||
email = $email
|
email = $email
|
||||||
password = $password
|
password = $password
|
||||||
} | ConvertTo-Json -Depth 3
|
} | ConvertTo-Json -Depth 3
|
||||||
|
|
||||||
$response = Invoke-RestMethod -Uri "$apiUrl/api/room/receiveRegister" -Method Post `
|
$url = "$apiUrl/api/room/receiveRegister"
|
||||||
-Body $body -ContentType "application/json"
|
Log "curl Requesting token from: $url"
|
||||||
|
|
||||||
if ($response.token) {
|
try {
|
||||||
Write-Output "✅ Token 取得成功:$($response.token)"
|
$response = Invoke-RestMethod -Uri $url -Method Post `
|
||||||
return $response.token
|
-Body $body -ContentType "application/json"
|
||||||
} else {
|
|
||||||
Write-Error "❌ 無法取得 Token"
|
if ($response.data.token) {
|
||||||
|
Log "ok Token acquired: $($response.data.token)"
|
||||||
|
return $response.data.token
|
||||||
|
} else {
|
||||||
|
Log "error Failed to acquire token (no token in response)"
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
Log "error Token request failed: $($_.Exception.Message)"
|
||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,19 +73,15 @@ function Send-Heartbeat($token, $ip) {
|
|||||||
-Headers @{ Authorization = "Bearer $token" } `
|
-Headers @{ Authorization = "Bearer $token" } `
|
||||||
-Body $heartbeat -ContentType "application/json"
|
-Body $heartbeat -ContentType "application/json"
|
||||||
|
|
||||||
Write-Output "✅ 心跳送出:$(Get-Date)"
|
Log "ok Heartbeat sent successfully"
|
||||||
} catch {
|
} catch {
|
||||||
Write-Error "❌ 心跳失敗:$($_.Exception.Message)"
|
Log "error Heartbeat failed: $($_.Exception.Message)"
|
||||||
}
|
}
|
||||||
|
|
||||||
Start-Sleep -Seconds 60
|
Start-Sleep -Seconds 60
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# 執行主流程
|
# Main
|
||||||
$token = Get-Token
|
$token = Get-Token
|
||||||
|
Send-Heartbeat -token $token -ip $ip
|
||||||
while ($true) {
|
|
||||||
Send-Heartbeat -token $token -ip $ip
|
|
||||||
Start-Sleep -Seconds 60
|
|
||||||
}
|
|
16
操作說明.txt
16
操作說明.txt
@ -10,3 +10,19 @@
|
|||||||
• 若 config.json 中有變更,只需更新該檔案即可。
|
• 若 config.json 中有變更,只需更新該檔案即可。
|
||||||
• 若需卸載,只要刪除工作排程即可:
|
• 若需卸載,只要刪除工作排程即可:
|
||||||
Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false
|
Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false
|
||||||
|
|
||||||
|
|
||||||
|
Set-ExecutionPolicy RemoteSigned -Scope Process -Force
|
||||||
|
& "C:\KtvSetup\install.ps1"
|
||||||
|
|
||||||
|
Get-ScheduledTask -TaskName "KtvHeartbeat" | Select-Object TaskName, State
|
||||||
|
若狀態是 Disabled,可使用以下指令啟用:
|
||||||
|
Enable-ScheduledTask -TaskName "KtvHeartbeat"
|
||||||
|
然後再次查看執行結果
|
||||||
|
Get-ScheduledTask -TaskName "KtvHeartbeat" | Get-ScheduledTaskInfo
|
||||||
|
|
||||||
|
|
||||||
|
手動測試任務能否正常執行
|
||||||
|
Start-ScheduledTask -TaskName "KtvHeartbeat"
|
||||||
|
|
||||||
|
Stop-ScheduledTask -TaskName "KtvHeartbeat"
|
Loading…
x
Reference in New Issue
Block a user