• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Use Powershell to spin up Azure MarketPlace SQL 2012 VM

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!
  • 2
  • 2
1 Solution
Pawan KumarDatabase ExpertCommented:
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.
K BAuthor Commented:
Thank you for your reply.   However, as I mentioned I am looking for Resource Manager not classic
Pawan KumarDatabase ExpertCommented:
K BAuthor Commented:
I am not sure you are reading my question completely
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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Will You Be GDPR Compliant by 5/28/2018?

GDPR? That's a regulation for the European Union. But, if you collect data from customers or employees within the EU, then you need to know about GDPR and make sure your organization is compliant by May 2018. Check out our preparation checklist to make sure you're on track today!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now