Solved

Use Powershell to spin up Azure MarketPlace SQL 2012 VM

Posted on 2016-09-15
5
64 Views
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!
0
Comment
Question by:K B
  • 2
  • 2
5 Comments
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
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.

Copy
$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.

Copy
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.

Copy
$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.

Copy
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.

Copy
$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.

Copy
$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.

Copy
$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.
0
 
LVL 5

Author Comment

by:K B
Comment Utility
Thank you for your reply.   However, as I mentioned I am looking for Resource Manager not classic
0
 
LVL 17

Expert Comment

by:Pawan Kumar Khowal
Comment Utility
0
 
LVL 5

Author Comment

by:K B
Comment Utility
I am not sure you are reading my question completely
0
 
LVL 39

Accepted Solution

by:
footech earned 500 total points
Comment Utility
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

0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Exchange server is not supported in any cloud-hosted platform (other than Azure with Azure Premium Storage).
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the process of installing of Data Protection Manager on a server running Windows Server 2012 R2, including the prerequisites. Microsoft .Net 3.5 is required. To install this feature, go to Server Manager…
Viewers will learn how the fundamental information of how to create a table.

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now