Skip to content

Instantly share code, notes, and snippets.

@anhnvme
Last active December 14, 2024 08:57
Show Gist options
  • Save anhnvme/2c3dc16b38fccab3b43fa37f67f2f5e3 to your computer and use it in GitHub Desktop.
Save anhnvme/2c3dc16b38fccab3b43fa37f67f2f5e3 to your computer and use it in GitHub Desktop.
veeambackup_notify.ps1
# Định nghĩa thông tin Telegram Bot và Chat ID
$botToken = "724315259:AAE5uwl-jtqSR620E-cjJ3I9kgDBdZqhg3M" # Thay thế bằng token của bot bạn
$chatId = "-1002178510123" # Thay thế bằng chat ID của bạn (hoặc nhóm)
# Hàm gửi tin nhắn đến Telegram
function Send-TelegramMessage {
param (
[string]$message
)
$url = "https://api.telegram.org/bot$botToken/sendMessage"
$body = @{
chat_id = $chatId
text = $message
parse_mode = "HTML" # Có thể sử dụng "Markdown" nếu muốn
}
Invoke-RestMethod -Uri $url -Method Post -Body $body
}
# Lấy tất cả các session backup trong vòng 24 giờ qua
$startTime = (Get-Date).AddHours(-12)
$vmlist = Get-VBRBackupSession | Where-Object { $_.EndTime -ge $startTime }
$agentlist = Get-VBRComputerBackupJobSession | Where-Object { $_.EndTime -ge $startTime }
# Kiểm tra và gửi thông báo cho mỗi session
foreach ($vm in $vmlist) {
$vmName = $vm.JobName
$vmstatus = if ($vm.Result -eq 'Success') { "✅" } else { "❌" }
$vmstartTime = $vm.CreationTime
$vmendTime = $vm.EndTime
$vmduration = [DateTime]::Parse($vmendTime) - [DateTime]::Parse($vmstartTime)
# Chuẩn bị thông báo
$message_vm = "<b>Veeam Backup hoàn tất:</b>`n`n"
$message_vm += "<b>Job Name:</b> $vmName`n"
$message_vm += "<b>Bắt đầu:</b> $vmstartTime`n"
$message_vm += "<b>Kết thúc:</b> $vmendTime`n"
$message_vm += "<b>Thời lượng:</b> $vmduration`n"
$message_vm += "<b>Trạng thái:</b> $vmstatus`n"
# Gửi thông báo đến Telegram
Send-TelegramMessage -message $message_vm
}
foreach ($agent in $agentlist) {
$agsessionName = $agent.JobName
$agstatus = if ($agent.Result -eq 'Success') { "✅" } else { "❌" }
$agstartTime = $agent.CreationTime
$agendTime = $agent.EndTime
$agduration = [DateTime]::Parse($agendTime) - [DateTime]::Parse($agstartTime)
# Chuẩn bị thông báo
$message_ag = "<b>Veeam Backup hoàn tất:</b>`n`n"
$message_ag += "<b>Job Name:</b> $agsessionName`n"
$message_ag += "<b>Bắt đầu:</b> $agstartTime`n"
$message_ag += "<b>Kết thúc:</b> $agendTime`n"
$message_ag += "<b>Thời lượng:</b> $agduration`n"
$message_ag += "<b>Trạng thái:</b> $agstatus`n"
# Gửi thông báo đến Telegram
Send-TelegramMessage -message $message_ag
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment