Skip to content

Instantly share code, notes, and snippets.

@brettmillerb
Last active August 20, 2020 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brettmillerb/5a5ab9a9a8c987e30a161dced31b35df to your computer and use it in GitHub Desktop.
Save brettmillerb/5a5ab9a9a8c987e30a161dced31b35df to your computer and use it in GitHub Desktop.
ElasticPool Migration to vCore
[CmdletBinding()]
param (
[int]
$MigrationLimit = 1
)
$connectionName = "connectionName"
try {
# Get the connection "AzureRunAsConnection" You can't use the Az module version for reasons.
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$connectAzAccountSplat = @{
ServicePrincipal = $true
TenantId = $servicePrincipalConnection.TenantId
ApplicationId = $servicePrincipalConnection.ApplicationId
CertificateThumbprint = $servicePrincipalConnection.CertificateThumbprint
}
Connect-AzAccount @connectAzAccountSplat -ErrorAction Stop | Out-Null
}
catch {
if (!$servicePrincipalConnection) {
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
}
else {
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$automationAccountName = Get-AutomationVariable -Name 'AutomationAccountName'
$automationAccountResourceGroup = Get-AutomationVariable -Name 'AutomationAccountResourceGroup'
# Get all Elastic Pools & Group output by servername.
# This means that one EP per server will be migrated ( dependent on $MigrationLimit )
# Expand the groups and select the first elasticpool from each server which does not have the migrated tag.
$elasticpools = (Get-AzSqlServer | Get-AzSqlElasticPool).where{$_.skuname -eq 'StandardPool'} |
Group-Object -Property Servername | Foreach-Object {
$_ | Select-Object -ExpandProperty Group |
Select-Object -First 1
}
# Select the Elastic Pools to Migrate based on the MigrationLimit Parameter
$elasticpoolsToMigrate = $elasticpools | Select-Object -First $MigrationLimit
foreach ($elasticPool in $elasticpoolsToMigrate) {
Write-Output ('Starting runbook to migrate {0}' -f $elasticPool.ElasticPoolName)
$migrateElasticPoolSplat = @{
ResourceGroup = $elasticPool.ResourceGroupName
ElasticPoolName = $elasticPool.ElasticPoolName
ServerName = $elasticPool.ServerName
Edition = 'GeneralPurpose'
VCoreCount = 2
ComputeGeneration = 'Gen5'
DatabaseVCoreMax = 0.25
}
$startAutomationRunBookSplat = @{
AutomationAccountName = $automationAccountName
Name = 'Invoke-DbMigration'
ResourceGroupName = $automationAccountResourceGroup
Parameters = $migrateElasticPoolSplat
}
Start-AzAutomationRunbook @startAutomationRunBookSplat -verbose
$elasticPool | Set-AzSqlElasticPool -Tags @{
vCoreMigrated = (Get-Date -Format FileDate)
}
}
[CmdletBinding()]
param (
[string]
$ResourceGroup,
[string]
$ElasticPoolName,
[string]
$ServerName,
[string]
$Edition = 'GeneralPurpose',
[int]
$VCoreCount = 2,
[string]
$ComputeGeneration = 'Gen5',
[double]
$DatabaseVCoreMax = 0.25
)
$connectionName = "connectionName"
try {
# Get the connection "AzureRunAsConnection" You can't use the Az module version for reasons.
$servicePrincipalConnection = Get-AutomationConnection -Name $connectionName
"Logging in to Azure..."
$connectAzAccountSplat = @{
ServicePrincipal = $true
TenantId = $servicePrincipalConnection.TenantId
ApplicationId = $servicePrincipalConnection.ApplicationId
CertificateThumbprint = $servicePrincipalConnection.CertificateThumbprint
}
Connect-AzAccount @connectAzAccountSplat
}
catch {
if (!$servicePrincipalConnection) {
$ErrorMessage = "Connection $connectionName not found."
throw $ErrorMessage
}
else {
Write-Error -Message $_.Exception
throw $_.Exception
}
}
$setAzSqlElasticPoolsSplat = @{
ResourceGroupName = $ResourceGroup
ElasticPoolName = $ElasticPoolName
ServerName = $ServerName
Edition = $Edition
VCore = $VCoreCount
ComputeGeneration = $ComputeGeneration
DatabaseVCoreMax = $DatabaseVCoreMax
}
Set-AzSqlElasticPool @setAzSqlElasticPoolsSplat
Write-Output ('Migrating {0}' -f $setAzSqlElasticPoolsSplat['ElasticPoolName'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment