UPDATE: I FOUND THE PROBLEM. I had the ExecuteExternalProgram action set to run as NetworkService. I changed that to use a service account we use for Solarwinds and it worked perfectly. Thank you all for the assistance.
# Import the SolarWinds module
Import-Module SwisPowerShell
# Connection string
try {
$swis = Connect-Swis -Hostname localhost -Trusted
} catch {
Write-Error "Failed to connect to SolarWinds SWIS: $_"
exit
}
# Convert Solarwinds Variables to Powershell Variables
$esxHost = ${N=SwisEntity;M=Caption}
# SWQL query to get VMs running on the specified host
$query = @"
SELECT VM.DisplayName AS VM_Name
FROM Orion.VIM.VirtualMachines AS VM
JOIN Orion.VIM.Hosts AS H ON VM.HostID = H.HostID
WHERE H.HostName = '$esxHost'
# Execute the query
try {
$results = Get-SwisData $swis $query
} catch {
Write-Error "Failed to execute query or fetch data: $_"
exit
}
# Convert the query results to JSON format
$jsonResults = $results | ConvertTo-Json -Depth 100
# Create the webhook payload
$payload = [PSCustomObject][Ordered]@{
"@type" = "MessageCard"
"@context" = "http://schema.org/extensions"
"themeColor" = "0076D7"
"title" = "ESX Host $esxHost has gone down!"
"text" = "VMs running on host $esxHost`:`n`n$jsonResults"
} | ConvertTo-Json
$parameters = @{
"URI" = 'https://mydomain.webhook.office.com/webhookb2/XXXXXXXXXXX'
"Method" = 'POST'
"Body" = $payload
"ContentType" = 'application/json'
}
# Force TLS version
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# Send the webhook request
try {
$response = Invoke-RestMethod @parameters
Write-Host "Webhook sent successfully."
} catch {
Write-Error "Failed to send webhook: $_"
}
I am trying to build a custom action that sends a message to MS Teams when a VM ESX Host goes down. The payload includes all the VMs that were running on the Host. The script above works fine when I explicitly define a host (e.g. myhost1.domain.com). HOWEVER, when I try to get the host dynamically using SWIS variables, it fails, and it appears the $esxHost variable is not populated. Am I using the correct SWIS variable and am I referencing it correctly?