Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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 32

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 32

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 41

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
This tutorial will walk an individual through the process of configuring basic necessities in order to use the 2010 version of Data Protection Manager. These include storage, agents, and protection jobs. Launch Data Protection Manager from the deskt…

604 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