Skip to content

Commit b687860

Browse files
author
Cameron Battagler
committed
Moving to new authentication method, more in depth documentation
1 parent 4fb964b commit b687860

File tree

2 files changed

+37
-41
lines changed

2 files changed

+37
-41
lines changed

samples/manage/azure-automation-automated-export/AutoExport.ps1

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ $retryLimit = 5;
3131
# The number of minutes you want to wait for an operation to finish before you fail.
3232
$waitInMinutes = 30;
3333

34+
# Connection Asset Name for Authenticating (Keep as AzureClassicRunAsConnection if you created the default RunAs accounts)
35+
$connectionAssetName = "AzureClassicRunAsConnection";
36+
3437
$storageKeyVariableName = "STORAGEKEYVARIABLENAME";
3538
$storageAccountName = "STORAGEACCOUNTNAME";
36-
$automationCertificateName = "CERTIFICATENAME";
37-
$subId = "00000000-0000-0000-0000-000000000000";
38-
$subName = "SUBSCRIPTIONNAME";
39+
$storageContainerName = "STORAGECONTAINERNAME";
3940

4041
function LogMessage($message)
4142
{
@@ -147,7 +148,7 @@ function StartExport($dbObj)
147148
# Get the storage context.
148149
$stgctx = New-AzureStorageContext -StorageAccountName $global:storageAccountName -StorageAccountKey $storageKey;
149150
# Start the export. If there is an error, stop the export and set the state to ToDrop.
150-
$dbObj.Export = Start-AzureSqlDatabaseExport -SqlConnectionContext $ctx -StorageContext $stgctx -StorageContainerName autoexportcontainer -DatabaseName $dbObj.DatabaseCopyName -BlobName $blobName;
151+
$dbObj.Export = Start-AzureSqlDatabaseExport -SqlConnectionContext $ctx -StorageContext $stgctx -StorageContainerName $global:storageContainerName -DatabaseName $dbObj.DatabaseCopyName -BlobName $blobName;
151152
# $? is true if the last command succeeded and false if the last command failed. If it is false, go to the ToDrop state.
152153
if (-not $? -and $global:retryLimit -ile $dbObj.RetryCount)
153154
{
@@ -266,11 +267,25 @@ function ExportProcess
266267
}
267268
}
268269

269-
# Get the certificate to authenticate the subscription
270-
$cert = Get-AutomationCertificate -Name $global:automationCertificateName;
271-
# Set the subscription to use
272-
Set-AzureSubscription -SubscriptionName $global:subName -Certificate $cert -SubscriptionId $global:subID;
273-
Select-AzureSubscription -Current $global:subName;
270+
# Authenticate to Azure with certificate
271+
Write-Verbose "Get connection asset: $connectionAssetName" -Verbose;
272+
$Conn = Get-AutomationConnection -Name $connectionAssetName;
273+
if ($Conn -eq $null)
274+
{
275+
throw "Could not retrieve connection asset: $connectionAssetName. Assure that this asset exists in the Automation account.";
276+
}
277+
278+
$CertificateAssetName = $Conn.CertificateAssetName;
279+
Write-Verbose "Getting the certificate: $CertificateAssetName" -Verbose;
280+
$AzureCert = Get-AutomationCertificate -Name $CertificateAssetName;
281+
if ($AzureCert -eq $null)
282+
{
283+
throw "Could not retrieve certificate asset: $CertificateAssetName. Assure that this asset exists in the Automation account.";
284+
}
285+
286+
Write-Verbose "Authenticating to Azure with certificate." -Verbose;
287+
Set-AzureSubscription -SubscriptionName $Conn.SubscriptionName -SubscriptionId $Conn.SubscriptionID -Certificate $AzureCert;
288+
Select-AzureSubscription -SubscriptionId $Conn.SubscriptionID;
274289

275290
$currentIndex = 0;
276291
for($currentRun = 0; $currentRun -lt ([math]::Ceiling($databaseServerPairs.Length/$batchingLimit)); $currentRun++)

samples/manage/azure-automation-automated-export/README.md

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,35 +8,18 @@ author: trgrie-msft
88

99
Provides the scripts and lists the steps to set up automatically exporting your databases to Azure Storage with Azure Automation.
1010

