The highs and lows.
You’re flying high – you’ve created a stored procedure that you’ve managed to reference in your automation account within a powershell runbook and you’ve got it nicely churning over on an extremely regular basis. You want to run it daily or weekly but as a test you run it hourly just to ensure that it works before releasing it to production. A day passes ok and you think wow this is really great. Then you forget about it and come back to it a week later. You find that somehow it stopped working at 1 am on Tuesday.
Sorry you got hit by a block resulting from dynamic IP change on the runbook in the Automation Account – DAMN.
(there is probably a good reason for having a dynamic IP on automation accounts but damn it was really going too well)
So after quite a lot of investigation I found those in the know seemed to be saying the solution is.
Azure Automation linked to a Hybrid Worker
I kept investigating this and I kept getting directed to Microsoft documentation which although really extensive was just not helping me really get my head around things. It wasn’t until I found Travis Roberts that things started to click… I link to three of his videos below.
So the downsides of Hybrid Worker setup..
Despite being the recommended solution configuration is complicated.
You need to create a VM with a static IP just to run the hybrid worker (and then grant firewall access to the databaseserver for that VM)
That VM needs to have 2 cores and 4Gb of Ram (yes unfortunately not the smallest VM so you could be looking at $25 a month charge)
You need to set up log analytics
It is recommended that log analytics is in the SAME Azure location as the sample database BUT not all Azure locations have log analytics set up… Use UK South! Hands up who has already set up their server and just realised it is in a location that doesn’t allow log analytics.
So I was really struggling with this after my runbook unexpectedly started failing. After several days of reading and searching I found Travis Roberts. Hallelujah for MVPs.
So I haven’t done it yet but I think the procedure can all be defined in here.
1. Setting up an Azure VM with a fixed IP (It can be any VM but Azure is good for me)
Setup VM with static IP
The server you setup will need to have internet connectivity with SSL outbound on port 443
2. Setting up Azure Log Analytics a step by step guide
Setting up Azure Log Analytics a step by step guide
3. Azure Automation Setup and Linking Log Analytics to your Automation account
Set up Azure Automation and link it to Log Analytics
4. Configuring an Azure Automation Hybrid Runbook Worker to run your runbook.
Installation of Hybrid Runbook Worker
Includes information about Runbook Worker Group and
You add credentials to the Runbook Worker Group
Firstly a big thank you to Travis Roberts for putting out those videos he has loads of Azure Centric content on his youtube channel which you can find here
CAVEAT I haven’t implemented this myself properly and at the time of writing(December 2021) those videos are a couple of years old now. At the start of one of the Videos Travis indicates that things are changing quickly in the space and expect that there has been changes since these videos. Lets face it this should really be easier to do Azure is just frustrating their users with this complicated configuration.
Its vital to be able to run stored procedures on a schedule it can be extremely important for doing daily housekeeping.
This site was where I learned how to do this.
How to Schedule an Azure SQL Database’s Stored Procedure
Runbooks sit in Azure Automation and a Runbook consists of a script that performs a task.
Runbooks can be written in Python or Powershell. See the last part of this post for an important additional issue.
"Data Source=01server.database.windows.net;Initial Catalog=database;Integrated Security=False;User ID=01userid;Password=JK12jl423;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
# Instantiate the connection to the SQL Database
$sqlConnection = new-object System.Data.SqlClient.SqlConnection
$sqlConnection.ConnectionString = "Data Source=servername.database.windows.net;Initial Catalog=database;Integrated Security=False;User ID=userid;Password=password;Connect Timeout=60;Encrypt=False;TrustServerCertificate=False"
Write-Output "Azure SQL database connection opened"
# Define the SQL command to run
$sqlCommand = new-object System.Data.SqlClient.SqlCommand
$sqlCommand.CommandTimeout = 120
$sqlCommand.Connection = $sqlConnection
Write-Output "Issuing command to run stored procedure"
# Execute the SQL command
$sqlCommand.CommandText= 'exec [dbo].[storedProcedure]'
$result = $sqlCommand.ExecuteNonQuery()
Write-Output "Stored procedure execution completed"
# Close the SQL connection
Write-Output "Run completed"
Honestly the hardest part is getting the code setting up the scheduling after that is relatively easy.
WARNING : Runbooks have dynamic IPs so although you can set grant access to an IP your runbook will only run for a certain period before it stops working unless you either have a Hybrid Runbook Worker setup OR you allow Azure services and resources to access this server (which isn’t recommended) The next post I will do is my initial notes on how to configure a Hybrid Runbook Worker.
Useful if you have factory products that are being packed into sets of a specfic number. In our case sixes.
Swap out to a different amount if you need to. The last group in the record set will have a remainder level if the recordset is not perfectly divisible by the set integer.
floor(((ROW_NUMBER() OVER(ORDER BY pkid))-1)/6)+1 as grpNum
Here is a screenshot of the above code being run on a table.