How to create a SQL Server in Windows VM in Azure with PowerShell.

The below powershell script will create a SQL Server in Azure VM. You need a subscription to to work with Azure.
cls
#Variables
$Location = "EastUS"
$ResourceGroupName = 'TestRG'

#Storage
$StorageName = "123456stest"
$StorageSKu = "Standard_LRS"

##Network
$InterfaceName = $ResourceGroupName + "ServerInterface"
$NSGName = $ResourceGroupName + "nsg"
$VnetName = $ResourceGroupName + "VNet"
$SubnetName = "Default"
$VnetAddressPrefix = '10.0.0.0/16'
$VnetSubnetAddressPrefix = '10.0.0.0/24'
$TCPIPAllocationMethod = "Dynamic"
$DomainName = "mamillapalli"

##Compute
$VMName = $ResourceGroupName+"VM"
$ComputerName = $ResourceGroupName+"Server"
$VmSize = "Standard_B2s"
$OSDiskName = "rameshDisk"

#Image
$PublisherName = "MicrosoftSQLServer"
$OfferName = "SQL2017-WS2016"
$sku = "SQLDEV"
$Version = "latest"

#Login SQL Server envionrmnet
#Login-AzAccount
#Resource group creation
Write-Host "Creating a resource group named $ResourceGroupName...." -ForegroundColor DarkGreen -Verbose
New-AzResourceGroup `
-Name $ResourceGroupName `
-Location $Location `
-Verbose

#Creating a storage account
Write-Host "Creating a storage account named $StorageName...." -ForegroundColor DarkGreen -Verbose
$StorageAccount = New-AzStorageAccount `
-ResourceGroupName $ResourceGroupName `
-Name $StorageName `
-SkuName $StorageSKu `
-Location $Location `
-Kind Storage -Verbose

#Network
Write-Host "Creating a subnet  named $SubnetName...." -ForegroundColor DarkGreen -Verbose
$SubnetConfig = New-AzVirtualNetworkSubnetConfig `
-Name $SubnetName `
-AddressPrefix $VnetSubnetAddressPrefix `
-Verbose
Write-Host "Creating a virtual network  named $VnetName...." -ForegroundColor DarkGreen -Verbose
$Vnet = New-AzVirtualNetwork `
-Name $VnetName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-AddressPrefix $VnetAddressPrefix `
-Subnet $SubnetConfig `
-Verbose
Write-Host "Creating a public ip  named $InterfaceName...." -ForegroundColor DarkGreen -Verbose
$PublicIP = New-AzPublicIpAddress `
-Name $InterfaceName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-AllocationMethod $TCPIPAllocationMethod  `
-DomainNameLabel $DomainName `
-Verbose

Write-Host "Creating a NSG rule named RDPRule...." -ForegroundColor DarkGreen -Verbose
$NSGRuleRDP = New-AzNetworkSecurityRuleConfig `
-Name "RDPRule" `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 `
-Access Allow -Verbose
Write-Host "Creating a NSG rule named MSSQLRule...." -ForegroundColor DarkGreen -Verbose
$NSGRuleSQL = New-AzNetworkSecurityRuleConfig `
-Name "MSSQLRule" `
-Protocol Tcp `
-Direction Inbound `
-Priority 1001 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 1433 `
-Access Allow -Verbose

#Creating a NSG after defining NSGRules
Write-Host "Creating a NSG  named $NSGName...." -ForegroundColor DarkGreen -Verbose
$Nsg = New-AzNetworkSecurityGroup `
-Name $NSGName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-SecurityRules $NSGRuleRDP,$NSGRuleSQL `
-Verbose

#Creating a Network Interface
Write-Host "Creating a NIC  named $InterfaceName...." -ForegroundColor DarkGreen -Verbose
$Interface = New-AzNetworkInterface `
-Name $InterfaceName `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-SubnetId $Vnet.Subnets[0].Id `
-PublicIpAddressId $PublicIP.Id `
-NetworkSecurityGroupId $Nsg.Id `
-Verbose

#Compute
Write-Host "Creating a virtual machine named $VMName...." -ForegroundColor DarkGreen -Verbose
$VirtualMachine = New-AzVMConfig -VMName $VMName -VMSize $VmSize -Verbose
$Credential = Get-Credential -Message "Type the name and password of the local adminstrator account..."
$VirtualMachine = Set-AzVMOperatingSystem `
-VM $VirtualMachine `
-Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate

#Adding Network Interface
Write-Host "Adding a netwrok interface named  $Interface...." -ForegroundColor DarkGreen -Verbose
$VirtualMachine = Add-AzVMNetworkInterface `
-VM $VirtualMachine `
-Id $Interface.Id -Verbose
$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + ".vhd"
$VirtualMachine = Set-AzVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -Caching ReadOnly -CreateOption FromImage -Verbose

#Image
$VirtualMachine = Set-AzVMSourceImage -VM $VirtualMachine -PublisherName $PublisherName -Offer $OfferName -Skus $sku -Version $Version -Verbose

#Create the VM in azure
Write-Host "At last virtuala machine named $VMName created suceesffully...." -ForegroundColor DarkGreen -Verbose
New-AzVM `
-ResourceGroupName $ResourceGroupName `
-Location $Location `
-VM $VirtualMachine `
-Verbose

#Add the SQL IaaS extension, and choose the licene type
Write-Host "Created SQL Environment  suceesffully...." -ForegroundColor DarkGreen -Verbose
New-AzSqlVM `
-ResourceGroupName $ResourceGroupName `
-Name $VMName `
-Location $Location `
-LicenseType PAYG `
-Verbose

Comments

Popular posts from this blog

Configuring Always on setup in Microsoft Azure.

AZCopy-- How to copy files from Azure Storage Blob Container to on-premises local storage drive and folder