11-
## Azure Automation Set Up
12-
13-
1. Create and uploade the certificates that you will use to authenticate your connection to azure.
14-
- Run powershell as admin.
15-
- Run the New-SelfSignedCertificate command:
16-
17-
18-
`$cert = New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -DnsName <certificateName>`
19-
20-
- Export the certificate as a .cer file
21-
22-
`Export-Certificate -Cert "cert:\localmachine\my\$($cert.Thumbprint)" -FilePath <PathAndFileName>.cer`
23-
24-
- Create a corresponding pfx certificate by taking the thumbprint of the newly created certificate and running these commands:
25-
26-
`$CertPassword = ConvertTo-SecureString -String <YourPassword> -Force -AsPlainText`
27-
28-
`Export-PfxCertificate -Cert "cert:\localmachine\my\$($cert.Thumbprint)" -FilePath <PathAndFileName>.pfx -Password $CertPassword`
29-
30-
- Upload the .cer file to your subscription [in the old portal](https://manage.windowsazure.com/)
31-
- Upload the .pfx file to the certificates under Assets in the automation account that you want to use on Azure. You will use the password you gave in the previous step to authenticate it.
32-
2. Create new a new credentials asset to authenticate your server with.
33-
- Under assets, click on Credentials, and then click on Add a credential.
34-
- Name the credential and give the username and password that you will be logging into the server with.
35-
3. Create a new variable asset to pass the storage key of the Azure storage account you will be using.
36-
- Under assets, click on variables and then Add a variable.
37-
- Give the value of the storage key and you can make it encrypted so that only Azure Automation can read the variable and it won't show the key in plaintext if someone looks at the variable.
38-
4. Set Up Log Analytics (OMS) and Alerts
39-
- If you don't have Log Analytics set up on your Azure account, follow [these](https://azure.microsoft.com/en-us/documentation/articles/automation-manage-send-joblogs-log-analytics/) instructions for setting it up.
11+
## Prerequisite Set Up
12+
13+
1. Create and set up your Azure Automation Account
14+
- Create an Azure Automation Account by [following the instructions here](https://docs.microsoft.com/en-us/azure/automation/automation-sec-configure-azure-runas-account).
15+
2. Add Azure Automation Credential assets for your SQL Azure servers
16+
- Create your Automation Credential for each of your SQL Azure servers you intend to export by [following the instructions here](https://docs.microsoft.com/en-us/azure/automation/automation-credentials#creating-a-new-credential-asset).
17+
3. Create the Azure Storage Account to hold your bacpac files
18+
- Create the Storage Account by [following the instructions here](https://docs.microsoft.com/en-us/azure/storage/storage-create-storage-account#create-a-storage-account).
19+
- Copy your Storage Account access keys by [following the instructions here](https://docs.microsoft.com/en-us/azure/storage/storage-create-storage-account#view-and-copy-storage-access-keys).
20+
- Create an Azure Automation string Variable asset for your Storage Account access key by [following the instructions here](https://docs.microsoft.com/en-us/azure/automation/automation-variables#creating-an-automation-variable).
21+
4. Set Up Log Analytics (OMS) and Alerts (optional for alerting)
22+
- If you don't have Log Analytics set up on your Azure account, [follow these](https://docs.microsoft.com/en-us/azure/automation/automation-manage-send-joblogs-log-analytics) instructions for setting it up.
4023
5. Set Up Log Analytics Alerts
4124
- To send yourself an email if an error occurs or one of the jobs fails, you need to set up alerts.
4225
- Select your log analytics account that you want to use in the azure portal and click on the OMS Portal box under Management.
@@ -55,9 +38,7 @@ Provides the scripts and lists the steps to set up automatically exporting your
5538
- $waitTimeInMinutes: This tells the script how long it can wait for an operation to complete before it fails.
5639
- $storageKeyVariableName: This is the AutomationAccount you created the StorageKey variable under (probably the same one you are running the RunBook under) and -Name is the name of the variable.
5740
- $storageAccountName: This is the name of the storage account you are exporting to.
58-
- $automationCertificateName for Get-AutomationCertificate: This is the name of the certificate you setup to authenticate with Azure.
59-
- $subId: The ID of the subscription you are using. This will be used to tell Azure Automation which subscription to use.
60-
- $subName: The name of the subscription you are using. This will be used to tell Azure Automation which subscription to use.
41+
- $connectionAssetName: Connection Asset Name for Authenticating (Keep as AzureClassicRunAsConnection if you created the default RunAs accounts)
6142
2. In AutoExportBlobRetention, here are the values that need to be modified:
6243
- -Name for Get-AzureAutomationVariable: This is the AutomationAccount you created the StorageKey variable under (probably the same one you are running the RunBook under) and -Name is the name of the variable.
6344
- $storageContainer: This is the name of the storage container where you will be monitoring the exported blobs.

0 commit comments

Comments
 (0)