Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# <FullScript>
# Connect-AzAccount
# The SubscriptionId in which to create these objects
$subscriptionId = "<Subscription-ID>"
# Set the resource group name and location for your source server
$sourceResourceGroupName = "mySourceResourceGroup-$(Get-Random)"
$sourceResourceGroupLocation = "westus2"
# Set the resource group name and location for your target server
$targetResourceGroupName = "myTargetResourceGroup-$(Get-Random)"
$targetResourceGroupLocation = "eastus"
# Set an admin login and password for your server
$adminSqlLogin = "<admin>"
$password = "<password>"
# The logical server names have to be unique in the system
$sourceServerName = "source-server-$(Get-Random)"
$targetServerName = "target-server-$(Get-Random)"
# The sample database name
$sourceDatabaseName = "mySampleDatabase"
$targetDatabaseName = "CopyOfMySampleDatabase"
# The IP address range that you want to allow to access your servers
$sourceStartIp = "0.0.0.0"
$sourceEndIp = "0.0.0.0"
$targetStartIp = "0.0.0.0"
$targetEndIp = "0.0.0.0"

# Set subscription
Set-AzContext -SubscriptionId $subscriptionId

# Create two new resource groups
$sourceResourceGroup = New-AzResourceGroup -Name $sourceResourceGroupName -Location $sourceResourceGroupLocation
$targetResourceGroup = New-AzResourceGroup -Name $targetResourceGroupName -Location $targetResourceGroupLocation

# Build the SQL administrator credential reused for both servers
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $adminSqlLogin, (ConvertTo-SecureString -String $password -AsPlainText -Force)

# Create a server with a system-wide unique server name
$sourceServerParams = @{
ResourceGroupName = $sourceResourceGroupName
ServerName = $sourceServerName
Location = $sourceResourceGroupLocation
SqlAdministratorCredentials = $adminCredential
}
$sourceResourceGroup = New-AzSqlServer @sourceServerParams
$targetServerParams = @{
ResourceGroupName = $targetResourceGroupName
ServerName = $targetServerName
Location = $targetResourceGroupLocation
SqlAdministratorCredentials = $adminCredential
}
$targetResourceGroup = New-AzSqlServer @targetServerParams

# Create a server firewall rule that allows access from the specified IP range
$sourceFirewallParams = @{
ResourceGroupName = $sourceResourceGroupName
ServerName = $sourceServerName
FirewallRuleName = "AllowedIPs"
StartIpAddress = $sourceStartIp
EndIpAddress = $sourceEndIp
}
$sourceServerFirewallRule = New-AzSqlServerFirewallRule @sourceFirewallParams
$targetFirewallParams = @{
ResourceGroupName = $targetResourceGroupName
ServerName = $targetServerName
FirewallRuleName = "AllowedIPs"
StartIpAddress = $targetStartIp
EndIpAddress = $targetEndIp
}
$targetServerFirewallRule = New-AzSqlServerFirewallRule @targetFirewallParams

# Create a blank database in the source-server with an S0 performance level
$sourceDatabaseParams = @{
ResourceGroupName = $sourceResourceGroupName
ServerName = $sourceServerName
DatabaseName = $sourceDatabaseName
RequestedServiceObjectiveName = "S0"
}
$sourceDatabase = New-AzSqlDatabase @sourceDatabaseParams

# Copy source database to the target server
$databaseCopyParams = @{
ResourceGroupName = $sourceResourceGroupName
ServerName = $sourceServerName
DatabaseName = $sourceDatabaseName
CopyResourceGroupName = $targetResourceGroupName
CopyServerName = $targetServerName
CopyDatabaseName = $targetDatabaseName
}
$databaseCopy = New-AzSqlDatabaseCopy @databaseCopyParams

# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $sourceResourceGroupName
# Remove-AzResourceGroup -ResourceGroupName $targetResourceGroupName
# </FullScript>
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# <FullScript>
# Connect-AzAccount
# The SubscriptionId in which to create these objects
$subscriptionId = "<Subscription-ID>"
# Set the resource group name and location for your server
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "westus2"
# Set an admin login and password for your server
$adminSqlLogin = "<admin>"
$password = "<password>"
# Set server name - the logical server name has to be unique in the system
$serverName = "server-$(Get-Random)"
# The sample database name
$databaseName = "mySampleDatabase"
# The IP address range that you want to allow to access your server
$startIp = "0.0.0.0"
$endIp = "0.0.0.0"

# Set subscription
Set-AzContext -SubscriptionId $subscriptionId

# Create a resource group
$resourceGroup = New-AzResourceGroup -Name $resourceGroupName -Location $location

# Create a credential for the server admin
$adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $adminSqlLogin, $(ConvertTo-SecureString -String $password -AsPlainText -Force)

# Create a server with a system-wide unique server name
$serverParams = @{
ResourceGroupName = $resourceGroupName
ServerName = $serverName
Location = $location
SqlAdministratorCredentials = $adminCredential
}
$server = New-AzSqlServer @serverParams

# Create a server firewall rule that allows access from the specified IP range
$firewallParams = @{
ResourceGroupName = $resourceGroupName
ServerName = $serverName
FirewallRuleName = "AllowedIPs"
StartIpAddress = $startIp
EndIpAddress = $endIp
}
$serverFirewallRule = New-AzSqlServerFirewallRule @firewallParams

# Create a blank database with an S0 performance level
$databaseParams = @{
ResourceGroupName = $resourceGroupName
ServerName = $serverName
DatabaseName = $databaseName
RequestedServiceObjectiveName = "S0"
SampleName = "AdventureWorksLT"
}
$database = New-AzSqlDatabase @databaseParams

# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
# </FullScript>
Loading