Automate and Send Alerts for Elastic Jobs Failures

Published Dec 12 2020 05:49 AM 19.5K Views
Microsoft

 

Alerts can be sent for any particular state of an elastic job. In this article, we discuss the case of 'Failed' jobs, however the solution can be easily extended to other scenarios. The script can be executed on client machines in background on a schedule or in Azure Automation Runbook. If failed jobs are identified, email notifications are sent, with an attachment of a log file containing only the failed jobs. 

 

Elastic Jobs are currently in public preview and the monitoring capabilities are resuming for now to ad-hoc executions of T-SQL / PowerShell commands to retrieve the elastic job execution status, with no embedded option for sending alerts in case of a job failure. 

 

This need has been addressed through a PowerShell script that filters job executions based on their status and sends the output to a log file, that is further sent as an attachment in a notification email and in the same time the log file is stored in an Azure Storage Account for further reference. Storing the output and the ability to archive run results in a storage account is an important feature, since the job history is limited to 45 days in our service. 

 

script output when executed on client machinescript output when executed on client machine

 

The execution results are stored in the Job database, defined at the job creation time. The jobs are filtered with the below T-SQL, that can be modified as per the requirements:

 

SELECT * FROM jobs.job_executions WHERE lifecycle = 'Failed' ORDER BY start_time DESC

 

 

Other possible values for job execution states are CreatedInProgressWaitingForRetry, Succeeded, SucceededWithSkipped, Failed, TimedOut, Canceled, Skipped and WaitingForChildJobExecutions.

 

Generated log file:

log file snippetlog file snippet

 

Save log file to storage account:

 

# Get key to storage account
$acctKey = (Get-AzStorageAccountKey -Name generals -ResourceGroupName general).Value[0]

# Map to the reports BLOB context
$storageContext = New-AzStorageContext -StorageAccountName $StorageAccountName -StorageAccountKey $acctKey

# Copy the file to the storage account
Set-AzStorageBlobContent -File $output_file -Container StorageContainerName -BlobType "Block" -Context $storageContext -Verbose

 

 

save log to storage accountsave log to storage account

 

The email functionality can be leveraged through any Simple Mail Transfer Protocol server. The proposed script is using smtp.mail.yahoo.com on port 587

 

email notification alertemail notification alert

 

For sending email alerts, an alternative to SMTP is to use a SendGrid Account, as per the documentation: Send an email from an Azure Automation runbook | Microsoft Docs.

 

How to run and automate alerts

 

As elastic jobs can be scheduled to run on a regular basis from both PowerShell and T-SQL, so our alert script can. Depending on the desired deployment type you choose, there are several options. For any of the below options, you have to set the required parameters in the script: Insert SQL Server Details, Credentials & SMTP server details:

 

$server = '.database.windows.net' # Set the name of the server
$jobDB = ''   # Set the name of the job database you wish to test
$user = '' # Set the login username you wish to use
$passw = '' # Set the login password you wish to use
$FromEmail = 'xxxxxxxxxxxx@yahoo.com' # "from" field's account must match the smtp server domain 
$FromEmailPassw = '' # use app/server access token - it works with account passw
$ToEmail = 'xxxxxxxxxxxx@yyyyy.com'
$SMTPServer = "smtp.mail.yahoo.com" #insert SMTP server
$SMTPPort = "587" # or port 465
$StorageAccountName = ''
$StorageContainerName = ''

 

 

 

OPTION#1 

Run the script on schedule in background on client machine

 

In order to run it you need to:

  • Open Windows PowerShell ISE in Administrator mode

  • Open a New Script window

  • Paste the content in script window

  • Run it

  • If failed jobs are found, an alert email will be triggered and the log file containing details on the failed jobs will be attached to the email and either saved locally or sent to a storage account. The result of the script can be followed in the output window.

  • To run the script in background on a schedule, you can use the following commands:

 

##  =========================================================================
##                Schedule Commands for Client Machines 
##  =========================================================================
## To schedule script execution in background, please see below options

#insert parameter values
$script = 'script.ps1' # insert script path
$Time= 'MM/DD/YYYY HH:MM' # insert desired start time for schedule
$jobName = 'Job1'#insert desired job name


# display all scheduled jobs
Get-ScheduledJob

