בניגוד ל-VM שיש להם – כמו לכל שרת – agent, ג’ובים, וכו’; ב-SQL Databases (או Paas) זה קצת יותר מורכב.
כרגע, Azure מציע אוסף של כלים, פתרונות וטכנולוגיות; שלא תמיד מסונכרנים בינהם, ולכן אין פתרון אחד, ומה שיש כיום זה לא מה שיהיה מחר.
בכל מקרה: פתרון אפשרי, נכון לעכשיו.
Automation Accounts – נכנסים לזה מהתפריט הראשי בפורטל. אם יש כבר חשבונות – ניתן להשתמש באחד מהם, ואם לא – יוצרים חדש.
Credentials – יש ליצור credential מתאים לדטבייס איתו רוצים לעבוד (אופצייה מתחת ל-Automation Account): שם משתמש, וסיסמה. בקוד עצמו, בהמשך, נעשה שימוש ב-credential, ולא נציין באופן גלוי שם וסיסמה.
Runbook – זה הקוד להרצה (אופצייה מתחת ל-Automation Account). יוצרים אחד חדש מסוג PowerShell(יש טמפלייטים מוכנים אך אין צורך במקרה זה), ובו הקוד הבא (יש לשנות את מה שבאדום):
param( [parameter(Mandatory=$True)]
[string] $SqlServer="myserver.database.windows.net",
[parameter(Mandatory=$True)]
[string] $Database="MyDatabse",
[parameter(Mandatory=$True)]
[string] $SqlCredentialAsset ="MyCredential"
)
$SqlCredential = Get-AutomationPSCredential -Name $SqlCredentialAsset
if ($SqlCredential -eq $null)
{
throw "Could not retrieve '$SqlCredentialAsset' credential asset. Check that you created this first in the Automation service."
}
# Get the username and password from the SQL Credential
$SqlUsername = $SqlCredential.UserName
$SqlPass = $SqlCredential.GetNetworkCredential().Password
# Define the connection to the SQL Database
$Conn = New-Object System.Data.SqlClient.SqlConnection("Server=tcp:$SqlServer;Database=$Database;User ID=$SqlUsername;Password=$SqlPass;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;")
# Open the SQL connection
$Conn.Open()
# Define the SQL command to run. In this case we are getting the number of rows in the table
$Cmd=new-object system.Data.SqlClient.SqlCommand("Exec MyProc;", $Conn)
$Cmd.CommandTimeout=0
# Execute the SQL command
$Ds=New-Object system.Data.DataSet
$Da=New-Object system.Data.SqlClient.SqlDataAdapter($Cmd)
[void]$Da.fill($Ds)
# Output the count
$Ds.Tables.Column1
# Close the SQL connection
$Conn.Close()
Schedule – במסך הניהול של ה-runbook שזה עתה יצרנו, ניתן להריץ הרצת נסיון (Start) או לתזמן אותו (Schedule.). ניתן ליצור אחד חדש, או להשתמש בקיים (אופצייה מתחת ל-Automation Account).
Jobs – מה שנקרא ג’וב ב-On Premise נקרא כאן כאמור runbook, ואילו jobs (אופצייה מתחת ל-Runbook) מתייחס ללוג של הריצות, לצורך מעקב וניטור שגיאות.