Skip to content

Commit 98dff66

Browse files
Merge pull request #437 from srdan-bozovic-msft/master
Add PS to attach VM with SSMS to existing SQL MI subnet
2 parents b695f00 + 1d71fa1 commit 98dff66

4 files changed

Lines changed: 459 additions & 1 deletion

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# Attaches VM with SQL Server Management Studio (SSMS) to Managed Instance virtual network
2+
3+
### Contents
4+
5+
[About this sample](#about-this-sample)<br/>
6+
[Before you begin](#before-you-begin)<br/>
7+
[Run this sample](#run-this-sample)<br/>
8+
[Sample details](#sample-details)<br/>
9+
[Disclaimers](#disclaimers)<br/>
10+
[Related links](#related-links)<br/>
11+
12+
13+
<a name=about-this-sample></a>
14+
15+
## About this sample
16+
17+
- **Applies to:** Azure SQL Database
18+
- **Key features:** Managed Instance
19+
- **Workload:** n/a
20+
- **Programming Language:** PowerShell
21+
- **Authors:** Srdan Bozovic
22+
- **Update history:** n/a
23+
24+
<a name=before-you-begin></a>
25+
26+
## Before you begin
27+
28+
To run this sample, you need the following prerequisites.
29+
30+
**Software prerequisites:**
31+
32+
1. PowerShell 5.1
33+
2. Azure PowerShell 5.4.2 or higher
34+
35+
**Azure prerequisites:**
36+
37+
1. Permission to manage Azure virtual network
38+
39+
<a name=run-this-sample></a>
40+
41+
## Run this sample
42+
43+
Run the script below from Windows PowerShell or Azure Cloud Shell
44+
45+
```powershell
46+
47+
$scriptUrlBase = 'https://raw.githubusercontent.com/Microsoft/sql-server-samples/master/samples/manage/azure-sql-db-managed-instance/attach-jumpbox'
48+
49+
$parameters = @{
50+
subscriptionId = '<subscriptionId>'
51+
resourceGroupName = '<resourceGroupName>'
52+
virtualNetworkName = '<virtualNetworkName>'
53+
administratorLogin = 'cloudSA'
54+
administratorLoginPassword = '<password>'
55+
}
56+
57+
Invoke-Command -ScriptBlock ([Scriptblock]::Create((iwr ($scriptUrlBase+'/attachJumpbox.ps1?t='+ [DateTime]::Now.Ticks)).Content)) -ArgumentList $parameters, $scriptUrlBase
58+
59+
```
60+
61+
<a name=sample-details></a>
62+
63+
## Sample details
64+
65+
This sample shows how to attach VM with SSMS to Managed Instance virtual network using PowerShell
66+
67+
<a name=disclaimers></a>
68+
69+
## Disclaimers
70+
The scripts and this guide are copyright Microsoft Corporations and are provided as samples. They are not part of any Azure service and are not covered by any SLA or other Azure-related agreements. They are provided as-is with no warranties express or implied. Microsoft takes no responsibility for the use of the scripts or the accuracy of this document. Familiarize yourself with the scripts before using them.
71+
72+
<a name=related-links></a>
73+
74+
## Related Links
75+
<!-- Links to more articles. Remember to delete "en-us" from the link path. -->
76+
77+
For more information, see these articles:
78+
79+
- [What is a Managed Instance (preview)?](https://docs.microsoft.com/azure/sql-database/sql-database-managed-instance)
80+
- [Configure a VNet for Azure SQL Database Managed Instance](https://docs.microsoft.com/azure/sql-database/sql-database-managed-instance-vnet-configuration)
Lines changed: 179 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,179 @@
1-
#placeholder
1+
$parameters = $args[0]
2+
3+
$subscriptionId = $parameters['subscriptionId']
4+
$resourceGroupName = $parameters['resourceGroupName']
5+
$virtualNetworkName = $parameters['virtualNetworkName']
6+
$administratorLogin = $parameters['administratorLogin']
7+
$administratorLoginPassword = $parameters['administratorLoginPassword']
8+
9+
$scriptUrlBase = $args[1]
10+
11+
function VerifyPSVersion
12+
{
13+
Write-Host "Verifying PowerShell version, must be 5.0 or higher."
14+
if($PSVersionTable.PSVersion.Major -ge 5)
15+
{
16+
Write-Host "PowerShell version verified." -ForegroundColor Green
17+
}
18+
else
19+
{
20+
Write-Host "You need to install PowerShell version 5.0 or heigher." -ForegroundColor Red
21+
Break;
22+
}
23+
}
24+
25+
function EnsureLogin ()
26+
{
27+
$context = Get-AzureRmContext
28+
If($null -eq $context.Subscription)
29+
{
30+
Write-Host "Loging in ..."
31+
If($null -eq (Login-AzureRmAccount -ErrorAction SilentlyContinue -ErrorVariable Errors))
32+
{
33+
Write-Host ("Login failed: {0}" -f $Errors[0].Exception.Message) -ForegroundColor Red
34+
Break
35+
}
36+
}
37+
Write-Host "User logedin." -ForegroundColor Green
38+
}
39+
40+
function SelectSubscriptionId {
41+
param (
42+
$subscriptionId
43+
)
44+
Write-Host "Selecting subscription '$subscriptionId'."
45+
$context = Get-AzureRmContext
46+
If($context.Subscription.Id -ne $subscriptionId)
47+
{
48+
Try
49+
{
50+
Select-AzureRmSubscription -SubscriptionId $subscriptionId -ErrorAction Stop | Out-null
51+
}
52+
Catch
53+
{
54+
Write-Host "Subscription selection failed: $_" -ForegroundColor Red
55+
Break
56+
}
57+
}
58+
Write-Host "Subscription selected." -ForegroundColor Green
59+
}
60+
61+
function LoadVirtualNetwork {
62+
param (
63+
$resourceGroupName,
64+
$virtualNetworkName
65+
)
66+
Write-Host("Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName, $resourceGroupName)
67+
$virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName -ErrorAction SilentlyContinue
68+
If($null -ne $virtualNetwork.Id)
69+
{
70+
Write-Host "Virtual network loaded." -ForegroundColor Green
71+
return $virtualNetwork
72+
}
73+
else
74+
{
75+
Write-Host "Virtual network not found." -ForegroundColor Red
76+
Break
77+
}
78+
}
79+
80+
function SetVirtualNetwork
81+
{
82+
param($virtualNetwork)
83+
84+
Write-Host "Applying changes to the virtual network."
85+
Try
86+
{
87+
Set-AzureRmVirtualNetwork -VirtualNetwork $virtualNetwork -ErrorAction Stop | Out-Null
88+
}
89+
Catch
90+
{
91+
Write-Host "Failed: $_" -ForegroundColor Red
92+
}
93+
}
94+
95+
function ConvertCidrToUint32Array
96+
{
97+
param($cidrRange)
98+
$cidrRangeParts = $cidrRange.Split(@(".","/"))
99+
$ipnum = ([Convert]::ToUInt32($cidrRangeParts[0]) -shl 24) -bor `
100+
([Convert]::ToUInt32($cidrRangeParts[1]) -shl 16) -bor `
101+
([Convert]::ToUInt32($cidrRangeParts[2]) -shl 8) -bor `
102+
[Convert]::ToUInt32($cidrRangeParts[3])
103+
104+
$maskbits = [System.Convert]::ToInt32($cidrRangeParts[4])
105+
$mask = 0xffffffff
106+
$mask = $mask -shl (32 -$maskbits)
107+
$ipstart = $ipnum -band $mask
108+
$ipend = $ipnum -bor ($mask -bxor 0xffffffff)
109+
return @($ipstart, $ipend)
110+
}
111+
112+
function ConvertUInt32ToIPAddress
113+
{
114+
param($uint32IP)
115+
$v1 = $uint32IP -band 0xff
116+
$v2 = ($uint32IP -shr 8) -band 0xff
117+
$v3 = ($uint32IP -shr 16) -band 0xff
118+
$v4 = ($uint32IP -shr 24)
119+
return "$v4.$v3.$v2.$v1"
120+
}
121+
122+
function CalculateNextAddressPrefix
123+
{
124+
param($virtualNetwork, $prefixLength)
125+
Write-Host "Calculating address prefix."
126+
$startIPAddress = 0
127+
ForEach($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes)
128+
{
129+
$endIPAddress = (ConvertCidrToUint32Array $addressPrefix)[1]
130+
If($endIPAddress -gt $startIPAddress)
131+
{
132+
$startIPAddress = $endIPAddress
133+
}
134+
}
135+
$startIPAddress += 1
136+
return (ConvertUInt32ToIPAddress $startIPAddress) + "/" + $prefixLength
137+
}
138+
139+
function CalculateVpnClientAddressPoolPrefix
140+
{
141+
param($gatewaySubnetPrefix)
142+
Write-Host "Calculating VPN client address pool prefix."
143+
If($gatewaySubnetPrefix.StartsWith("10."))
144+
{
145+
return "192.168.0.0/24"
146+
}
147+
else
148+
{
149+
return "172.16.0.0/24"
150+
}
151+
152+
}
153+
154+
VerifyPSVersion
155+
EnsureLogin
156+
SelectSubscriptionId -subscriptionId $subscriptionId
157+
158+
$virtualNetwork = LoadVirtualNetwork -resourceGroupName $resourceGroupName -virtualNetworkName $virtualNetworkName
159+
160+
$managementSubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
161+
162+
$virtualNetwork.AddressSpace.AddressPrefixes.Add($managementSubnetPrefix)
163+
Add-AzureRmVirtualNetworkSubnetConfig -Name Management -VirtualNetwork $virtualNetwork -AddressPrefix $managementSubnetPrefix | Out-Null
164+
165+
SetVirtualNetwork $virtualNetwork
166+
167+
Write-Host
168+
169+
# Start the deployment
170+
Write-Host "Starting deployment..."
171+
172+
$templateParameters = @{
173+
virtualNetworkName = $virtualNetworkName
174+
managementSubnetPrefix = $managementSubnetPrefix
175+
administratorLogin = $administratorLogin
176+
administratorLoginPassword = $administratorLoginPassword
177+
}
178+
179+
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase+'/azuredeploy.json?t='+ [DateTime]::Now.Ticks) -TemplateParameterObject $templateParameters

0 commit comments

Comments
 (0)