# add new job to run at schedule
Register-ScheduledJob -Name $jobName -FilePath $script -Trigger (New-JobTrigger -Once -At $Time `
    -RepetitionInterval (New-TimeSpan -Minutes 1) -RepetitionDuration ([TimeSpan]::MaxValue))

# command to remove a scheduled job
Unregister-ScheduledJob $jobName

 

 

 

OPTION#2

Run the script from Azure Runbook

 

  • Create a new Automation Account as described here and make sure you choose "YES" for option "Create Azure Run As Account".
  • Import the following Azure Modules by browsing the gallery: 
            Az.Accounts (≥ 2.2.2)
            Az.Storage
            Az.Automation

             browse gallery - add modulebrowse gallery - add module

 

  • Create a runbook to run the script and make sure you choose Powershell runbook type.

           add a runbookadd a runbook

 

  • Add the following login section when connecting from a Runbook 

 

##  =========================================================================
##             Login when running from a Runbook
##  =========================================================================
## Enable this code section when running from Azure Runbook
## Get the connection "AzureRunAsConnection" when run from automation account

$connection = Get-AutomationConnection -Name AzureRunAsConnection
Connect-AzAccount `
    -ServicePrincipal `
    -Tenant $connection.TenantID `
    -ApplicationId $connection.ApplicationID `
    -CertificateThumbprint $connection.CertificateThumbprint

"Login successful..."

 

 

 

  • Schedule task

         schedule taskschedule task

 

Note: Script execution can be monitored in the portal and alerts can be set for any script execution failure.

 

job execution monitorjob execution monitor

 

The script can be found in the following Git repository. This project welcomes contributions and suggestions.

 

 

%3CLINGO-SUB%20id%3D%22lingo-sub-1981457%22%20slang%3D%22en-US%22%3EAutomate%20and%20Send%20Alerts%20for%20Elastic%20Jobs%20Failures%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1981457%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAlerts%20can%20be%20sent%20for%20any%20particular%20state%20of%20an%20elastic%20job.%20In%20this%20article%2C%20we%20discuss%20the%20case%20of%20'Failed'%20jobs%2C%20however%20the%20solution%20can%20be%20easily%20extended%20to%20other%20scenarios.%20The%20script%20can%20be%20executed%20on%20client%20machines%20in%20background%20on%20a%20schedule%20or%20in%20Azure%20Automation%20Runbook.%20If%20failed%20jobs%20are%20identified%2C%20email%20notifications%20are%20sent%2C%20with%20an%20attachment%20of%20a%20log%20file%20containing%20only%20the%20failed%20jobs.%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EElastic%20Jobs%20are%20currently%20in%20public%20preview%20and%20the%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Felastic-jobs-powershell-create%23monitor-status-of-job-executions%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Emonitoring%20capabilities%3C%2FA%3E%20are%20resuming%20for%20now%20to%20ad-hoc%20executions%20of%20T-SQL%20%2F%20PowerShell%20commands%20to%20retrieve%20the%20elastic%20job%20execution%20status%2C%20with%20no%20embedded%20option%20for%20sending%20alerts%20in%20case%20of%20a%20job%20failure.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThis%20need%20has%20been%20addressed%20through%20a%20PowerShell%20script%20that%20filters%20job%20executions%20based%20on%20their%20status%20and%20sends%20the%20output%20to%20a%20log%20file%2C%20that%20is%20further%20sent%20as%20an%20attachment%20in%20a%20notification%20email%20and%20in%20the%20same%20time%20the%20log%20file%20is%20stored%20in%20an%20Azure%20Storage%20Account%20for%20further%20reference.%20Storing%20the%20output%20and%20the%20ability%20to%20archive%20run%20results%20in%20a%20storage%20account%20is%20an%20important%20feature%2C%20since%20the%20job%20history%20is%20limited%20to%26nbsp%3B45%20days%20in%20our%20service.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capture2.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239961iA3429FDF87625364%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capture2.PNG%22%20alt%3D%22script%20output%20when%20executed%20on%20client%20machine%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Escript%20output%20when%20executed%20on%20client%20machine%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20execution%20results%20are%20stored%20in%20the%26nbsp%3BJob%20database%2C%20defined%20at%20the%20job%20creation%20time.%26nbsp%3B%3C%2FSPAN%3EThe%20jobs%20are%20filtered%20with%20the%20below%20T-SQL%2C%20that%20can%20be%20modified%20as%20per%20the%20requirements%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-sql%22%3E%3CCODE%3ESELECT%20*%20FROM%20jobs.job_executions%20WHERE%20lifecycle%20%3D%20'Failed'%20ORDER%20BY%20start_time%20DESC%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOther%20possible%20values%20for%20job%20execution%20states%20are%26nbsp%3B%3CFONT%20color%3D%22%230000FF%22%3ECreated%3C%2FFONT%3E%2C%26nbsp%3B%3CFONT%20color%3D%22%230000FF%22%3EInProgress%3C%2FFONT%3E%2C%26nbsp%3B%3CFONT%20color%3D%22%230000FF%22%3EWaitingForRetry%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3ESucceeded%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3ESucceededWithSkipped%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3EFailed%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3ETimedOut%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3ECanceled%3C%2FFONT%3E%2C%20%3CFONT%20color%3D%22%230000FF%22%3ESkipped%20%3C%2FFONT%3Eand%20%3CFONT%20color%3D%22%230000FF%22%3EWaitingForChildJobExecutions%3C%2FFONT%3E.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EGenerated%20log%20file%3A%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capture1.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239956iD26DF605084D8597%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capture1.PNG%22%20alt%3D%22log%20file%20snippet%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Elog%20file%20snippet%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ESave%20log%20file%20to%20storage%20account%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20Get%20key%20to%20storage%20account%0A%24acctKey%20%3D%20(Get-AzStorageAccountKey%20-Name%20generals%20-ResourceGroupName%20general).Value%5B0%5D%0A%0A%23%20Map%20to%20the%20reports%20BLOB%20context%0A%24storageContext%20%3D%20New-AzStorageContext%20-StorageAccountName%20%24StorageAccountName%20-StorageAccountKey%20%24acctKey%0A%0A%23%20Copy%20the%20file%20to%20the%20storage%20account%0ASet-AzStorageBlobContent%20-File%20%24output_file%20-Container%20StorageContainerName%20-BlobType%20%22Block%22%20-Context%20%24storageContext%20-Verbose%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capturasase.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239979iA78EF7F4FFD6B4C8%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capturasase.PNG%22%20alt%3D%22save%20log%20to%20storage%20account%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Esave%20log%20to%20storage%20account%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThe%20email%20functionality%20can%20be%20leveraged%20through%20any%20Simple%20Mail%20Transfer%20Protocol%20server.%20The%20proposed%20script%20is%20using%26nbsp%3B%3CSPAN%3E%3CSTRONG%3Esmtp.mail.yahoo.com%3C%2FSTRONG%3E%20on%20port%20%3CSTRONG%3E587%3C%2FSTRONG%3E.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capture.PNG%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239955i1AE420AEA59497A6%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Capture.PNG%22%20alt%3D%22email%20notification%20alert%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Eemail%20notification%20alert%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EFor%20sending%20email%20alerts%2C%20an%20alternative%20to%20SMTP%20is%20to%20use%20a%20SendGrid%20Account%2C%20as%20per%20the%20documentation%3A%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-send-email%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3ESend%20an%20email%20from%20an%20Azure%20Automation%20runbook%20%7C%20Microsoft%20Docs.%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH2%20id%3D%22toc-hId--1180009353%22%20id%3D%22toc-hId--1180009353%22%3EHow%20to%20run%20and%20automate%20alerts%3C%2FH2%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAs%20elastic%20jobs%20can%20be%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fazure-sql%2Fdatabase%2Felastic-jobs-powershell-create%23run-the-job%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Escheduled%3C%2FA%3E%20to%20run%20on%20a%20regular%20basis%20from%20both%20PowerShell%20and%20T-SQL%2C%20so%20our%20alert%20script%20can.%20Depending%20on%20the%20desired%20deployment%20type%20you%20choose%2C%20there%20are%20several%20options.%26nbsp%3B%3CSPAN%3EFor%20any%20of%20the%20below%20options%2C%20you%20have%20to%20set%20the%20required%20parameters%20in%20the%20script%3A%20Insert%20SQL%20Server%20Details%2C%20Credentials%20%26amp%3B%20SMTP%20server%20details%3A%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24server%20%3D%20'.database.windows.net'%20%23%20Set%20the%20name%20of%20the%20server%0A%24jobDB%20%3D%20''%20%20%20%23%20Set%20the%20name%20of%20the%20job%20database%20you%20wish%20to%20test%0A%24user%20%3D%20''%20%23%20Set%20the%20login%20username%20you%20wish%20to%20use%0A%24passw%20%3D%20''%20%23%20Set%20the%20login%20password%20you%20wish%20to%20use%0A%24FromEmail%20%3D%20'xxxxxxxxxxxx%40yahoo.com'%20%23%20%22from%22%20field's%20account%20must%20match%20the%20smtp%20server%20domain%20%0A%24FromEmailPassw%20%3D%20''%20%23%20use%20app%2Fserver%20access%20token%20-%20it%20works%20with%20account%20passw%0A%24ToEmail%20%3D%20'xxxxxxxxxxxx%40yyyyy.com'%0A%24SMTPServer%20%3D%20%22smtp.mail.yahoo.com%22%20%23insert%20SMTP%20server%0A%24SMTPPort%20%3D%20%22587%22%20%23%20or%20port%20465%0A%24StorageAccountName%20%3D%20''%0A%24StorageContainerName%20%3D%20''%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-2008568058%22%20id%3D%22toc-hId-2008568058%22%3EOPTION%231%26nbsp%3B%3C%2FH4%3E%0A%3CP%3ERun%20the%20script%20on%20schedule%20in%20background%20on%20client%20machine%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20order%20to%20run%20it%20you%20need%20to%3A%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3E%3CP%3EOpen%20Windows%20PowerShell%20ISE%20in%20Administrator%20mode%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EOpen%20a%20New%20Script%20window%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EPaste%20the%20content%20in%20script%20window%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3ERun%20it%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3EIf%20failed%20jobs%20are%20found%2C%20an%20alert%20email%20will%20be%20triggered%20and%20the%20log%20file%20containing%20details%20on%20the%20failed%20jobs%20will%20be%20attached%20to%20the%20email%20and%20either%20saved%20locally%20or%20sent%20to%20a%20storage%20account.%20The%20result%20of%20the%20script%20can%20be%20followed%20in%20the%20output%20window.%3C%2FP%3E%0A%3C%2FLI%3E%0A%3CLI%3E%3CP%3ETo%20run%20the%20script%20in%20background%20on%20a%20schedule%2C%20you%20can%20use%20the%20following%20commands%3A%3C%2FP%3E%0A%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%23%20%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%23%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20Schedule%20Commands%20for%20Client%20Machines%20%0A%23%23%20%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%23%20To%20schedule%20script%20execution%20in%20background%2C%20please%20see%20below%20options%0A%0A%23insert%20parameter%20values%0A%24script%20%3D%20'script.ps1'%20%23%20insert%20script%20path%0A%24Time%3D%20'MM%2FDD%2FYYYY%20HH%3AMM'%20%23%20insert%20desired%20start%20time%20for%20schedule%0A%24jobName%20%3D%20'Job1'%23insert%20desired%20job%20name%0A%0A%0A%23%20display%20all%20scheduled%20jobs%0AGet-ScheduledJob%0A%0A%23%20add%20new%20job%20to%20run%20at%20schedule%0ARegister-ScheduledJob%20-Name%20%24jobName%20-FilePath%20%24script%20-Trigger%20(New-JobTrigger%20-Once%20-At%20%24Time%20%60%0A%20%20%20%20-RepetitionInterval%20(New-TimeSpan%20-Minutes%201)%20-RepetitionDuration%20(%5BTimeSpan%5D%3A%3AMaxValue))%0A%0A%23%20command%20to%20remove%20a%20scheduled%20job%0AUnregister-ScheduledJob%20%24jobName%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CH4%20id%3D%22toc-hId-201113595%22%20id%3D%22toc-hId-201113595%22%3EOPTION%232%3C%2FH4%3E%0A%3CP%3ERun%20the%20script%20from%20Azure%20Runbook%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20a%20new%20Automation%20Account%20as%20described%20%3CA%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fautomation%2Fautomation-create-standalone-account%23create-a-new-automation-account-in-the-azure-portal%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3Ehere%3C%2FA%3E%26nbsp%3Band%20make%20sure%20you%20choose%20%22YES%22%20for%20option%20%22Create%20Azure%20Run%20As%20Account%22.%3CSTRONG%3E%3CBR%20%2F%3E%3C%2FSTRONG%3E%3C%2FLI%3E%0A%3CLI%3EImport%20the%20following%20Azure%20Modules%20by%20browsing%20the%20gallery%3A%26nbsp%3B%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Az.Accounts%20(%E2%89%A5%202.2.2)%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Az.Storage%3CBR%20%2F%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Az.Automation%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Capturex.PNG%22%20style%3D%22width%3A%20658px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239976i40F3D49C062D9ED8%2Fimage-dimensions%2F658x654%3Fv%3D1.0%22%20width%3D%22658%22%20height%3D%22654%22%20role%3D%22button%22%20title%3D%22Capturex.PNG%22%20alt%3D%22browse%20gallery%20-%20add%20module%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ebrowse%20gallery%20-%20add%20module%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ECreate%20a%20runbook%20to%20run%20the%20script%20and%20make%20sure%20you%20choose%20Powershell%20runbook%20type.%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22saaCapture.PNG%22%20style%3D%22width%3A%20561px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239977iDE8ADE28F46987F1%2Fimage-dimensions%2F561x492%3Fv%3D1.0%22%20width%3D%22561%22%20height%3D%22492%22%20role%3D%22button%22%20title%3D%22saaCapture.PNG%22%20alt%3D%22add%20a%20runbook%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Eadd%20a%20runbook%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3EAdd%20the%20following%20login%20section%20when%20connecting%20from%20a%20Runbook%26nbsp%3B%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%23%20%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%23%20%20%20%20%20%20%20%20%20%20%20%20%20Login%20when%20running%20from%20a%20Runbook%0A%23%23%20%20%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%3D%0A%23%23%20Enable%20this%20code%20section%20when%20running%20from%20Azure%20Runbook%0A%23%23%20Get%20the%20connection%20%22AzureRunAsConnection%22%20when%20run%20from%20automation%20account%0A%0A%24connection%20%3D%20Get-AutomationConnection%20-Name%20AzureRunAsConnection%0AConnect-AzAccount%20%60%0A%20%20%20%20-ServicePrincipal%20%60%0A%20%20%20%20-Tenant%20%24connection.TenantID%20%60%0A%20%20%20%20-ApplicationId%20%24connection.ApplicationID%20%60%0A%20%20%20%20-CertificateThumbprint%20%24connection.CertificateThumbprint%0A%0A%22Login%20successful...%22%0A%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CUL%3E%0A%3CLI%3ESchedule%20task%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22sch.PNG%22%20style%3D%22width%3A%20581px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239978iD692A83F978467EE%2Fimage-dimensions%2F581x339%3Fv%3D1.0%22%20width%3D%22581%22%20height%3D%22339%22%20role%3D%22button%22%20title%3D%22sch.PNG%22%20alt%3D%22schedule%20task%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Eschedule%20task%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENote%3A%3C%2FSTRONG%3E%20Script%20execution%20can%20be%20monitored%20in%20the%20portal%20and%20alerts%20can%20be%20set%20for%20any%20script%20execution%20failure.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22non.PNG%22%20style%3D%22width%3A%20623px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F239980iCD0589FF41218739%2Fimage-dimensions%2F623x336%3Fv%3D1.0%22%20width%3D%22623%22%20height%3D%22336%22%20role%3D%22button%22%20title%3D%22non.PNG%22%20alt%3D%22job%20execution%20monitor%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3Ejob%20execution%20monitor%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20script%20can%20be%20found%20in%20the%20following%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Fgithub.com%2FMelaniaNitu%2FAzureSQL%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%22%3EGit%3C%2FA%3E%20repository.%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3EThis%20project%20welcomes%20contributions%20and%20suggestions.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-1981457%22%20slang%3D%22en-US%22%3E%3CP%3EAlerts%20can%20be%20sent%20for%20any%20particular%20state%20of%20an%20elastic%20job.%20In%20this%20article%2C%20we%20discuss%20the%20case%20of%20'Failed'%20jobs%2C%20however%20the%20solution%20can%20be%20easily%20extended%20to%20other%20scenarios.%20The%20script%20can%20be%20executed%20on%20client%20machines%20in%20background%20on%20a%20schedule%20or%20in%20Azure%20Automation%20Runbook.%20If%20failed%20jobs%20are%20identified%2C%20email%20notifications%20are%20sent%2C%20with%20an%20attachment%20of%20a%20log%20file%20containing%20only%20the%20failed%20jobs.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-TEASER%3E
Version history
Last update:
‎Dec 16 2020 02:22 AM
Updated by: