Consider you have a Dedicated SQL Pool in an Azure Subscription and you need to create a copy of this resource to a newly created Azure Subscription under the same tenant. To be more precise, please refer to the below image.

If you ever be in that situation, you may follow the below steps to achieve your goal. These steps must be executed in Windows PowerShell.
- $ErrorActionPreference = “Stop”
- The $ErrorActionPreference variable specifies the action to take in response to an error occurring. The following values are supported:
- SilentlyContinue — Don’t display an error message continue to execute subsequent commands.
- Continue — Display any error message and attempt to continue execution of subsequence commands.
- Inquire — Prompts the user whether to continue or terminate the action
- Stop — Terminate the action with error.
- In our case we set the variable to “Stop” by using the above command.
- The $ErrorActionPreference variable specifies the action to take in response to an error occurring. The following values are supported:
- $SourceSubscriptionName=”Source_Subscription_Name”
- Set your Source Subscription Name
- $TargetSubscriptionName=”Target_Subscription_Name”
- Set your Target Subscription Name
- $TargetSubscriptionID=(Get-AzSubscription -SubscriptionName $TargetSubscriptionName).id
- Get the Id of the Target Subscription and store in the variable $TargetSubscriptionID
- $SourceResourceGroupName=”Source_Resource_Group”
- Set the Source Resource Group Name
- $TargetResourceGroupName=”Target_Resource_Group”
- Set the Target Resource Group Name
- $SourceServerName=”SourceSQLServerName” # Without database.windows.net
- Set the Source SQL Server Name without database.windows.net
- For example if the server name is “sourceserver.database.windows.net”, just specify “sourceserver”.
- $TargetServerName=”TargetSQLServerName” # Without database.windows.net
- Set the Target SQL Server Name without database.windows.net
- For example if the server name is “targetserver.database.windows.net”, just specify “targetserver”.
- $SourceDatabaseName=”SourceDatabaseName”
- Set the Source Database Name
- $TargetDatabaseName=”TargetDatabaseName”
- Set the Target Database Name
- $location = “Region”
- Set the Region
- $Environment=”Target_Environment_Name”
- Set the Target Environment Name
- You can set any name as you see fit
- $TargetAdminUser = “Target_Admin_User”
- Set the Target Admin User
- $TargetAdminPassword = -join ((65..90) + (97..122) | Get-Random -Count 14 | % {[char]$_}) + ‘.’ + $(Get-Random 100)
- Generate the Target Admin User Password
- $TargetAdminPassword = ConvertTo-SecureString “$TargetAdminPassword” -AsPlainText -Force
- Convert the Target Admin User Password to a Plain Text
- $SQLCreds = New-Object System.Management.Automation.PSCredential (“$TargetAdminUser”, $TargetAdminPassword)
- Store the Target Admin User and Target Admin User Password
- Set-AzContext -SubscriptionName “$SourceSubscriptionName”
- This command is going to set the Subscription to the Source Subscription
- New-AzSqlServer -ResourceGroupName “$SourceResourceGroupName” -ServerName “$TargetServerName” -Location “$location” -SqlAdministratorCredentials $SQLCreds
- This command will create a new SQL Server in the Source Resource Group with the name as mentioned in the variable $TargetServerName
- Set-AzContext -SubscriptionName “$TargetSubscriptionName”
- This command is going to set the Subscription to the Target Subscription
- Set-AzKeyVaultSecret -VaultName “TargetKeyVaultName” -Name “DWH-Admin-Password” -SecretValue $TargetAdminPassword
- The above command is to save the Target Admin User Password to the Target Key Vault under the name DWH-Admin-Password
- Set-AzContext -SubscriptionName “$SourceSubscriptionName”
- This command is going to set the Subscription to the Source Subscription
- $SourceDatabase = Get-AzSqlDatabase -ResourceGroupName $SourceResourceGroupName -ServerName $SourceServerName -DatabaseName $SourceDatabaseName
- This command is to get the specific database to restore
- $PointInTime = ((Get-AzSqlDatabaseRestorePoint -ResourceGroupName “$SourceResourceGroupName” -ServerName “$SourceServerName” -DatabaseName “$SourceDatabaseName”).RestorePointCreationDate)[-1]
- The above command is to pick the most recent restore point using RestorePointCreationDate “xx/xx/xxxx xx:xx:xx xx
- Restore-AzSqlDatabase -FromPointInTimeBackup -PointInTime “$PointInTime” -ResourceGroupName “$SourceResourceGroupName” -ServerName “$TargetServerName” -TargetDatabaseName “$TargetDatabaseName” -ResourceId $SourceDatabase.ResourceID
- The above command is to perform Restore to the specified target server in the SOURCE subscription
- Move-AzResource -DestinationResourceGroupName $TargetResourceGroupName -ResourceId (Get-AzResource -ResourceGroupName “$SourceResourceGroupName” -ResourceName “$TargetServerName”).ResourceId -DestinationSubscriptionId $TargetSubscriptionID -Force
- Move to Target Subscription and RG
Few Points to Note:
- You do not have to create either the SQL Server or the SQL Data Warehouse in the Target Subscription. The above code snippet will automatically create those resources.
- The logic above creates the SQL server and SQL Database in the Source Subscription and then moves them to the Target Subscription
ITECHSTORECA
FOR ALL YOUR TECH SOLUTIONS