[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V

Hello everyone, this is the third post of the series. .

 

Background

===============

In my solution, I have 15 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 be very bored and easy to make mistakes. I started using PowerShell and it really helped ease the pain.

 

What can this one do

===============

I need to create about 20 VMs, they have different settings, such as number of CPU, Memory amount, Virtual Disk Size, Location, etc. I don’t want to mannually configure them all in GUI. So…

This script will read a CSV file, and get the configuration for each VM, and then automatically create all the VMs based on your requirement.

As always, everything in one click!

 

Logic Intro

===============

Nothing special, just read CSV and create VMs one after another.

The hard part is 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-Csv D:\XYZABC\CreateVMs.csv -Header VMName, Processor, RAM, VHDSize, VMLocation, VirtualSwtich

Foreach ( $vm in $b )
{
    New-Item -Path $vm.VMLocation -ItemType directory -force -ErrorAction SilentlyContinue
    New-VM $vm.VMName -path $vm.VMLocation
    New-VHD -Path ("{0}/{1}/{1}.vhdx" -f $vm.VMLocation, $vm.VMName) -SizeBytes (Invoke-Expression $vm.VHDSize)
    Add-VMHardDiskDrive -VMName $vm.VMName -ControllerType SCSI -ControllerNumber 0 -path ("{0}/{1}/{1}.vhdx" -f $vm.VMLocation, $vm.VMName)
    Get-VM $vm.VMName | Set-VMMemory -DynamicMemoryEnabled 0 -StartupBytes (Invoke-Expression $vm.RAM)
    Get-VM $vm.VMName | Set-VMProcessor -count $vm.Processor
    Get-VM $vm.VMName | Add-VMNetworkAdapter -SwitchName $vm.VirtualSwtich
}
 

This is what the CSV look like.

 

Further

============

I know SCVMM should be better at this task. I will learn it, and post update on this topic as soon as I tuned how to make SCVMM able to do the same thing on a windows failover cluster.

 

Please forgive me for not listing all the articles that supported me on writing out this script. There are so many… …

时间: 2024-10-07 15:02:13

[PowerShell Utils] Create a list of virtual machines based on configuration read from a CSV file in Hyper-V的相关文章

[SQL in Azure] Getting Started with SQL Server in Azure Virtual Machines

This topic provides guidelines on how to sign up for SQL Server on a Azure virtual machine and how to get started creating SQL Server databases in Microsoft public cloud environment. With SQL Server in Azure Virtual Machines, you get the full benefit

使用Powershell在Microsoft Azure中创建Virtual Machine

获取虚拟机镜像 PS C:\WINDOWS\system32> Get-AzureVMImage 仅获得虚拟机名 PS C:\WINDOWS\system32> (Get-AzureVMImage).ImageName 0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essentials-20131018-enus 0c5c79005aae478e8883bf950a861ce0__Windows-Server-2012-Essent

PatentTips - Method for network interface sharing among multiple virtual machines

BACKGROUND Many computing systems include a network interface card (NIC) to provide for communications with other systems and devices over a network. In a computing system running multiple operating systems (OSs) on multiple virtual machines, each OS

Opennebula自定义VM 实现方法-Contextualizing Virtual Machines 2.2

from:http://archives.opennebula.org/documentation:archives:rel2.2:cong There are two contextualization mechanisms available in OpenNebula: the automatic IP assignment, and a more generic way to give any file and configuration parameters. You can use

PatentTips - Transparent unification of virtual machines

BACKGROUND Virtualization technology enables a single host computer running a virtual machine monitor ("VMM") to present multiple abstractions and/or views of the host, such that the underlying hardware of the host appears as one or more indepen

PatentTips - Method to manage memory in a platform with virtual machines

BACKGROUND INFORMATION Various mechanisms exist for managing memory in a virtual machine environment. A virtual machine platform typically executes an underlying layer of software called a virtual machine monitor (VMM) which hosts one to many operati

Methods and systems to control virtual machines

Methods and systems are provided to control the execution of a virtual machine (VM). A VM Monitor (VMM) accesses VM Control Structures (VMCS) indirectly through access instructions passed to a processor. In one embodiment, the access instructions inc

[转] Vmware vs Virtualbox vs KVM vs XEN: virtual machines performance comparison

http://www.ilsistemista.net/index.php/virtualization/1-virtual-machines-performance-comparison.html?limitstart=0 Today, "Virtual machine" seems to be a magic words in the computer industry. Why? Simply stated, this technology promise better serv

PatentTips - Enhancing the usability of virtual machines

BACKGROUND Virtualization technology enables a single host computer running a virtual machine monitor ("VMM") to present multiple abstractions and/or views of the host, such that the underlying hardware of the host appears as one or more indepen