En este tutorial aprenderemos cómo monitorear el estado de un flujo en Power Automate utilizando PowerShell. Este proceso es útil si necesitas realizar un seguimiento de la ejecución del flujo y recibir alertas cuando ocurran ciertas condiciones.
Conectar con SharePoint Online
Para comenzar, es necesario que te conectes a SharePoint Online utilizando la identidad administrada por el sistema. Utiliza el siguiente comando en PowerShell para establecer la conexión:
Connect-PnPOnline -url zerogap.sharepoint.com -ManagedIdentity
Definir los flujos a monitorear
A continuación, define los flujos de Power Automate que deseas monitorear. Para hacerlo, crea un diccionario con el nombre del flujo y su ID correspondiente.
$flows_to_monitor = @{Audit_Log_Handler = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
Establecer el objetivo del webhook de alerta
Establece la variable que contiene el webhook al que se enviarán las alertas. Este webhook puede ser una URL de un servicio como Teams, Slack, etc.
$webhook = Get-AutomationVariable -Name "ExampleAlertWebhookVariable"
Obtener las últimas ejecuciones
Para cada flujo, obtén las últimas ejecuciones utilizando el comando get-pnpflowrun
.
$flow_id_string=[String]$flow.Values
write-output $flow_id_string
$monitor=get-pnpflowrun -Environment "yyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" -Flow $flow_id_string | select -First 10
Contar y alertar si hay demasiados trabajos en ejecución
Revisa cada trabajo y, si el estado es «Running», incrementa el contador de alertas.
foreach ($job in $monitor) {
if ($job.Properties.Status -eq "Running") {
$counter_alert++
}
}
Si hay más de 5 trabajos ejecutándose al mismo tiempo, se dispara una alerta.
write-output $counter_alert
if ($counter_alert -gt 5 ) {
Tomar acción en caso de alerta
Define qué hacer en caso de que se cumpla la condición de alerta. Por ejemplo, enviar una notificación a través del webhook con detalles sobre el flujo y el trabajo más reciente.
$job_id=$monitor[0].id
$flow_name=[String]$flow.keys
$payload = @{"text" = "Flow Alert: at least 5 jobs are still running in the $flow_name flow. The last job execution is $job_id"}
Invoke-RestMethod -uri $webhook -method Post -Body (convertto-json -InputObject $payload)
}
}
Con este script de PowerShell, puedes mantener un monitoreo constante sobre tus flujos en Power Automate y estar alerta ante cualquier comportamiento inusual que pueda requerir tu atención.
Ejemplo completo
# Connect using the system managed Identity
Connect-PnPOnline -url zerogap.sharepoint.com -ManagedIdentity
#### Flows to monitor ####
$flows_to_monitor = @{Audit_Log_Handler = "xxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}
$counter_alert = 0
## Alert Webhook Target ##
$webhook = Get-AutomationVariable -Name "ExampleAlertWebhookVariable"
# Check last 10 jobs for all targeted flows
foreach ($flow in $flows_to_monitor) {
# Get the last 10 runs
$flow_id_string=[String]$flow.Values
write-output $flow_id_string
$monitor=get-pnpflowrun -Environment "yyyyyy-yyyy-yyyy-yyyy-yyyyyyyyyyyy" -Flow $flow_id_string | select -First 10
foreach ($job in $monitor) {
if ($job.Properties.Status -eq "Running") {
$counter_alert++
}
}
# If there are more than 5 jobs running at the same time. Alert.
write-output $counter_alert
if ($counter_alert -gt 5 ) {
# Do something
$job_id=$monitor[0].id
$flow_name=[String]$flow.keys
$payload = @{"text" = "Flow Alert: at least 5 jobs are still running in the $flow_name flow. The last job execution is $job_id"}
Invoke-RestMethod -uri $webhook -method Post -Body (convertto-json -InputObject $payload)
}
}
- ChromaDB + Autenticación y Colecciones - mayo 17, 2024
- PowerShell + REST APIs para monitorear el estado de MS Flows - mayo 17, 2024
- IA Agent - mayo 11, 2024