Blogger Templates

பல இணைய தளங்களிலிருந்து சேகரித்தவைகளை பலருக்கும் பயனாக அமையும் என்ற நோக்கம் கருதி இந்த - Blogல் பதிந்துள்ளேன்

- பொன்.சுதாகர்

Friday, January 6, 2017

Code to create Virtual Machine in Powershell



Procedure:
  • Create New Virtual Machine
  • Create Virtual Hard Disk
  • Attach Virtual Hard Disk in the Virtual Machine
  • Connect  Network Adapter in Virtual Machine 
  • Specify ISO to boot the VM

CREATE NEW VIRTUAL MACHINE

Let's go to create the new virtual machine. Type
new-vm -vmname test -memorystartupbytes 1GB

CREATE VIRTUAL HARD DRIVE

new-vhd -path "C:\users\Public\Documents\Hyper-v\Virtual Hard Disks\test.vhdx" -sizebytes 20GB -fixed
Use the -fixed to determine that your Virtual Hard Drive will be fixed. Because by default use Dynamic Virtual Hard Disk Type.
After create the Virtual Hard Disk you will see the results to verify the type of your VHD.

ATTACH VIRTUAL HARD DRIVE IN THE VIRTUAL MACHINE

add-vmharddrive -vmname test -path "C:\users\Public\Documents\Hyper-v\Virtual Hard Disks\test.vhdx" -controllertype IDE -controllernumber 0 -location 1

CONNECT NETWORK ADAPTER IN VIRTUAL MACHINE

Find the name of the Network Adapter for the new Virtual Machine
get-vmnetworkadapter -vmname test
Identify the name of the Virtual Switch from HYPER-V Host  that will be connect.
get-vmswitch
Connect the network adapter of the new Virtual Machine with the Virtual Switch. So now your Virtual Machine is connected in the network that use the Virtual Switch.
connect-vmnetworkadapter -vmname test -vmnetworkadaptername "network adapter" -switchname  "new virtual switch".
Verify the connection with 
get-vmnetworkadapter -vmname test

SPECIFY ISO TO BOOT THE VM

set-vmdvddrive -vmname test -controlnumber 1 -path "C:\users\user\Documents\ISO\Windows8.1 Professional_w_SP1_x64.ISO"
Start the VM to install the Windows
start-vm -vmname test

This article explain only basic commands how can create a new Virtual Machine and configure it. You can find lot of powershell commands for HYPER-V in 

Source Code:
PS C:\Windows\system32> Login-AzureRmAccount

PS C:\Windows\system32> $location = "southindia"

PS C:\Windows\system32> $RG = "rguser26"

PS C:\Windows\system32> New-AzureRmResourceGroup -Name $RG -Location $location

PS C:\Windows\system32> $st = "stuser26"

PS C:\Windows\system32> $st = New-AzureRmStorageAccount -ResourceGroupName $RG -Name $st -SkuName Standard_LRS -Location $location -Kind Storage

PS C:\Windows\system32> $sn = New-AzureRmVirtualNetworkSubnetConfig -Name "subneto1" -AddressPrefix 10.16.150.0/25

PS C:\Windows\system32> $vnet = New-AzureRmVirtualNetwork -Name "vnetuser26" -ResourceGroupName $RG -Location $location -AddressPrefix 10.16.150.0/24 -Subnet $sn

PS C:\Windows\system32> $publicip = New-AzureRmPublicIpAddress -Name "mypublicip" -ResourceGroupName $RG -Location $location -AllocationMethod Dynamic

PS C:\Windows\system32> $ni = New-AzureRmNetworkInterface -Name "myNIC" -ResourceGroupName $RG `
-Location $location -SubnetId $vnet.Subnets[0].Id -PublicIpAddressId $publicip.Id

PS C:\Windows\system32> $cred = Get-Credential

PS C:\Windows\system32> $vm = New-AzureRmVMConfig -VMName "vmuser26" -VMSize "Standard_A1"

PS C:\Windows\system32> $vm = set-azurermvmoperatingsystem -VM $vm -Windows -ComputerName "Sutha" -Credential $cred -ProvisionVMAgent -EnableAutoUpdate

PS C:\Windows\system32> $vm = Set-AzureRmVMSourceImage -VM $vm -PublisherName "MicrosoftWindowsServer" -Offer "WindowsServer" -Skus "2012-R2-Datacenter" -Version "latest"

PS C:\Windows\system32> $vm = Add-AzureRmVMNetworkInterface -VM $vm -Id $ni.Id

PS C:\Windows\system32> $blobPath = "vhds/myOsDisk1.vhd" $osDiskUri = $st.PrimaryEndpoints.Blob.ToString() + $blobPath

PS C:\Windows\system32> $vm = Set-AzureRmVMOSDisk -VM $vm -Name "myOsDisk1" -VhdUri $osDiskUri -CreateOption fromImage

PS C:\Windows\system32> New-Azurermvm -ResourceGroupName $RG -Location $location -VM $vm 
Share:

என்னை பற்றி...

My photo
Am Working as Assistant Professor in the Department of Information Technology at Sri Sairam Institute of Technology, Chennai.

முக்கிய செய்திகள்...

நாள்காட்டி

வானிலை



IP and Flag Counter

Wikipedia

Search results

QR Code Generator

All Conversion Widget

Recent Posts

Copyright © பொன். சுதாகர் | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Bloggertemplates4u