Wednesday, May 1, 2013

Simple Powershell script to create a hyper-v VM

This has now been added to pshvm.codeplex.com a free powershell hyper-v manager project.

Updated 6/25/2013

This script will ask you questions and then builds the VM per your spec's / answers, unlike the one below this one will create a Fixed (Static) or Differencing (Dynamic) VHDx disk

$vswitch = get-vmswitch
$VPATH = get-vmhost
$VPATH | select virtualharddiskpath,virtualmachinepath | format-table -autosize
Write-Host -foreground "magenta" "This script will walk you through creating a new VM with a Fixed or  Dynamic Hard Disk"
Write-Host -foreground "magenta" "!!! If you do not see any paths listed above, this process will fail !!!"

#Ask for input
Write-Host -foreground "magenta" "-----------------------------------------------------------------"
$SelectedIndex = Read-Host "Type 0 to exit out of this Menu or Enter to Continue”
$Exit = $SelectedIndex

if($EXIT -eq 0)
{exit}
else {
Write-Host -foreground "magenta" "Type in what is asked for: DO NOT USE ANY Quotes to frame answers"
$SelectedIndex1 = Read-Host "New VM Name (TEST1)"
$SelectedIndex2 = Read-Host "RAM in GB (2)"
$SelectedIndex3 = Read-Host "vCPU Count (4)"
$SelectedIndex6 = Read-Host "Virtual hard Disk Size (70GB)"
$SelectedIndex7 = Read-Host "OS ISO file path (C:\iso\2008r2sp1.iso)"
Write-Host -foreground "magenta" ""

$1 = "\"
$2 = ".vhdx"
$3 = "\"
$NAME = $SelectedIndex1
$Name1 = $Name
$RAM = [int64]$SelectedIndex2 * 1073741824
$vCPU = $SelectedIndex3
$PATHVM1 = ($VPATH.virtualmachinepath)
$PATHVM = ($PATHVM1 += $1 += $NAME)
$newVHDPATH1 = ($VPATH.virtualharddiskpath)
$newVHDPATH = ($newVHDPATH1 += $1 += $3 +=$NAME1 += $2)
$newVHDsize = [int64]$SelectedIndex6 * 1073741824
$PATHDVD = $SelectedIndex7

#Ask for disk type selection
Write-host -foreground "magenta" "1. Fixed = Static"
Write-host -foreground "magenta" "2. Differencing = Dynamic"
Write-host -foreground "yellow" "NOTE: this step can take several minutes if you choose Fixed"
$SelectedIndex9 = Read-Host "**** Please select a Number For type of VHDx Disk ***"
$Selection = $SelectedIndex9
if($Selection -eq 1)
{New-VHD -Fixed -Path $newVHDPATH -SizeBytes $newVHDsize}
elseif ($Selection -eq 2)
{New-VHD -Path $newVHDPATH -SizeBytes $newVHDsize}

# create numbered array of vswitches
$i = 1
$vswitch | ForEach-Object {
                Write-Host "$i $($_.Name)”
                $i++
}

#Ask for number selection
Write-Host -foreground "magenta" ""
$SelectedVMIndex = Read-Host "**** Please select a Number to attach the VM too ***”
$SelectedVMIndex = $SelectedVMIndex - 1
$SelectedVM = $vswitch[$SelectedVMIndex]
$vSwitch = $SelectedVM.name

Write-Host -foreground "magenta" ""
Write-Host -foreground "magenta" "**** Input 1=Start-vm or Press Enter to Leave VM powered off ****"
$SelectedIndex8 = Read-Host "Type Selection”
$Power = $SelectedIndex8

NEW-VM –Name $NAME –MemoryStartupBytes $RAM -Path $PATHVM
#New-VHD -Path $newVHDPATH $Disktype -SizeBytes $newVHDsize
Add-VMHardDiskDrive -VMName $NAME -Path $newVHDPATH
Set-VMProcessor $NAME -Count $vCPU
get-vmnetworkadapter -vmname $NAME | connect-vmnetworkadapter -switchname $vSwitch
Set-VMBios $NAME -EnableNumlock
Set-VMDvdDrive -VMName $NAME -ControllerNumber 1 -ControllerLocation 0 –Path $PATHDVD

if($SelectedIndex8 -eq 1)
{start-vm "$Name"
cls
Write-Host -foreground "magenta" "**** Virtual Machine $Name Status *****"
Write-Host -foreground "Yellow" "!!!! It shows $Name Status twice, the bottom one is the true STATE !!!!"
get-vm "$Name"}
else {
cls
Write-Host -foreground "magenta" "**** Virtual Machine $Name is ready to be started ****"
get-vm "$Name"}
}

