Last active
September 26, 2023 20:02
-
-
Save HiteaFR/47b7d8b8dc5445332cadb0707aba5de9 to your computer and use it in GitHub Desktop.
WSB rapport sur Microsoft Teams (Webhook)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$jobname = "Windows_Backup_Teams" | |
$action = New-ScheduledTaskAction –Execute "@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File C:\Windows_Backup_Teams_Report.ps1" | |
$trigger = New-ScheduledTaskTrigger -daily -At 7am | |
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -DontStopOnIdleEnd | |
Register-ScheduledTask -TaskName $jobname -Action $action -Trigger $trigger -RunLevel Highest -Settings $settings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ServerList = (Join-Path $PSScriptRoot "BackupsSrv.txt") | |
$webhook = 'Teams WebHook' | |
$client = "Client Name" | |
#Check if server list exists | |
If (!(Test-Path $ServerList)) { | |
Write-Host "Can not get servers list. Script will not continue" -ForegroundColor Red; Exit | |
} | |
$servers = @() | |
$job_details = "" | |
Get-Content $ServerList | Foreach-Object { $servers += $_ } | |
$results = (1..$servers.length) | |
for ($i = 0; $i -lt $servers.length; $i++) { | |
$ConnectionError = 0 | |
Write-Host "Getting result from server: " $servers[$i] | |
try { | |
$Session = New-PSSession -ComputerName $servers[$i] | |
$WindowsVersion = Invoke-Command -session $session -ScriptBlock { (Get-WmiObject win32_operatingsystem).version } | |
if ($WindowsVersion -match "6.1") | |
{ $WBSummary = Invoke-Command -session $session -ScriptBlock { add-pssnapin windows.serverbackup; Get-WBSummary } } | |
else { $WBSummary = Invoke-Command -session $session -ScriptBlock { Get-WBSummary } } | |
Remove-PSSession $Session | |
} | |
catch { | |
Write-Host "Error connecting remote server" | |
write-host "Caught an exception:" -ForegroundColor Red | |
write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red | |
write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red | |
$ConnectionError = 1 | |
} | |
$job_details += "## Serveur : " + $servers[$i] + "`n" | |
if ($ConnectionError -eq 1) { | |
$job_details += "- **Statut** : Error connecting remote server`n" | |
} | |
else { | |
if ($WBSummary.LastBackupResultHR -eq 0) { $job_details += "- **Statut** : Success`n"; $result = "Success" } | |
else { $job_details += "- **Statut** : Failure`n"; $result = "Failure" } | |
$job_details += "- **Date** : " + $WBSummary.LastSuccessfulBackupTime + "`n" | |
if ([string]::IsNullOrEmpty($WBSummary.DetailedMessage)) { $job_details += "- **Message** : Success`n"; $message = "Success" } | |
else { $job_details += "- **Message** : " + $WBSummary.DetailedMessage + "`n"; $message = $WBSummary.DetailedMessage } | |
$job_details += "- **Nombre de sauvegardes** : " + $WBSummary.NumberOfVersions + "`n" | |
if ([string]::IsNullOrEmpty($WBSummary.LastBackupTarget)) { $job_details += "- **Destination** : None`n" } | |
else { $job_details += "- **Destination** : " + $WBSummary.LastBackupTarget + "`n" } | |
Write-Host "Last Backup Result: $result" | |
Write-Host "Last Successful Backup Time:" $WBSummary.LastSuccessfulBackupTime | |
Write-Host "Detailed Message: $message" | |
Write-Host "Number of Backups:" $WBSummary.NumberOfVersions | |
Write-Host "-----------------------------------------------------------------" | |
} | |
} | |
$body = ConvertTo-Json -depth 3 @{ | |
summary = 'Rapport de sauvegarde' | |
themeColor = '0055DD' | |
sections = @( | |
@{ | |
activityTitle = "Sauvegarde du client: " + $client + "`n" | |
activityText = "$job_details" | |
} | |
) | |
} | |
Invoke-RestMethod -Method post -ContentType 'Application/Json' -Body $body -Uri $webhook |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment