PowerShell Hyper-V management with Server 2012 #01

Hyper-v management with PowerShell is just beyond. The following script sets up a new VM, with user-defined RAM and HD sizes and virtual guest name. The XXXXX represents the host, not the virtual guest.A couple of disclaimers: WordPress has wrapped some of the lines, I’ve put an ! at the END of the wrapped lines, these will need to be removed and put back at the end of the preceding line (it might be me, but wordpress is really making my life hard copying stuff into it- I get paragraphs where I don’t need them, lines jumping onto preceding lines etc). Also, the line which attaches the floppy disk (last but one line) works in that it functions, but doesn’t actually perform any unattend function (or I can’t get it to, anyway) so the OS just boots onto the normal welcome screen.
This script might seem long, but you could do most of this in a couple of lines if (a) it wasn’t so heavily commented and (b) there were no variables. If you hard-coded everything, you’d actually only need the New-VM, Set-VMDvdDrive and Start-VM lines. although you’d have fun taking this route, because for some strange reason the disk size and RAM are measured in Bytes, hence all the * 1024 with the variables.
# equivalent to CLS- just wipes the screen
# sets up the following variables: $Multiplier is the value to increase other variables to get the right number of bytes; $RequiredRAM is the “friendly” amount of RAM; $RequiredDisk is the “friendly” disk size;!
# $ActualRAM and $ActualDisk are the prevoius 2 variables multiplied by $Multiplier
[int]$RequiredRAM = read-host -Prompt “Please enter the size of RAM you’d like in GB”
[int]$RequiredDisk = read-host -Prompt “Enter enter the size of the system disk you’d like in GB”
$ActualRAM = $Multiplier * $RequiredRAM
$ActualDisk = $Multiplier * $RequiredDisk
# $NewMachineName is set to the arbitrary value input by the end user;
$NewMachineName = read-host -Prompt “Please type the machine name”
# New-VM creates a new VM with $NewMachineName as its name & disk name, $ActualRAM as the amount of RAM and $ActualDisk as the disk size in bytes, and sets the machine to boot from DVD;!
New-Vm -Name $NewMachineName -MemoryStartupBytes $ActualRAM -NewVHDPath C:\Hyper-V\VHD\$NewMachineName.vhdx -NewVHDSizeBytes $ActualDisk -BootDevice CD -Path C:\Hyper-V -!ComputerName XXXXX!
# The next three lines: point the DVD drive at an ISO, point the floppy disk at a virtual floppy and start the machine up;!
Set-VMDvdDrive -VMName $NewMachineName -Path F:\ISO\Win_Svr_Std_and_DataCtr_2012.ISO -ComputerName XXXXX!
Set-VMFloppyDiskDrive -VMName $NewMachineName C:\Hyper-V\unattend.vfd -ComputerName XXXXX
Start-VM -Name $NewMachineName -ComputerName XXXXX

One thought on “PowerShell Hyper-V management with Server 2012 #01

  1. Pingback: PowerShell Hyper-V management with Server 2012 | Rhidian’s IT … « Data Protection & Availability

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s