mirror of
https://github.com/Microsoft/sql-server-samples.git
synced 2025-12-08 14:58:54 +00:00
mod: switch to Az module
This commit is contained in:
@@ -1,47 +1,69 @@
|
||||
$parameters = $args[0]
|
||||
$scriptUrlBase = $args[1]
|
||||
|
||||
$subscriptionId = $parameters['subscriptionId']
|
||||
$resourceGroupName = $parameters['resourceGroupName']
|
||||
$virtualMachineName = $parameters['virtualMachineName']
|
||||
$virtualNetworkName = $parameters['virtualNetworkName']
|
||||
$managementSubnetName = $parameters['subnetName']
|
||||
$administratorLogin = $parameters['administratorLogin']
|
||||
$administratorLoginPassword = $parameters['administratorLoginPassword']
|
||||
$administratorLogin = $parameters['administratorLogin']
|
||||
$administratorLoginPassword = $parameters['administratorLoginPassword']
|
||||
|
||||
$scriptUrlBase = $args[1]
|
||||
|
||||
if($virtualMachineName -eq '' -or $virtualMachineName -eq $null) {
|
||||
if ($virtualMachineName -eq '' -or ($null -eq $virtualMachineName)) {
|
||||
$virtualMachineName = 'Jumpbox'
|
||||
Write-Host "VM Name: 'Jumpbox'." -ForegroundColor Green
|
||||
}
|
||||
|
||||
if($managementSubnetName -eq '' -or $managementSubnetName -eq $null) {
|
||||
if ($managementSubnetName -eq '' -or ($null -eq $managementSubnetName)) {
|
||||
$managementSubnetName = 'Management'
|
||||
Write-Host "Using subnet 'Management' to deploy jumpbox VM." -ForegroundColor Green
|
||||
}
|
||||
|
||||
function VerifyPSVersion
|
||||
{
|
||||
Write-Host "Verifying PowerShell version, must be 5.0 or higher."
|
||||
if($PSVersionTable.PSVersion.Major -ge 5)
|
||||
{
|
||||
Write-Host "PowerShell version verified." -ForegroundColor Green
|
||||
function VerifyPSVersion {
|
||||
Write-Host "Verifying PowerShell version."
|
||||
if ($PSVersionTable.PSEdition -eq "Desktop") {
|
||||
if (($PSVersionTable.PSVersion.Major -ge 6) -or
|
||||
(($PSVersionTable.PSVersion.Major -eq 5) -and ($PSVersionTable.PSVersion.Minor -ge 1))) {
|
||||
Write-Host "PowerShell version verified." -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Host "You need to install PowerShell version 5.1 or heigher." -ForegroundColor Red
|
||||
Break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host "You need to install PowerShell version 5.0 or heigher." -ForegroundColor Red
|
||||
Break;
|
||||
else {
|
||||
if ($PSVersionTable.PSVersion.Major -ge 6) {
|
||||
Write-Host "PowerShell version verified." -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Host "You need to install PowerShell version 6.0 or heigher." -ForegroundColor Red
|
||||
Break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function EnsureLogin ()
|
||||
{
|
||||
$context = Get-AzureRmContext
|
||||
If($null -eq $context.Subscription)
|
||||
{
|
||||
function EnsureAzModule {
|
||||
Write-Host "Checking if Az module is imported."
|
||||
$module = Get-Module Az
|
||||
If ($null -eq $module) {
|
||||
try {
|
||||
Import-Module Az -ErrorAction Stop
|
||||
Write-Host "Module Az imported." -ForegroundColor Green
|
||||
}
|
||||
catch {
|
||||
Install-Module Az -AllowClobber
|
||||
Write-Host "Module Az installed." -ForegroundColor Green
|
||||
}
|
||||
} else {
|
||||
Write-Host "Module Az imported." -ForegroundColor Green
|
||||
}
|
||||
}
|
||||
|
||||
function EnsureLogin () {
|
||||
$context = Get-AzContext
|
||||
If ($null -eq $context.Subscription) {
|
||||
Write-Host "Sign-in..."
|
||||
If($null -eq (Login-AzureRmAccount -ErrorAction SilentlyContinue -ErrorVariable Errors))
|
||||
{
|
||||
If ($null -eq (Connect-AzAccount -ErrorAction SilentlyContinue -ErrorVariable Errors)) {
|
||||
Write-Host ("Sign-in failed: {0}" -f $Errors[0].Exception.Message) -ForegroundColor Red
|
||||
Break
|
||||
}
|
||||
@@ -54,16 +76,14 @@ function SelectSubscriptionId {
|
||||
$subscriptionId
|
||||
)
|
||||
Write-Host "Selecting subscription '$subscriptionId'..."
|
||||
$context = Get-AzureRmContext
|
||||
If($context.Subscription.Id -ne $subscriptionId)
|
||||
{
|
||||
Try
|
||||
{
|
||||
Write-Host "Switching subscription $context.Subscription.Id to '$subscriptionId'." -ForegroundColor Green
|
||||
Select-AzureRmSubscription -SubscriptionId $subscriptionId -ErrorAction Stop | Out-null
|
||||
$context = Get-AzContext
|
||||
If ($context.Subscription.Id -ne $subscriptionId) {
|
||||
Try {
|
||||
$currentSubscriptionId = $context.Subscription.Id
|
||||
Write-Host "Switching subscription $currentSubscriptionId to '$subscriptionId'." -ForegroundColor Green
|
||||
Select-AzSubscription -SubscriptionId $subscriptionId -ErrorAction Stop | Out-null
|
||||
}
|
||||
Catch
|
||||
{
|
||||
Catch {
|
||||
Write-Host "Subscription selection failed: $_" -ForegroundColor Red
|
||||
Break
|
||||
}
|
||||
@@ -76,59 +96,52 @@ function LoadVirtualNetwork {
|
||||
$resourceGroupName,
|
||||
$virtualNetworkName
|
||||
)
|
||||
Write-Host("Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName, $resourceGroupName)
|
||||
$virtualNetwork = Get-AzureRmVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName -ErrorAction SilentlyContinue
|
||||
$id = $virtualNetwork.Id
|
||||
If($null -ne $id)
|
||||
{
|
||||
Write-Host "Virtual network with id $id is loaded." -ForegroundColor Green
|
||||
If($virtualNetwork.VirtualNetworkPeerings.Count -gt 0) {
|
||||
Write-Host "Virtual network is loaded, but it should not have peerings." -ForegroundColor Red
|
||||
}
|
||||
return $virtualNetwork
|
||||
}
|
||||
else
|
||||
{
|
||||
Write-Host "Virtual network $virtualNetworkName cannot be found." -ForegroundColor Red
|
||||
Break
|
||||
Write-Host("Loading virtual network '{0}' in resource group '{1}'." -f $virtualNetworkName, $resourceGroupName)
|
||||
$virtualNetwork = Get-AzVirtualNetwork -ResourceGroupName $resourceGroupName -Name $virtualNetworkName -ErrorAction SilentlyContinue
|
||||
$id = $virtualNetwork.Id
|
||||
If ($null -ne $id) {
|
||||
Write-Host "Virtual network with id $id is loaded." -ForegroundColor Green
|
||||
If ($virtualNetwork.VirtualNetworkPeerings.Count -gt 0) {
|
||||
Write-Host "Virtual network is loaded, but it should not have peerings." -ForegroundColor Red
|
||||
}
|
||||
return $virtualNetwork
|
||||
}
|
||||
else {
|
||||
Write-Host "Virtual network $virtualNetworkName cannot be found." -ForegroundColor Red
|
||||
Break
|
||||
}
|
||||
}
|
||||
|
||||
function SetVirtualNetwork
|
||||
{
|
||||
function SetVirtualNetwork {
|
||||
param($virtualNetwork)
|
||||
|
||||
Write-Host "Applying changes to the virtual network."
|
||||
Try
|
||||
{
|
||||
Set-AzureRmVirtualNetwork -VirtualNetwork $virtualNetwork -ErrorAction Stop | Out-Null
|
||||
Try {
|
||||
Set-AzVirtualNetwork -VirtualNetwork $virtualNetwork -ErrorAction Stop | Out-Null
|
||||
}
|
||||
Catch
|
||||
{
|
||||
Catch {
|
||||
Write-Host "Failed to configure Virtual Network: $_" -ForegroundColor Red
|
||||
}
|
||||
}
|
||||
|
||||
function ConvertCidrToUint32Array
|
||||
{
|
||||
function ConvertCidrToUint32Array {
|
||||
param($cidrRange)
|
||||
$cidrRangeParts = $cidrRange.Split("/")
|
||||
$ipParts = $cidrRangeParts[0].Split(".")
|
||||
$ipnum = ([Convert]::ToUInt32($ipParts[0]) -shl 24) -bor `
|
||||
([Convert]::ToUInt32($ipParts[1]) -shl 16) -bor `
|
||||
([Convert]::ToUInt32($ipParts[2]) -shl 8) -bor `
|
||||
[Convert]::ToUInt32($ipParts[3])
|
||||
([Convert]::ToUInt32($ipParts[1]) -shl 16) -bor `
|
||||
([Convert]::ToUInt32($ipParts[2]) -shl 8) -bor `
|
||||
[Convert]::ToUInt32($ipParts[3])
|
||||
|
||||
$maskbits = [System.Convert]::ToInt32($cidrRangeParts[1])
|
||||
$mask = 0xffffffff
|
||||
$mask = $mask -shl (32 -$maskbits)
|
||||
$mask = $mask -shl (32 - $maskbits)
|
||||
$ipstart = $ipnum -band $mask
|
||||
$ipend = $ipnum -bor ($mask -bxor 0xffffffff)
|
||||
return @($ipstart, $ipend)
|
||||
}
|
||||
|
||||
function ConvertUInt32ToIPAddress
|
||||
{
|
||||
function ConvertUInt32ToIPAddress {
|
||||
param($uint32IP)
|
||||
$v1 = $uint32IP -band 0xff
|
||||
$v2 = ($uint32IP -shr 8) -band 0xff
|
||||
@@ -137,16 +150,13 @@ function ConvertUInt32ToIPAddress
|
||||
return "$v4.$v3.$v2.$v1"
|
||||
}
|
||||
|
||||
function CalculateNextAddressPrefix
|
||||
{
|
||||
function CalculateNextAddressPrefix {
|
||||
param($virtualNetwork, $prefixLength)
|
||||
Write-Host "Calculating address prefix with length $prefixLength..."
|
||||
$startIPAddress = 0
|
||||
ForEach($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes)
|
||||
{
|
||||
ForEach ($addressPrefix in $virtualNetwork.AddressSpace.AddressPrefixes) {
|
||||
$endIPAddress = (ConvertCidrToUint32Array $addressPrefix)[1]
|
||||
If($endIPAddress -gt $startIPAddress)
|
||||
{
|
||||
If ($endIPAddress -gt $startIPAddress) {
|
||||
$startIPAddress = $endIPAddress
|
||||
}
|
||||
}
|
||||
@@ -156,22 +166,20 @@ function CalculateNextAddressPrefix
|
||||
return $addressPrefixResult
|
||||
}
|
||||
|
||||
function CalculateVpnClientAddressPoolPrefix
|
||||
{
|
||||
function CalculateVpnClientAddressPoolPrefix {
|
||||
param($gatewaySubnetPrefix)
|
||||
Write-Host "Calculating VPN client address pool prefix."
|
||||
If($gatewaySubnetPrefix.StartsWith("10."))
|
||||
{
|
||||
If ($gatewaySubnetPrefix.StartsWith("10.")) {
|
||||
return "192.168.0.0/24"
|
||||
}
|
||||
else
|
||||
{
|
||||
else {
|
||||
return "172.16.0.0/24"
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
VerifyPSVersion
|
||||
EnsureAzModule
|
||||
EnsureLogin
|
||||
SelectSubscriptionId -subscriptionId $subscriptionId
|
||||
|
||||
@@ -179,34 +187,36 @@ $virtualNetwork = LoadVirtualNetwork -resourceGroupName $resourceGroupName -virt
|
||||
|
||||
$subnets = $virtualNetwork.Subnets.Name
|
||||
|
||||
If($false -eq $subnets.Contains($managementSubnetName))
|
||||
{
|
||||
If ($false -eq $subnets.Contains($managementSubnetName)) {
|
||||
Write-Host "$managementSubnetName is not one of the subnets in $subnets" -ForegroundColor Yellow
|
||||
Write-Host "Creating subnet $managementSubnetName ($managementSubnetPrefix) in the VNet..." -ForegroundColor Green
|
||||
$managementSubnetPrefix = CalculateNextAddressPrefix $virtualNetwork 28
|
||||
Write-Host "Creating subnet $managementSubnetName ($managementSubnetPrefix) in the virtual network ..." -ForegroundColor Green
|
||||
|
||||
$virtualNetwork.AddressSpace.AddressPrefixes.Add($managementSubnetPrefix)
|
||||
Add-AzureRmVirtualNetworkSubnetConfig -Name $managementSubnetName -VirtualNetwork $virtualNetwork -AddressPrefix $managementSubnetPrefix | Out-Null
|
||||
Add-AzVirtualNetworkSubnetConfig -Name $managementSubnetName -VirtualNetwork $virtualNetwork -AddressPrefix $managementSubnetPrefix | Out-Null
|
||||
|
||||
SetVirtualNetwork $virtualNetwork
|
||||
Write-Host "Added subnet $managementSubnetName into VNet." -ForegroundColor Green
|
||||
} else {
|
||||
Write-Host "The subnet $managementSubnetName exists in the VNet." -ForegroundColor Green
|
||||
Write-Host "Added subnet $managementSubnetName into virtual network." -ForegroundColor Green
|
||||
}
|
||||
else {
|
||||
Write-Host "The subnet $managementSubnetName exists in the virtual network." -ForegroundColor Green
|
||||
}
|
||||
|
||||
Write-Host
|
||||
|
||||
# Start the deployment
|
||||
Write-Host "Starting deployment..."
|
||||
Write-Host "Deployment will take about 20m." -ForegroundColor Yellow
|
||||
|
||||
$templateParameters = @{
|
||||
virtualNetworkName = $virtualNetworkName
|
||||
managementSubnetName = $managementSubnetName
|
||||
virtualMachineName = $virtualMachineName
|
||||
administratorLogin = $administratorLogin
|
||||
administratorLoginPassword = $administratorLoginPassword
|
||||
location = $virtualNetwork.Location
|
||||
virtualNetworkName = $virtualNetworkName
|
||||
managementSubnetName = $managementSubnetName
|
||||
virtualMachineName = $virtualMachineName
|
||||
administratorLogin = $administratorLogin
|
||||
administratorLoginPassword = $administratorLoginPassword
|
||||
}
|
||||
|
||||
New-AzureRmResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase+'/azuredeploy.json?t='+ [DateTime]::Now.Ticks) -TemplateParameterObject $templateParameters
|
||||
New-AzResourceGroupDeployment -ResourceGroupName $resourceGroupName -TemplateUri ($scriptUrlBase + '/azuredeploy.json?t=' + [DateTime]::Now.Ticks) -TemplateParameterObject $templateParameters
|
||||
|
||||
Write-Host "Deployment completed."
|
||||
|
||||
Reference in New Issue
Block a user