第一版 20250522
This commit is contained in:
parent
28d847b8ea
commit
5e7b42c975
12
README.md
12
README.md
@ -0,0 +1,12 @@
|
||||
⚙️ 安裝步驟:
|
||||
1. 準備:將 heartbeat.ps1 與 config.json 放入 scripts 資料夾
|
||||
2. 打包:整個 KtvSetup 資料夾複製到每台設備
|
||||
3. 安裝:右鍵以「系統管理員身份」執行 install.ps1
|
||||
|
||||
⸻
|
||||
|
||||
📌 注意事項:
|
||||
• 安裝後,腳本會每次開機自動執行並回傳心跳。
|
||||
• 若 config.json 中有變更,只需更新該檔案即可。
|
||||
• 若需卸載,只要刪除工作排程即可:
|
||||
Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false
|
19
install.ps1
Normal file
19
install.ps1
Normal file
@ -0,0 +1,19 @@
|
||||
# 建立目的資料夾
|
||||
$targetDir = "C:\scripts"
|
||||
if (-not (Test-Path $targetDir)) {
|
||||
New-Item -Path $targetDir -ItemType Directory | Out-Null
|
||||
}
|
||||
|
||||
# 複製腳本與設定檔
|
||||
Copy-Item ".\scripts\heartbeat.ps1" -Destination "$targetDir\heartbeat.ps1" -Force
|
||||
Copy-Item ".\scripts\config.json" -Destination "$targetDir\config.json" -Force
|
||||
|
||||
# 設定開機自動執行任務
|
||||
$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 "✅ 安裝完成:腳本已部署並設為開機自動執行。" -ForegroundColor Green
|
BIN
scripts/.DS_Store
vendored
Normal file
BIN
scripts/.DS_Store
vendored
Normal file
Binary file not shown.
7
scripts/config.json
Normal file
7
scripts/config.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"apiUrl": "https://ktv.test",
|
||||
"branchId": 1,
|
||||
"roomName": "102",
|
||||
"email": "MachineKTV@gmail.com",
|
||||
"password": "aa147258-"
|
||||
}
|
72
scripts/heartbeat.ps1
Normal file
72
scripts/heartbeat.ps1
Normal file
@ -0,0 +1,72 @@
|
||||
# 讀取 config 檔
|
||||
$configPath = "C:\scripts\config.json"
|
||||
if (!(Test-Path $configPath)) {
|
||||
Write-Error "❌ 找不到設定檔:$configPath"
|
||||
exit 1
|
||||
}
|
||||
$config = Get-Content $configPath | ConvertFrom-Json
|
||||
|
||||
# 使用設定值
|
||||
$apiUrl = $config.apiUrl
|
||||
$branchId = $config.branchId
|
||||
$roomName = $config.roomName
|
||||
$email = $config.email
|
||||
$password = $config.password
|
||||
|
||||
# 取得本機 IP
|
||||
$ip = (Get-NetIPAddress -AddressFamily IPv4 | Where-Object {
|
||||
$_.IPAddress -notlike "169.*" -and $_.IPAddress -notlike "127.*"
|
||||
}).IPAddress
|
||||
|
||||
function Get-Token {
|
||||
$body = @{
|
||||
branch_id = $branchId
|
||||
room_name = $roomName
|
||||
email = $email
|
||||
password = $password
|
||||
} | ConvertTo-Json -Depth 3
|
||||
|
||||
$response = Invoke-RestMethod -Uri "$apiUrl/api/room/receiveRegister" -Method Post `
|
||||
-Body $body -ContentType "application/json"
|
||||
|
||||
if ($response.token) {
|
||||
Write-Output "✅ Token 取得成功:$($response.token)"
|
||||
return $response.token
|
||||
} else {
|
||||
Write-Error "❌ 無法取得 Token"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
function Send-Heartbeat($token, $ip) {
|
||||
while ($true) {
|
||||
$heartbeat = @{
|
||||
hostname = $env:COMPUTERNAME
|
||||
ip = $ip
|
||||
cpu = [math]::Round((Get-Counter '\Processor(_Total)\% Processor Time').CounterSamples.CookedValue, 2)
|
||||
memory = [math]::Round((Get-Counter '\Memory\Available MBytes').CounterSamples.CookedValue, 2)
|
||||
disk = [math]::Round((Get-PSDrive -Name C).Used / 1MB, 2)
|
||||
status = "online"
|
||||
} | ConvertTo-Json -Depth 3
|
||||
|
||||
try {
|
||||
Invoke-RestMethod -Uri "$apiUrl/api/room/heartbeat" -Method Post `
|
||||
-Headers @{ Authorization = "Bearer $token" } `
|
||||
-Body $heartbeat -ContentType "application/json"
|
||||
|
||||
Write-Output "✅ 心跳送出:$(Get-Date)"
|
||||
} catch {
|
||||
Write-Error "❌ 心跳失敗:$($_.Exception.Message)"
|
||||
}
|
||||
|
||||
Start-Sleep -Seconds 60
|
||||
}
|
||||
}
|
||||
|
||||
# 執行主流程
|
||||
$token = Get-Token
|
||||
|
||||
while ($true) {
|
||||
Send-Heartbeat -token $token -ip $ip
|
||||
Start-Sleep -Seconds 60
|
||||
}
|
12
操作說明.txt
Normal file
12
操作說明.txt
Normal file
@ -0,0 +1,12 @@
|
||||
⚙️ 安裝步驟:
|
||||
1. 準備:將 heartbeat.ps1 與 config.json 放入 scripts 資料夾
|
||||
2. 打包:整個 KtvSetup 資料夾複製到每台設備
|
||||
3. 安裝:右鍵以「系統管理員身份」執行 install.ps1
|
||||
|
||||
⸻
|
||||
|
||||
📌 注意事項:
|
||||
• 安裝後,腳本會每次開機自動執行並回傳心跳。
|
||||
• 若 config.json 中有變更,只需更新該檔案即可。
|
||||
• 若需卸載,只要刪除工作排程即可:
|
||||
Unregister-ScheduledTask -TaskName "KtvHeartbeat" -Confirm:$false
|
Loading…
x
Reference in New Issue
Block a user