<## AWX/Tower settings
$jobTemplateName = "HealthCheckWinRMPing"
$apiUsername = "APITEST"
$apiPassword = ""
$clientId = ""
$clientSecret = ""
# Email settings
$smtpServer = "SERVER"
$smtpPort = 25
$smtpUsername = ""
$smtpPassword = ""
#>
[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
# Authenticate and obtain access token
$authUrl = "$awxUrl/api/o/token/"
$authBody = @{
"grant_type" = "password"
"username" = $apiUsername
"password" = $apiPassword
}
$authHeaders = @{
"Content-Type" = "application/x-www-form-urlencoded"
"Authorization" = "Basic " + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("${clientId}:${clientSecret}"))
}
#Authentication Headers
Write-Output "Authentication Headers"
$authHeaders | Format-List | Out-String | Write-Output
write-output "Auth URL: $authUrl"
$authResponse = Invoke-RestMethod -Method Post -Uri $authUrl -Headers $authHeaders -Body $authBody
$accessToken = $authResponse.access_token
# Get the Job Template
$jobTemplateListUrl = "$awxUrl/api/v2/job_templates/?name=$([System.Web.HttpUtility]::UrlEncode($jobTemplateName))"
Write-verbose "Job Template Name: $jobTemplateName"
write-output "Template URL: $jobTemplateListUrl"
$jobTemplateListHeaders = @{
"Authorization" = "Bearer $accessToken"
"Content-Type" = "application/json"
}
$jobTemplateListResponse = Invoke-RestMethod -Method GET -Uri $jobTemplateListUrl -Headers $jobTemplateListHeaders
$jobTemplateListResponse.results[0].id
# Print job template response for debugging
$jobTemplateListResponse | Format-List | Out-String | Write-Output
# Check if last_job is not null before accessing its ID
if($null -ne $jobTemplateListResponse.results[0].summary_fields.last_job) {
} else {
Write-Output "No last_job found in job template response."
exit
}
# Get the job events for the last job
$jobEventsUrl = "$awxUrl/api/v2/jobs/$lastJobId/job_events/"
$jobEventsHeaders = @{
"Authorization" = "Bearer $accessToken"
"Content-Type" = "application/json"
}
$jobEventsResponse = Invoke-RestMethod -Method GET -Uri $jobEventsUrl -Headers $jobEventsHeaders
# Filter the events for failed or unreachable hosts and format them for the email body
$emailBody = ""
$failedEvents = $jobEventsResponse.results | Where-Object { $_.failed -eq $true -or $_.event -eq 'runner_unreachable' }
foreach ($event in $failedEvents) {
$hostName = $event.host_name
$errorMessage = $event.event_data.res.msg
$emailBody += "${hostName}: $errorMessage`n"
}
# Format the email subject
$emailSubject = "$jobTemplateName - Job # $lastJobId - $($jobTemplateListResponse.results[0].created)"
# Send the email
$mailMessage = New-Object System.Net.Mail.MailMessage
$mailMessage.From = $emailFrom
$mailMessage.To.Add($emailTo)
$mailMessage.Subject = $emailSubject
$mailMessage.Body = $emailBody
# SMTP settings
$smtpClient = New-Object System.Net.Mail.SmtpClient($smtpServer, $smtpPort)
$smtpClient.EnableSsl = $false
#$smtpClient.Credentials = New-Object System.Net.NetworkCredential($smtpUsername, $smtpPassword)
# Send the email
try {
$smtpClient.Send($mailMessage)
} catch {
Write-Error "Failed to send email: $_"
}