Use Powershell to spin up Azure MarketPlace SQL 2012 VM

Posted on 2016-09-15
Medium Priority
Last Modified: 2016-09-18
Does anybody have a sample of the Powershell code that will do this or something close to this (i can spin up the OS-based VMs with Powershell already).

This would be using the new Resource Manager and not the classic.

Thank you!
Question by:K B
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41800866
Here are the details...

Step 1: Add your account
At the PowerShell prompt, type Add-AzureAccount and click Enter.
Type in the email address associated with your Azure subscription and click Continue.
Type in the password for your account.
Click Sign in.
Step 2: Set your subscription and storage account
Set your Azure subscription and storage account by running these commands at the Windows PowerShell command prompt. Replace everything within the quotes, including the < and > characters, with the correct names.

$subscr="<subscription name>"
$staccount="<storage account name>"
Select-AzureSubscription -SubscriptionName $subscr –Current
Set-AzureSubscription -SubscriptionName $subscr -CurrentStorageAccountName $staccount
You can get the correct subscription name from the SubscriptionName property of the output of the Get-AzureSubscription command. You can get the correct storage account name from the Label property of the output of the Get-AzureStorageAccount command after you run the Select-AzureSubscription command.

Step 3: Determine the ImageFamily
Next, you need to determine the ImageFamily or Label value for the specific image corresponding to the Azure virtual machine you want to create. You can get the list of available ImageFamily values with this command.

Get-AzureVMImage | select ImageFamily -Unique
Here are some examples of ImageFamily values for Windows-based computers:

Windows Server 2012 R2 Datacenter
Windows Server 2008 R2 SP1
Windows Server 2016 Technical Preview 4
SQL Server 2012 SP1 Enterprise on Windows Server 2012
If you find the image you are looking for, open a fresh instance of the text editor of your choice or the PowerShell Integrated Scripting Environment (ISE). Copy the following into the new text file or the PowerShell ISE, substituting the ImageFamily value.

$family="<ImageFamily value>"
$image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1
In some cases, the image name is in the Label property instead of the ImageFamily value. If you didn't find the image that you are looking for using the ImageFamily property, list the images by their Label property with this command.

Get-AzureVMImage | select Label -Unique
If you find the right image with this command, open a fresh instance of the text editor of your choice or the PowerShell ISE. Copy the following into the new text file or the PowerShell ISE, substituting the Label value.

$label="<Label value>"
$image = Get-AzureVMImage | where { $_.Label -eq $label } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1
Step 4: Build your command set
Build the rest of your command set by copying the appropriate set of blocks below into your new text file or the ISE and then filling in the variable values and removing the < and > characters. See the two examples at the end of this article for an idea of the final result.

Start your command set by choosing one of these two command blocks (required).

Option 1: Specify a virtual machine name and a size.

$vmname="<machine name>"
$vmsize="<Specify one: Small, Medium, Large, ExtraLarge, A5, A6, A7, A8, A9>"
$vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image
Option 2: Specify a name, size, and availability set name.

$vmname="<machine name>"
$vmsize="<Specify one: Small, Medium, Large, ExtraLarge, A5, A6, A7, A8, A9>"
$availset="<set name>"
$vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image -AvailabilitySetName $availset
For the InstanceSize values for D-, DS-, or G-series virtual machines, see Virtual Machine and Cloud Service Sizes for Azure.

Author Comment

by:K B
ID: 41800881
Thank you for your reply.   However, as I mentioned I am looking for Resource Manager not classic
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41800883

Author Comment

by:K B
ID: 41800885
I am not sure you are reading my question completely
LVL 40

Accepted Solution

footech earned 2000 total points
ID: 41802004
Haven't actually done it for SQL server, but based off just creating a standard Windows VM, I think this would work.
$localcred = Get-Credential -Message "Type the name and password of the local administrator account."
$vmName = "test-SQL"
$location = "westus"
$rgName = "RG1" #name of your Resource Group

# Configure the OS disk
$OSdiskName = "OSDisk"
# $storageAcc is a retrieved or created Storage Account
$osDiskUri = $storageAcc.PrimaryEndpoints.Blob.ToString() + "vhds/" + $vmName + $OSdiskName  + ".vhd"

# Configure VM info
$compName = $vmName

# choose a PublisherName from 
# Get-AzureRmVMImagePublisher -Location $location
$pubName = "MicrosoftSQLServer"

# choose an Offer from
# Get-AzureRmVMImageOffer -Location $location -PublisherName $pubName
$offerName = "SQL2014SP1-WS2012R2"

# choose a SKU from
# Get-AzureRmVMImageSku -Location $location -PublisherName $pubName -Offer $offerName
$skuName = "Standard"

New-AzureRmVMConfig -VMName $vmName -VMSize "Standard_A1" |
 Set-AzureRmVMOSDisk -Name $OSdiskName -VhdUri $osDiskUri -CreateOption FromImage |
 Set-AzureRmVMOperatingSystem -Windows -ComputerName $compName -Credential $localcred -ProvisionVMAgent -TimeZone "Pacific Standard Time" |
 Set-AzureRmVMSourceImage -PublisherName $pubName -Offer $offerName -Skus $skuName -Version "latest" |
 Add-AzureRmVMNetworkInterface -Id $nic.Id |
 New-AzureRmVM -ResourceGroupName $rgName -Location $location
# I haven't shown the creation of a NIC referenced by line 31
# also would probably want to change the VM size on line 27
# and add another disk to the config

Open in new window


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question