Menu for Creating Template Copies in Windows 8 Client Hyper-V

While working with the Windows 8 Client Preview, I start experimenting with the alternatives to automate the Client Hyper-V. After a little while I build a script to allow a user to duplicate their Hyper-V Templates from PowerShell

[code language = ps]

<#
.Synopsis
Create VM from a template in your Windows 8 Hyper-V Client PC.
.DESCRIPTION
This script will create Virtual Machines from a template already created in your Windows 8 Client Hyper-V PC.
Follow the prompts. It will guide you thru the whole process.
.EXAMPLE
Create-New-VM.ps1
#>

#########################################
# Author: Carlos E. Vargas #
# Name: Create-New-VM.ps1 #
# Version: 1.0 #
# carlosvargas at hotmail dot com #
#########################################

# Import Hyper-V Module
Import-Module Hyper-v

# Create Variable for VM name
$TemplateLocation = ‘C:UsersPublicDocumentsHyper-VVirtual hard disks'

#Information for VM
Write-Host “`n`n”
$VMname = Read-Host -Prompt “What is the name for the VM”
#loop for network cards
$VMSwitches = Get-vmswitch | select Name,SwitchType | Sort-Object name
$VMswitchesCount = $VMSwitches.count
Write-Host “”
Write-Host “”
Write-Host “Select the Virtual Network Switch for your VM.”
For($i = 0;$i -le ($VMswitches.count – 1);$i++)
{
$x = $i + 1
Write-Host “$x.” $VMSwitches[$i].name “(“$VMSwitches[$i].SwitchType”)”
}

Write-Host””
$VMSwichSelection = Read-Host -Prompt “Select Option 1 – $VMSwitchesCount”
$VMNetworkCard = $VMSwitches[$VMSwichSelection – 1].Name
Write-Host $VMNetworkCard

#List VHDs
$VHDParent = Get-childitem $TemplateLocation -Recurse | Select Name, FullName, DirectoryName |Where-Object {$_.Name -like ‘*.vhd*'}
$VHDCount = $VHDParent.count

Write-Host “`n`n”
Write-Host “Select the Parent Virtual Hard Disk”
For($i = 0;$i -le ($VHDParent.count – 1);$i++)
{
$x = $i + 1
Write-Host “$x.” $VHDParent[$i].name “(“$VHDParent[$i].DirectoryName”)”
}

Write-Host””
$VHDSelection = Read-Host -Prompt “Select Option 1 – $VHDCount”
$VHDArrayIndex = $VHDSelection – 1
$VHDextension = [System.IO.Path]::GetExtension($VHDParent[$VHDArrayIndex].name)
# Use $VHDParent[$VHDSelection – 1] to call the array values
#Write-Host $VHDParent[$VHDSelection – 1].Name

# Create Location for VM
#New-Item $VMLocation -ItemType Directory
$loctrue = $false
do
{
Write-Host “”
$VMLocation = Read-Host -Prompt “Type the location for the Virtual Machine”
$title = “Location for Virtual Machine”
$message = “Is this the correct folder in the folder? $VMLocation”
$yes = New-Object System.Management.Automation.Host.ChoiceDescription “&Yes”, `
“Deletes all the files in the folder.”
$no = New-Object System.Management.Automation.Host.ChoiceDescription “&No”, `
“Retains all the files in the folder.”
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
$result = $host.ui.PromptForChoice($title, $message, $options, 0)

switch ($result)
{
0 {if ((Test-Path $VMLocation -PathType container) -eq $true )
{$loctrue = $true }
Else
{New-Item -itemtype directory -Path $VMLocation|out-null;$loctrue = $true}
}
1 {Write-Host “Type the path again”; $loctrue = $false}
}
}
until ($loctrue -eq $True)

#Create VHD For VM

$NewVHD = $VMLocation + “” + $VMname + “” + $VMname + $VHDextension

#Write-Host “Parent:” $VHDParent[$VHDArrayIndex].FullName “-Path” $NewVHD -Differencing

New-VHD -ParentPath $VHDParent[$VHDArrayIndex].FullName -Path $NewVHD -Differencing | out-null

#Create VM
New-VM -Name $VMName -MemoryStartUpByte 512MB -Path $VMLocation -BootDevice IDE -VHDPath $NewVHD | Out-Null

# Configure VM Dynamic Memory
Get-VM -VM $VMname | Set-VM -DynamicMemory | Out-Null

# Configure Network Card and Connect it to the Selected Network
Get-VM -VM $VMname | Get-VMNetworkAdapter | Connect-VMNetworkAdapter -SwitchName $VMNetworkCard | Out-Null

[/code]

Subscribe For Latest Updates

Sign up for best of digital marketing, livestream and technology opinions on what matters to you.

Invalid email address
We promise not to spam you. You can unsubscribe at any time.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: