Hello Everyone, this is the third post of the series. .
Background
===============
In my solution, I has Windows hosts. I need to configure them from the OS installation to configure fail over cluster and then create and run VMs on them. Without scripting, manually complete related tasks would being very bored and easy to make mistakes. I started using PowerShell and it really helped ease the pain.
What can this one does
===============
I need to create about VMs, they has different settings, such as number of CPUs, Memory amount, Virtual Disk Size, Loca tion, etc. I don ' t want to mannually configure them all in GUI. So ...
This script would read a CSV file, and get the configuration for each VM, and then automatically create all the VMS based O N your requirement.
As always, everything in one click!
Logic Intro
===============
Nothing special, just read CSV and create a VMs one after another.
The hard part was the usage of each of the cmdlet involved. Took me most of the time.
Note the (invoke-expression $vairable. Member), this is special.
Script is here
===============
$b= Import-csvD:\XYZABC\CreateVMs.csv-headerVMName, Processor, RAM, vhdsize, vmlocation, VirtualswtichForeach($VMinch$b) {New-item-path$VM.Vmlocation-itemtypeDirectory-force-erroractionsilentlycontinueNEW-VM$VM.VMName-path$VM.VmlocationNew-vhd-path("{0}/{1}/{1}.VHDX"- F$VM.Vmlocation, $VM.VMName)-sizebytes(invoke-expression$VM.Vhdsize)add-vmharddiskdrive-vmname$VM.VMName-controllertypeSCSI-controllernumber0-path("{0}/{1}/{1}.VHDX"- F$VM.Vmlocation, $VM.VMName)GET-VM$VM.VMName| set-vmmemory-dynamicmemoryenabled0-startupbytes(invoke-expression$VM.RAM)GET-VM$VM.VMName| Set-vmprocessor-count$VM.ProcessorGET-VM$VM.VMName| Add-vmnetworkadapter-switchname$VM.Virtualswtich}
That's what the CSV look like.
Further
============
I know SCVMM should is better at the this task. I'll learn it, and post update on this topic as soon as I tuned how to make SCVMM able to does the same thing on a Windows Failover cluster.
Please forgive me for not listing all the articles, supported me on writing off this script. There is so many ...
[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V