Create Virtual Machine Through PowerShell

Requirement

  • Windows PowerShell ISE.
  • Azure Account.
  • No Proxy Internet Connection.

Step 1: Install PowerShell

Go to Below Links,

Follow the steps and select PowerShell from the downloader.

Step 2: Add your Azure Account to the local PowerShell Environment.

Open the PowerShell in administrator mode and type the below Script:

Add-AzureAccount

Script

This will ask you to sign into your Microsoft Azure Account.

Account

After signing in, you will get the list of all your subscriptions.

subscription list

Step 3: Set a default Azure Subscription

We are going to use 3f subscription from my account. You can choose from the subscriptions that you have and replace it with <”SubscriptionId”> in the script.

Select-AzureSubscription -SubscriptionId <”SubscriptionId”>

script

Before going ahead, you need to make sure that you already have created the Storage Account for the VM.


Step 4: Set Subscription and Storage Account

If you don’t have storage account, you will get an error like this.

error

Go to the following links to make a storage account 

Step 5: Determine the ImageFamily

Get Image Family list,

Get-AzureVMImage | select ImageFamily -Unique

list

Select Image Family to create VM, using the below script. (We are going to select “Windows Server 2008 R2 SP1”)

$family="Windows Server 2008 R2 SP1"
$image=Get-AzureVMImage | where { $_.ImageFamily -eq $family } | sort PublishedDate -Descending | select -ExpandProperty ImageName -First 1


script

Step 6: Build Your Command

By using this command, we are going to set VM Name, VM Size and Image Name

$vmname="VMByPowerShell"
$vmsize="Small"
$vm1=New-AzureVMConfig -Name $vmname -InstanceSize $vmsize -ImageName $image


command

Step 7: Set Credentials

In this command, you need to give user name and password for administrator profile for the VM.

$cred=Get-Credential -Message "Type the name and password of the local administrator account."
$vm1 | Add-AzureProvisioningConfig -Windows -AdminUsername $cred.Username -Password $cred.GetNetworkCredential().Password


script

It will prompt a popup window for user name and password.

popup

Step 8: Create the virtual machine in an existing cloud service

I have already created Empty Cloud Service. If you don’t know how to create one, just go here,
New-AzureVM -ServiceName "powershelldemo2" -VMs $vm1

script

script

It gives a warning because it’s an empty service.

Service

Now, you can see that it succeeded. On the portal you can see that Cloud Service and VM is created.

Cloud Service
Cloud Service

In Storage Account, you can see that Container is having “vhds” for the VM deployment. Vhds contains the Package and configuration files.

files
files

Now, you need to get port details and update the Port Endpoint.

Get Port Details

Get-AzureEndpoint -VM (Get-AzureVM -ServiceName "ServiceName" -Name "VMName") |
ft -AutoSize


Update Port Endpoint

Previous Endpoints are,

Endpoints

Set-AzureEndpoint -Name "RemoteDesktop" -PublicPort 443 -LocalPort 3389 -Protocol "tcp" |
Update-AzureVM


script

Now,

endpoint
You can also run these two scripts together at the same time.

Up Next
    Ebook Download
    View all
    Learn
    View all