Updated 6/14/2013

This script will ask you questions and then build the VM per your spec's / answers however it only build a Differncing (dynamic) VHDx file

$vswitch = get-vmswitch
$VPATH = get-vmhost
$VPATH | select virtualharddiskpath,virtualmachinepath | format-table -autosize
Write-Host -foreground "magenta" "This script will walk you through creating a new VM with a Dynamic Hard Disk"
Write-Host -foreground "magenta" "!!! If you do not see any paths listed above, this process will fail !!!"

#Ask for input
Write-Host -foreground "magenta" "-----------------------------------------------------------------"
$SelectedIndex = Read-Host "Type 0 to exit out of this Menu or Enter to Continue”
$Exit = $SelectedIndex


if($EXIT -eq 0)
{exit}
else {
Write-Host -foreground "magenta" "Type in what is asked for: DO NOT USE ANY Quotes to frame answers"
$SelectedIndex1 = Read-Host "New VM Name (TEST1)"
$SelectedIndex2 = Read-Host "RAM in GB (2)"
$SelectedIndex3 = Read-Host "vCPU Count (4)"
$SelectedIndex6 = Read-Host "Virtual hard Disk Size in GB (70)"
$SelectedIndex7 = Read-Host "OS ISO file path (C:\iso\2008r2sp1.iso)"
Write-Host -foreground "magenta" ""

$1 = "\"
$2 = ".vhdx"
$3 = "\"
$NAME = $SelectedIndex1
$Name1 = $Name
$RAM = [int64]$SelectedIndex2 * 1073741824
$vCPU = $SelectedIndex3
$PATHVM1 = ($VPATH.virtualmachinepath)
$PATHVM = ($PATHVM1 += $1 += $NAME)
$newVHDPATH1 = ($VPATH.virtualharddiskpath)
$newVHDPATH = ($newVHDPATH1 += $1 += $3 +=$NAME1 += $2)
$newVHDsize = [int64]$SelectedIndex6 * 1073741824
$PATHDVD = $SelectedIndex7

# reate numbered array of vswitches
$i = 1
$vswitch | ForEach-Object {
                Write-Host "$i $($_.Name)”
                $i++
}

#Ask for number selection
Write-Host -foreground "magenta" ""
$SelectedVMIndex = Read-Host "**** Please select a Number to attach theVM too ***”
$SelectedVMIndex = $SelectedVMIndex - 1
$SelectedVM = $vswitch[$SelectedVMIndex]
$vSwitch = $SelectedVM.name

Write-Host -foreground "magenta" ""
Write-Host -foreground "magenta" "**** Input 1=Start-vm or Press Enter to Leave VM powered off ****"
$SelectedIndex8 = Read-Host "Type Selection”
$Power = $SelectedIndex8

NEW-VM –Name $NAME –MemoryStartupBytes $RAM -Path $PATHVM –newVHDPath $newVHDPATH -newVHDsizebytes $newVHDsize
Set-VMProcessor $NAME -Count $vCPU
get-vmnetworkadapter -vmname $NAME | connect-vmnetworkadapter -switchname $vSwitch
Set-VMBios $NAME -EnableNumlock
Set-VMDvdDrive -VMName $NAME -ControllerNumber 1 -ControllerLocation 0 –Path $PATHDVD

if($SelectedIndex8 -eq 1)
{start-vm "$name"
get-vm "$name"
Write-Host -foreground "magenta" "!!!! It shows the same server twice but the bottom one is the true state !!!!" }
else {
Write-Host -foreground "magenta" "**** VM is ready to be started ****"
Write-Host -foreground "magenta" ""}

}

The above } is the end of the script

This method works the same but you have to edit file each time you want to create a VM.

I created this in notepad and named it addvm.ps1 and saved it to C:\software
To run this script open powershell

type cd\
type cd software
type .\addvm.ps1

Simple Powershell script to create a VM with a existing VHDX disk file

# Variables
# Name of server in hyper-v
$NAME = "test"
# Amount RAM in GB
$RAM = 4GB
# Path to store VM config files
$PATH = "C:\VirtualServers\test"
# Path to an existing VM Harddrive file
$VHDPATH = "C:\VirtualServers\test\2008r2-70gb.vhdx"
# Number vCPU's
$CPU = 4
# Name of Virtual Switch for first NIC
$NETWORK1 = "prod network"
# Un rem below for name of  virtual switch for second NIC
# $NETWORK2 =
# Set-VMBios = Num lock .... enabled = -EnableNumlock or disabled = -DisableNumLock

New-VM –Name $NAME –MemoryStartupBytes $RAM -Path $PATH –VHDPath $VHDPATH
Set-VMProcessor $NAME -Count $CPU
get-vmnetworkadapter -vmname $NAME | connect-vmnetworkadapter -switchname $NETWORK1
Set-VMBios $NAME -EnableNumlock




Simple Powershell script to create a VM with a new VHDX file and attach an ISO for OS installation
Note this will be a dynamic harddisk, if you wish to change this read below**
# Variables
## Name of server in hyper-v
$NAME = "test1"
## Amount RAM in GB
$RAM = 4GB
## Number vCPU's
$CPU = 4
## Path to store VM config files
$PATH = "C:\VirtualServers\test1"
## Path to the new VHDX VM hard drive file
$newVHDPATH = "C:\VirtualServers\test1\2008r2-70gb.vhdx"
## Size to make the VM hard drive
$newVHDsize =70GB
## Path to the DVD ISO for OS installation
$PATHDVD = C:\ISO\2008r2sp1.iso
## Name of Virtual Switch for first NIC
$NETWORK1 = "prod network"
# Un rem below for name of  virtual switch for second NIC
# $NETWORK2 =
## Set-VMBios = Num lock .... enabled = -EnableNumlock or disabled = -DisableNumLock

New-VM –Name $NAME –MemoryStartupBytes $RAM -Path $PATH –newVHDPath $newVHDPATH -newVHDsizebytes $newVHDsize
Set-VMProcessor $NAME -Count $CPU
get-vmnetworkadapter -vmname $NAME | connect-vmnetworkadapter -switchname $NETWORK1
Set-VMBios $NAME -EnableNumlock
Set-VMDvdDrive -VMName $NAME –Path $PATHDVD -ControllerNumber 1


**

Here are your steps to converta dynamic harddisk vhdx file toa fixed or static sized vhdx file:
1. Power the VM Server off if it is running
2. From the server console, open the powershell window, type (substitute C:\temp\ for your path)
PS C:\> Convert-VHD –Path c:\test\test.vhdx –DestinationPath c:\test\test1.vhdx -VHDType Fixed
This will convert your dynamic VHDX file and make it Fixed or Static VHDX file. The -VHDType options are Fixed, Dynamic and Differencing.
3. in powershell (removes the path to the old dynamic vhdx)
Remove-VMHardDiskDrive -VMName Test1 –ControllerType IDE -ControllerNumber 0 -ControllerLocation 0
4. In powershell (adds the path to the new Fixed or Static sized vhdx)
Add-VMHardDiskDrive -VMName Test1 -Path C:\test\test1.vhdx

 your are VM is ready to ps C:\>start-vm test1.

You might want to delete the old VHDX file unless you are not done with it unless you like to waste disk space.

If you are building a VM from scratch. (IE using the DVD ISO method) you will need to eject the OS ISO DVD once it is installed and insert the MS Integration Tools ISO to install them.

This is how you do that in Powershell:
To eject the current DVD/ISO file type
Set-VMDvdDrive -VMName Test1 -ControllerNumber 1 -ControllerLocation 0 -Path $null

To insert the MS Integration Tools ISO type
Set-VMDvdDrive -VMName Test1 -Path C:\Windows\system32\vmguest.iso

To eject the MS Integration Tools ISO once intsalled type 
Set-VMDvdDrive -VMName Test1 -ControllerNumber 1 -ControllerLocation 0 -Path $null

No comments:

Post a Comment