使用 PowerShell 创建托管实例
适用于:Azure SQL 托管实例
此 PowerShell 脚本示例在新虚拟网络的专用子网中创建托管实例。 它还为虚拟网络配置路由表和网络安全组。 成功运行脚本后,即可从虚拟网络或本地环境访问托管实例。 请参阅将 Azure VM 配置为连接到 Azure SQL 数据库托管实例和配置从本地到 Azure SQL 托管实例的点到站点连接。
重要
有关限制,请参阅支持的区域。
本教程需要 Azure PowerShell 1.4.0 或更高版本。 如果需要进行升级,请参阅 Install Azure PowerShell module(安装 Azure PowerShell 模块)。 需要运行 Connect-AzAccount -Environment AzureChinaCloud
以创建与 Azure 的连接。
示例脚本
# <SetVariables>
$NSnetworkModels = "Microsoft.Azure.Commands.Network.Models"
$NScollections = "System.Collections.Generic"
Connect-AzAccount -Environment AzureChinaCloud
# The SubscriptionId in which to create these objects
$SubscriptionId = ''
# Set the resource group name and location for your managed instance
$resourceGroupName = "myResourceGroup-$(Get-Random)"
$location = "chinaeast2"
# Set the networking values for your managed instance
$vNetName = "myVnet-$(Get-Random)"
$vNetAddressPrefix = "10.0.0.0/16"
$defaultSubnetName = "myDefaultSubnet-$(Get-Random)"
$defaultSubnetAddressPrefix = "10.0.0.0/24"
$miSubnetName = "MISubnet-$(Get-Random)"
$miSubnetAddressPrefix = "10.0.0.0/24"
#Set the managed instance name for the new managed instance
$instanceName = "mi-name-$(Get-Random)"
# Set the admin login and password for your managed instance
$miAdminSqlLogin = "SqlAdmin"
$miAdminSqlPassword = "ChangeThisPassword!!"
# Set the managed instance service tier, compute level, and license mode
$edition = "General Purpose"
$vCores = 8
$maxStorage = 256
$computeGeneration = "Gen5"
$license = "LicenseIncluded" #"BasePrice" or LicenseIncluded if you have don't have SQL Server licence that can be used for AHB discount
$dbname = 'SampleDB'
# </SetVariables>
# <CreateResourceGroup>
# Set subscription context
$subscriptionContextParams = @{
SubscriptionId = $SubscriptionId
}
Set-AzContext @subscriptionContextParams
# Create a resource group
$resourceGroupParams = @{
Name = $resourceGroupName
Location = $location
Tag = @{Owner="SQLDB-Samples"}
}
$resourceGroup = New-AzResourceGroup @resourceGroupParams
# </CreateResourceGroup>
# <CreateVirtualNetwork>
# Configure virtual network, subnets, network security group, and routing table
$networkSecurityGroupParams = @{
Name = 'myNetworkSecurityGroupMiManagementService'
ResourceGroupName = $resourceGroupName
Location = $location
}
$networkSecurityGroupMiManagementService = New-AzNetworkSecurityGroup @networkSecurityGroupParams
$routeTableParams = @{
Name = 'myRouteTableMiManagementService'
ResourceGroupName = $resourceGroupName
Location = $location
}
$routeTableMiManagementService = New-AzRouteTable @routeTableParams
$virtualNetworkParams = @{
ResourceGroupName = $resourceGroupName
Location = $location
Name = $vNetName
AddressPrefix = $vNetAddressPrefix
}
$virtualNetwork = New-AzVirtualNetwork @virtualNetworkParams
$subnetConfigParams = @{
Name = $miSubnetName
VirtualNetwork = $virtualNetwork
AddressPrefix = $miSubnetAddressPrefix
NetworkSecurityGroup = $networkSecurityGroupMiManagementService
RouteTable = $routeTableMiManagementService
}
$subnetConfig = Add-AzVirtualNetworkSubnetConfig @subnetConfigParams | Set-AzVirtualNetwork
$virtualNetwork = Get-AzVirtualNetwork -Name $vNetName -ResourceGroupName $resourceGroupName
$subnet= $virtualNetwork.Subnets[0]
# Create a delegation
$subnet.Delegations = New-Object "$NScollections.List``1[$NSnetworkModels.PSDelegation]"
$delegationName = "dgManagedInstance" + (Get-Random -Maximum 1000)
$delegationParams = @{
Name = $delegationName
ServiceName = "Microsoft.Sql/managedInstances"
}
$delegation = New-AzDelegation @delegationParams
$subnet.Delegations.Add($delegation)
Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork
$miSubnetConfigId = $subnet.Id
$allowParameters = @{
Access = 'Allow'
Protocol = 'Tcp'
Direction= 'Inbound'
SourcePortRange = '*'
SourceAddressPrefix = 'VirtualNetwork'
DestinationAddressPrefix = '*'
}
$denyInParameters = @{
Access = 'Deny'
Protocol = '*'
Direction = 'Inbound'
SourcePortRange = '*'
SourceAddressPrefix = '*'
DestinationPortRange = '*'
DestinationAddressPrefix = '*'
}
$denyOutParameters = @{
Access = 'Deny'
Protocol = '*'
Direction = 'Outbound'
SourcePortRange = '*'
SourceAddressPrefix = '*'
DestinationPortRange = '*'
DestinationAddressPrefix = '*'
}
$networkSecurityGroupParams = @{
ResourceGroupName = $resourceGroupName
Name = "myNetworkSecurityGroupMiManagementService"
}
$networkSecurityGroup = Get-AzNetworkSecurityGroup @networkSecurityGroupParams
$allowRuleParams = @{
Access = 'Allow'
Protocol = 'Tcp'
Direction = 'Inbound'
SourcePortRange = '*'
SourceAddressPrefix = 'VirtualNetwork'
DestinationAddressPrefix = '*'
}
$denyInRuleParams = @{
Access = 'Deny'
Protocol = '*'
Direction = 'Inbound'
SourcePortRange = '*'
SourceAddressPrefix = '*'
DestinationPortRange = '*'
DestinationAddressPrefix = '*'
}
$denyOutRuleParams = @{
Access = 'Deny'
Protocol = '*'
Direction = 'Outbound'
SourcePortRange = '*'
SourceAddressPrefix = '*'
DestinationPortRange = '*'
DestinationAddressPrefix = '*'
}
$networkSecurityGroup |
Add-AzNetworkSecurityRuleConfig @allowRuleParams -Priority 1000 -Name "allow_tds_inbound" -DestinationPortRange 1433 |
Add-AzNetworkSecurityRuleConfig @allowRuleParams -Priority 1100 -Name "allow_redirect_inbound" -DestinationPortRange 11000-11999 |
Add-AzNetworkSecurityRuleConfig @denyInRuleParams -Priority 4096 -Name "deny_all_inbound" |
Add-AzNetworkSecurityRuleConfig @denyOutRuleParams -Priority 4096 -Name "deny_all_outbound" |
Set-AzNetworkSecurityGroup
# </CreateVirtualNetwork>
# <CreateManagedInstance>
# Create credentials
$secpassword = ConvertTo-SecureString $miAdminSqlPassword -AsPlainText -Force
$credential = New-Object System.Management.Automation.PSCredential -ArgumentList @($miAdminSqlLogin, $secpassword)
$managedInstanceParams = @{
Name = $instanceName
ResourceGroupName = $resourceGroupName
Location = $location
SubnetId = $miSubnetConfigId
AdministratorCredential = $credential
StorageSizeInGB = $maxStorage
VCore = $vCores
Edition = $edition
ComputeGeneration = $computeGeneration
LicenseType = $license
}
New-AzSqlInstance @managedInstanceParams
# </CreateManagedInstance>
# <CreateDatabase>
$databaseParams = @{
ResourceGroupName = $resourceGroupName
InstanceName = $instanceName
Name = $dbname
Collation = 'Latin1_General_100_CS_AS_SC'
}
New-AzSqlInstanceDatabase @databaseParams
# </CreateDatabase>
# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
# </CreateDatabase>
# Clean up deployment
# Remove-AzResourceGroup -ResourceGroupName $resourceGroupName
清理部署
使用以下命令删除资源组及其相关的所有资源。
Remove-AzResourceGroup -ResourceGroupName $resourcegroupname
脚本说明
此脚本使用以下命令中的某些命令。 有关下表中使用的命令和其他命令的详细信息,请单击命令特定文档的链接。
命令 | 注释 |
---|---|
New-AzResourceGroup | 创建用于存储所有资源的资源组。 |
New-AzVirtualNetwork | 创建虚拟网络。 |
Add-AzVirtualNetworkSubnetConfig | 向虚拟网络添加子网配置。 |
Get-AzVirtualNetwork | 获取资源组中的虚拟网络。 |
Set-AzVirtualNetwork | 设置虚拟网络的目标状态。 |
Get-AzVirtualNetworkSubnetConfig | 获取虚拟网络中的子网。 |
Set-AzVirtualNetworkSubnetConfig | 配置虚拟网络中子网配置的目标状态。 |
New-AzRouteTable | 创建路由表。 |
Get-AzRouteTable | 获取路由表。 |
Set-AzRouteTable | 设置路由表的目标状态。 |
New-AzSqlInstance | 创建托管实例。 |
New-AzSqlInstanceDatabase | 为托管实例创建数据库。 |
Remove-AzResourceGroup | 删除资源组,包括所有嵌套的资源。 |
后续步骤
有关 Azure PowerShell 的详细信息,请参阅 Azure PowerShell 文档。
Azure SQL 托管实例的其他 PowerShell 脚本示例可在 Azure SQL 托管实例 PowerShell 脚本中找到。