PowerShell script exchange
Post Reply
kikanq
Novice
Posts: 3
Liked: never
Joined: Jan 15, 2024 2:47 pm
Full Name: kikanq
Contact:

Powershell webhook webex information

Post by kikanq »

Hello,
I want to receive webhook notifications for webex and I have the following script:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$webhookUrl = "https://webexapis.com/v1/webhooks/incoming/my identificator webhook webex"

$date = (Get-Date).AddMinutes(-2)
$VMsessions = Get-VBRBackupSession
$VMMessages = $VMsessions | Where-Object { $_.CreationTime -ge $date } | ForEach-Object {
$message = @"
**Type:** VM
**JobName:** $($_.JobName)
**CreationTime:** $($_.CreationTime)
**EndTime:** $($_.EndTime)
**Result:** $($_.Result)
**State:** $($_.State)
---
"@
$message
}
$PMMessages = foreach ($PBackup_job in (Get-VBRComputerBackupJob | Select-Object -ExpandProperty Name)) {
$PMSessions = Get-VBRComputerBackupJobSession -Name $PBackup_job | Where-Object { $_.CreationTime -ge $date } | Sort-Object CreationTime
$PMSessions | ForEach-Object {
$message = @"
**Type:** PM
**JobName:** $PBackup_job
**CreationTime:** $($_.CreationTime)
**EndTime:** $($_.EndTime)
**Result:** $($_.Result)
**State:** $($_.State)
---
"@
$message
}
}

$allMessages = $VMMessages + $PMMessages

$fullMessage = @"
**Backup Sessions:**
$($allMessages -join "`n")
"@

$body = @{
markdown = $fullMessage
} | ConvertTo-Json

Invoke-RestMethod -Uri $webhookUrl -Method POST -Body $body -Headers @{
"Content-Type" = "application/json"
}

unfortunately, after adding it to the task, I do not get all the data..
This is what the message looks like when the task is completed:
Backup Sessions:
Type: VM
JobName: TEST prooxy
CreationTime: 01/15/2024 15:49:42
EndTime: 01/01/1900 00:00:00
Result: None
State: Postprocessing
david.domask
Veeam Software
Posts: 1226
Liked: 322 times
Joined: Jun 28, 2016 12:12 pm
Contact:

Re: Powershell webhook webex information

Post by david.domask »

Hi @kikanq,

Can you show the shell output when it's not working correctly? What isn't populated or what is incorrect? The example you provided would indicate the task was still processing in fact -- EndTime == 01/01/1900 is a place-holder time for not yet completed sessions, so that looks pretty normal for a yet to complete session.

Maybe share what is missing when it's not working and an example and add some debugging to your script with Write-Host to print the variable name and the location in the script?
David Domask | Product Management: Principal Analyst
kikanq
Novice
Posts: 3
Liked: never
Joined: Jan 15, 2024 2:47 pm
Full Name: kikanq
Contact:

Re: Powershell webhook webex information

Post by kikanq »

I built the script and now it looks like this ( for VM backup ):

Code: Select all

Start-Sleep -Seconds 20

$webhookUrlVM = "https://webexapis.com/v1/webhooks/incoming/ID webhook webex"

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


$LastVMSession = Get-VBRBackupSession | Sort-Object CreationTime -Descending | Select-Object -First 1
$VMBackupDuration = $LastVMSession.EndTime - $LastVMSession.CreationTime
$VMBackupDurationFormatted = '{0:D2}:{1:D2}:{2:D2}' -f $VMBackupDuration.Hours, $VMBackupDuration.Minutes, $VMBackupDuration.Seconds


$jsonDataVM = @{
    "markdown" = @"
**Backup Sessions:**

- **Type:** VM
- **Job Name:** $($LastVMSession.JobName)
- **CreationTime:** $($LastVMSession.CreationTime)
- **EndTime:** $($LastVMSession.EndTime)
- **Duration:** $VMBackupDurationFormatted
- **Result:** $($LastVMSession.Result)
- **State:** $($LastVMSession.State)
"@
} | ConvertTo-Json


Invoke-RestMethod -Uri $webhookUrlVM -Method Post -Body $jsonDataVM -ContentType "application/json"
In the backup task for the field, another script is attached that triggers the first one:

Code: Select all

Start-Process powershell.exe -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File .\first_powershell.ps1" -WindowStyle Hidden
For this solution everything works fine.
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests