Create VM's in Hyper-V using Powershell

Create VM's in Hyper-V using Powershell


Hola todos, Today I am gonna talk about a small thing i did in my LAB at home, I got a new windows 2012 server DC installed, created a small Virtual infrastructure using Hyper-V

Because i don't like manual work and I believe in the term " Work smart and party hard" I created a small script which will create all my VM's, VHD's and configure them all in a small code.

the code is stright forward, I mentioned few comments for those who wants to read them,


take a look at the code :

#The code start here,
#Normally Hyper-V module is already imported in your PS session everytime
#you run your PS and Hyper-V server or Hyper-V Managment tools is installed
#Reading a text file which contains the server names
$R = Get-Content -Path "F:\VMServers.txt"
foreach ($rr in $R){

#Creating folders for each Vm
# you can create a case or if dstatment here to make sure that this folder
#doesn't exixts, but becuse this is normally a one time process or new infrastructure thing, checking is not required
# force option will overwrite any exissted folder, make sure you check the path
# before running the code.
new-item -itemtype directory -path "f:\VMS\$rr" -Force



#| Mount-VHD -Passthru |Initialize-Disk -Passthru | New-Partition -AssignDriveLetter -UseMaximumSize |Format-Volume -FileSystem NTFS -Confirm:$false -Force

# Creating and modifying VM's
$vmpath = "F:\VMS\$rr\"
New-VM -MemoryStartupBytes 2GB -Name $rr -Generation 2 -Path $vmpath

# you can comment this line if you don't wanna use dynamic memory
Set-VM -Name $rr -DynamicMemory -MemoryMinimumBytes 512MB -MemoryMaximumBytes 2GB

# Creating VHDX's for vm's
$vhdpath = "F:\VMS\$rr\$rr\Virtual Machines\$rr.vhdx"
$vhdsize = 200GB
New-VHD -Path $vhdpath -Dynamic -SizeBytes $vhdsize

#assign each vhdx to it's vm
Add-VMHardDiskDrive -VMName $rr -Path $vhdpath

#Create VNIC's and assign them to VM's
Add-VMNetworkAdapter -VMName $rr -SwitchName "Qualcomm Atheros AR9485 Wireless Network Adapter - Virtual Switch"
Add-VMNetworkAdapter -VMName $rr -SwitchName "HVINTSwich"

#create Virtual DVD drive

Add-VMDvdDrive -VMName $rr

#Code End Here.







Comments

Popular posts from this blog

IP Calculator in PowerShell...with IP exclusion