Skip to content

Commit

Permalink
Merge pull request #1 from hphuhtin/hphuhtin-update-sqlsyncmonitor-1
Browse files Browse the repository at this point in the history
Update DataSyncLogPowerShellRunbook.ps1
  • Loading branch information
hphuhtin authored Dec 12, 2021
2 parents 3444e43 + ce89d07 commit d092451
Showing 1 changed file with 23 additions and 43 deletions.
66 changes: 23 additions & 43 deletions samples/features/sql-data-sync/DataSyncLogPowerShellRunbook.ps1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#Requires -Modules @{ ModuleName="AzureRM.Sql"; ModuleVersion="4.12.1" }
#Requires -Modules @{ ModuleName="Az.Sql"; ModuleVersion="3.5.1" }
#Data Sync OMS Integration Runbook

#To use this script Change all the strings below to reflect your information.
#Setup a System Managed Identity in the Automation Account, with Reader permissions to the monitored SQL targets
#Also allow the managed identity access to the Automation Account itself to read/write the Variable used

#Information for Sync Group 1
#If you want to use all the sync groups in your subscription keep the $DS_xxxx fields empty.
Expand All @@ -16,14 +18,16 @@ $DS_ServerName = ""
$DS_DatabaseName = ""
$DS_SyncGroupName = ""

# Insert your Automation Account ResourceGroup and Account names
$AC_ResourceGroupName = "ResourceGroupName"
$AC_AccountName = "AutomationAccountName"
# Insert the name of the DateTime variable in the Automation Account for storing the last synctime
$AC_LastUpdatedTimeVariableName = "DataSyncLogLastUpdatedTime"

# Replace with your OMS Workspace ID
# Replace with your OMS Workspace ID (Log Analytics Workspace ID)
$CustomerId = "OMSCustomerID"

# Replace with your OMS Primary Key
# Replace with your OMS Primary Key (Log Analytics Primary Key)
$SharedKey = "SharedKey"

# Specify the name of the record type that you'll be creating
Expand All @@ -32,35 +36,12 @@ $LogType = "DataSyncLog"
# Specify a field with the created time for the records
$TimeStampField = "DateValue"

#Specify the interval of how often you want to send data to oms
#You can use -hours, -minutes or -days, use a negative number
#$interval = New-TimeSpan -hours -1


#Connect to Azure
$connectionName = "AzureRunAsConnection"
try
{
# Get the connection "AzureRunAsConnection "
$servicePrincipalConnection=Get-AutomationConnection -Name $connectionName

"Logging in to Azure..."
Add-AzureRmAccount `
-ServicePrincipal `
-TenantId $servicePrincipalConnection.TenantId `
-ApplicationId $servicePrincipalConnection.ApplicationId `
-CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint
}
catch {
if (!$servicePrincipalConnection)
{
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
} else{
Write-Error -Message $_.Exception
throw $_.Exception
}
}
# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process
# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -Identity).context
# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContextficateThumbprint $servicePrincipalConnection.CertificateThumbprint

# Create the function to create the authorization signature
Function Build-Signature ($customerId, $sharedKey, $date, $contentLength, $method, $contentType, $resource)
Expand All @@ -79,7 +60,6 @@ Function Build-Signature ($customerId, $sharedKey, $date, $contentLength, $metho
return $authorization
}


# Create the function to create and post the request
Function Post-OMSData($customerId, $sharedKey, $body, $logType)
{
Expand Down Expand Up @@ -113,19 +93,18 @@ Function Post-OMSData($customerId, $sharedKey, $body, $logType)


#Get Log Data
select-azurermsubscription -SubscriptionId $SubscriptionId
#Set the endtime, get StartTime-filter from Automation Account variable
$endtime =[System.DateTime]::UtcNow
$StartTime = Get-AzureRmAutomationVariable -ResourceGroupName $AC_ResourceGroupName `
$StartTime = Get-AzAutomationVariable -ResourceGroupName $AC_ResourceGroupName `
–AutomationAccountName $AC_AccountName `
-Name $AC_LastUpdatedTimeVariableName | Select -ExpandProperty Value


#Get Log
Write-Output "Getting Data Sync Log from $StartTime to $EndTime"

if ($DS_ResourceGroupName -eq "")
{
$ResourceGroupName = Get-AzureRmResourceGroup | select -ExpandProperty ResourceGroupName
$ResourceGroupName = Get-AzResourceGroup | select -ExpandProperty ResourceGroupName
}
else
{
Expand All @@ -136,7 +115,7 @@ foreach ($ResourceGroup in $ResourceGroupName)
{
if ($DS_ServerName -eq "")
{
$ServerName = Get-AzureRmSqlServer -ResourceGroupName $ResourceGroup | select -ExpandProperty ServerName
$ServerName = Get-AzSqlServer -ResourceGroupName $ResourceGroup | select -ExpandProperty ServerName
}
else
{
Expand All @@ -147,7 +126,7 @@ foreach ($ResourceGroup in $ResourceGroupName)
{
if ($DS_DatabaseName -eq "")
{
$DatabaseName = Get-AzureRmSqlDatabase -ResourceGroupName $ResourceGroup -ServerName $Server | select -ExpandProperty DatabaseName
$DatabaseName = Get-AzSqlDatabase -ResourceGroupName $ResourceGroup -ServerName $Server | select -ExpandProperty DatabaseName
}
else
{
Expand All @@ -163,7 +142,7 @@ foreach ($ResourceGroup in $ResourceGroupName)

if ($DS_SyncGroupName -eq "")
{
$SyncGroupName = Get-AzureRmSqlSyncGroup -ResourceGroupName $ResourceGroup -ServerName $Server -DatabaseName $Database | select -ExpandProperty SyncGroupName
$SyncGroupName = Get-AzSqlSyncGroup -ResourceGroupName $ResourceGroup -ServerName $Server -DatabaseName $Database | select -ExpandProperty SyncGroupName
}
else
{
Expand All @@ -172,7 +151,7 @@ foreach ($ResourceGroup in $ResourceGroupName)

foreach ($SyncGroup in $SyncGroupName)
{
$Logs = Get-AzureRmSqlSyncGroupLog -ResourceGroupName $ResourceGroup `
$Logs = Get-AzSqlSyncGroupLog -ResourceGroupName $ResourceGroup `
-ServerName $Server `
-DatabaseName $Database `
-SyncGroupName $SyncGroup `
Expand Down Expand Up @@ -226,9 +205,10 @@ foreach ($ResourceGroup in $ResourceGroupName)
}



Set-AzureRmAutomationVariable -ResourceGroupName $AC_ResourceGroupName `
# Write runtime into Automation Account variable
Set-AzAutomationVariable -ResourceGroupName $AC_ResourceGroupName `
–AutomationAccountName $AC_AccountName `
-DefaultProfile $AzureContext `
-Name $AC_LastUpdatedTimeVariableName `
-Value $EndTime `
-Encrypted $False

0 comments on commit d092451

Please sign in to comment.