Skip to content

Commit 6bf175d

Browse files
add: attach jumpbox
1 parent 972ca56 commit 6bf175d

3 files changed

Lines changed: 430 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: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,164 @@
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 ConvertCidrToUint32Array
81+
{
82+
param($cidrRange)
83+
$cidrRangeParts = $cidrRange.Split(@(".","/"))
84+
$ipnum = ([Convert]::ToUInt32($cidrRangeParts[0]) -shl 24) -bor `
85+
([Convert]::ToUInt32($cidrRangeParts[1]) -shl 16) -bor `
86+
([Convert]::ToUInt32($cidrRangeParts[2]) -shl 8) -bor `
87+
[Convert]::ToUInt32($cidrRangeParts[3])
88+
89+
$maskbits = [System.Convert]::ToInt32($cidrRangeParts[4])
90+
$mask = 0xffffffff
91+
$mask = $mask -shl (32 -$maskbits)
92+
$ipstart = $ipnum -band $mask
93+
$ipend = $ipnum -bor ($mask -bxor 0xffffffff)
94+
return @($ipstart, $ipend)
95+
}
96+
97+
function ConvertUInt32ToIPAddress
98+
{
99+
param($uint32IP)
100+
$v1 = $uint32IP -band 0xff
101+
$v2 = ($uint32IP -shr 8) -band 0xff
102+
$v3 = ($uint32IP -shr 16) -band 0xff
103+
$v4 = ($uint32IP -shr 24)
104+
return "$v4.$v3.$v2.$v1"
105+
}
106+
107+
function CalculateNextAddressPrefix
108+
{
109+
param($virtualNetwork, $prefixLength)
110+
Write-Host "Calculating address prefix."
111+
$startIPAddress = 0
112+
ForEach($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes)
113+
{
114+
$endIPAddress = (ConvertCidrToUint32Array $addressPrefix)[1]
115+
If($endIPAddress -gt $startIPAddress)
116+
{
117+
$startIPAddress = $endIPAddress
118+
}
119+
}
120+
$startIPAddress += 1
121+
return (ConvertUInt32ToIPAddress $startIPAddress) + "/" + $prefixLength
122+
}
123+
124+
function CalculateVpnClientAddressPoolPrefix
125+
{
126+
param($gatewaySubnetPrefix)
127+
Write-Host "Calculating VPN client address pool prefix."
128+
If($gatewaySubnetPrefix.StartsWith("10."))
129+
{
130+
return "192.168.0.0/24"
131+
}
132+
else
133+
{
134+
return "172.16.0.0/24"
135+
}
136+
137+
}
138+
139+
VerifyPSVersion
140+
EnsureLogin
141+
SelectSubscriptionId -subscriptionId $subscriptionId
142+
143+
$virtualNetwork = LoadVirtualNetwork -resourceGroupName $resourceGroupName -virtualNetworkName $virtualNetworkName
144+
145+
$managementSubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
146+
147+
$virtualNetwork.AddressSpace.AddressPrefixes.Add($managementSubnetPrefix)
148+
Add-AzureRmVirtualNetworkSubnetConfig -Name Management -VirtualNetwork $virtualNetwork -AddressPrefix $managementSubnetPrefix | Out-Null
149+
150+
Set-VirtualNetwork $virtualNetwork
151+
152+
Write-Host
153+
154+
# Start the deployment
155+
Write-Host "Starting deployment..."
156+
157+
$templateParameters = @{
158+
virtualNetworkName = $virtualNetworkName
159+
managementSubnetPrefix = $managementSubnetPrefix
160+
administratorLogin = $administratorLogin
161+
administratorLoginPassword = $administratorLoginPassword
162+
}
163+
164+
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase+'/azuredeploy.json?t='+ [DateTime]::Now.Ticks) -TemplateParameterObject $templateParameters

0 commit comments

Comments
 (0)