How to batch create virtual machines in Windows Server 2012(PowerShell)

Check Video

http://gallery.technet.microsoft.com/scriptcenter/How-to-batch-create-9efd1811#content

Introduction

This PowerShell script illustrates how to batch create multiple virtual machines based on comma delimited file by using PowerShell 3.0 in Windows Server 2012

Scenarios

IT admin requires to batch creating virtual machines in Windows Server 2012, although they can use few commands due to the lack of programming knowledge. Although it’s a set of Hyper-V command-lets within Windows PowerShell, IT Admins are reluctant to use them except simple a command which is widely used.

Script

Step1: First, we need to prepare the comma delimited CSV file. It contains valuable list of information about the virtual machines. The detailed information is given in the following table:

VHDFolder : Specifies the path of virtual hard disk folder.

VHDName : Specifies the name of the new virtual hard disk. (such as “Windows Server 2012.vhdx” or “Windows Server 2012.vhd”)

VHDParentPath : Specifies the path to the parent of the differencing disk to be created

VMFolder : Specifies the path of virtual machine folder.

VMName : Specifies the name of the new virtual machine.

MemoryStartupBytes : Specifies the amount of memory, in Megabytes, to assign to the virtual machine.

VMSwitchName : Specifies the name of the virtual switch if you want to connect the new virtual machine to an existing virtual switch to provide connectivity to a network. (Note:if we do not have any virtual switch to configure, we can leave this parameter blank.)

Step 2: Run the script in the Windows PowerShell Console, type the command: Import-Module <Script Path> at the prompt.

For example, type Import-Module E:\BatchCreateVirtualMachines.psm1

This is shown in the following figure:

Step 3: We can type the command Get-Help New-OSCSVirtualMachine to display the entire help file for this function, such as the syntax, parameters, or examples.

Step 4:  To create the virtual machines run the command New-OSCVirtualMachine  –CsvFilePath <path name>

Finally, when the script completes, it will return a report in the current Windows PowerShell Console. The report contains details of the virtual machines created.

Now, in Hyper-V Manager, you will see the virtual machines have been created.

Here are some code snippets for your references. To get the complete script sample, please click the download button at the beginning of this page.

PowerShell
if($isExisted -eq $null)  
{ 
    Write-Warning "Can not find virtual switch : $VMSwitchName, because the virtual switch does not exist." 
} 
else 
{ 
    #creates a virtual hard disk  
    New-VHD -Path $VHDPath -ParentPath $VHDParentPath -Differencing|Out-Null 

    #creates a virtual machine. 
    New-VM -Path $VMFolder -Name $VMName -VHDPath $VHDPath -MemoryStartupBytes $(Invoke-Expression -Command $MemoryStartupBytes)|Out-Null 

    #connects a virtual network adapter to a virtual switch 
    Connect-VMNetworkAdapter -VMName $VMName -SwitchName $VMSwitchName 
}

Prerequisite

Windows Server 2012

Windows PowerShell 3.0

Hyper-V 3.0

Advertisements
Posted in Hyper-V

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: