Kasraeian

vWorld v.3

PowerShell & Hyper-V

In past few days I worked with Microsoft Hyper-V and PowerShell as part of my study for updating my certificate and during this study time, I made few simple scripts which can be used in lab.

Mass linked VHD/VHDX creator

This script will create multiple VHD/VHDX file based on provided info of currently existed “Differencing” VHD/VHDX file; so later they can be attached to the VMs and so on.
[crayon title=”Mass linked VHD/VHDX (Ver 0.1)”]# Created by: Sohrab Kasraeian Fard

Mass linked VHD/VHDX (Ver 0.1)

CLS
$vhdpath = Read-Host “Provide parent VHD/VHDX file path”
$vhddest = Read-Host “Where should new VHD/VHDX saved”
$vhdnasc = Read-Host “Provide naming schema for new VHD/VHDX”
$vhdtype = Read-Host “VHD or VHDX”
$vhdqty = Read-Host “How many VHD/VHDX should be created/linked”
$target = “”
for ($i=1; $i -le $vhdqty; $i++) {
$target = “$vhddest” + “$vhdnasc” + “$i” + “.” + “$vhdtype”
New-VHD -ParentPath $vhdpath -Differencing -Path $target
}[/crayon]

Adding multiple virtual switches

This script will get naming schema and will build multiple virtual switches based on that
[crayon title=”Mass vSwitch Creator (Ver 0.1)”]# Created by: Sohrab Kasraeian Fard

Mass vSwitch Creator (Ver 0.1)

CLS
$vSwitchType = “Private” #Possible type are Private,External,Internal
$Note = “Test Only vSwitches” #Enter any note for vSwitch
$Name = “vSw01″,”vSw02″,”vSw03″,”vSw04″,”vSw05″,”vSw06” #List new virtual switches
$Name | %{New-VMSwitch -Name $_ -SwitchType $vSwitchType -Notes $Note -Confirm:$false}[/crayon]

Adding multiple VMs

This script will help to create multiple VMs by getting some basic info, creating “Differencing” VHD/VHDX based on pre-created/pre-configured OS, adding them to VMs and configuring them.
[crayon title=”Mass Linked VMs (Ver 0.1)”]# Created by: Sohrab Kasraeian Fard

CLS
Write-Host “Mass Linked VMs (Ver 0.1)nThis script would create new linked VMs for lab usage.n”

$VMNameTemp = Read-Host “Specify VM name template”
$VMQty = Read-Host “How many VMs should be created”

Get-VMSwitch | select Name,SwitchType
$vSwitch = Read-Host “`nWhich vSwitch new VMs should connect to (full or part of vSwitch name)”
$Switch = Get-VMSwitch | where {$_.name -like “*$vSwitch*”}
$Switch

$VHDpath = Read-Host “`nProvide parent vhd file path”
$VHDdest = Read-Host “Where should new vhd saved”
$VHDtype = Read-Host “vhd or vhdx”
$target = “”
for ($i=1; $i -le $VMQty; $i++) {
$target = “$VHDdest” + “$VMNameTemp” + “$i” + “.” + “$VHDtype”
New-VHD -ParentPath $VHDpath -Differencing -Path $target
}

for ($i=1; $i -le $VMQty; $i++) {
$Name = “$VMNameTemp” + “$i”
$VHDP = “$VHDdest” + “$VMNameTemp” + “$i” + “.” + “$VHDtype”
$VMCD = Get-date -DisplayHint DateTime
New-VM -Name $Name -VHDPath $VHDP -MemoryStartupBytes 256MB -SwitchName $Switch.Name -BootDevice IDE | Set-VM -ProcessorCount 2 -DynamicMemory:$true -Notes “$Name created on $VMCD” -MemoryMinimumBytes 384MB -MemoryMaximumBytes 512MB -MemoryStartupBytes 512MB
Write-Host “`n”
}[/crayon]

[crayon title=”Mass Linked VMs (Ver 0.2)”]# Created by: Sohrab Kasraeian Fard

CLS
Write-Host “Mass Linked VMs (Ver 0.2)nThis script would create new linked VMs for lab usagen”

############# Basic info #############
$VMNameTemp = Read-Host “Specify VM name template”
$VMQty = Read-Host “How many VMs should be created”

############# Virtual Switch Selection #############
Get-VMSwitch | select Name,SwitchType
$vSwitch = Read-Host “nWhich vSwitch new VMs should connect to (full or part of vSwitch name)"
$Switch = Get-VMSwitch | where {$_.name -like "*$vSwitch*"}
Write-Host "
“$($Switch.Name)" has been selected as target virtual switchn”

############# VHD/VHDX Config #############
#$VHDpath = Read-Host “`nProvide parent vhd file path”                                               # like:   F:\MS vLab\Base\LIBRARY.vhdx
$VHDpath = “F:\MS vLab\Base\LIBRARY.vhdx”                  # “Provide path to VHD/VHDX file, like:   F:\MS vLab\Base\LIBRARY.vhdx”
#$VHDdest = Read-Host “Where should new vhd saved”                                                   # like:   F:\MS vLab\Test\
$VHDdest = “F:\MS vLab\Test\”                              # “Provide path for saving new VHD/VHDX, like:   F:\MS vLab\Test\”
$PD = Get-VHD $VHDpath
if ($PD.VhdFormat -eq “VHDX”) {
Write-Host “Parent disk is in VHDX format, the child could be VHDX”
$VHDtype = “vhdx”
} elseif ($pd.VhdFormat -eq “VHD”) {
Write-Host “Parent disk is in VHDX format, the child could be VHD”
$VHDtype = “vhd”
}

$target = “”
for ($i=1; $i -le $VMQty; $i++) {
$target = “$VHDdest” + “$VMNameTemp” + “$i” + “.” + “$VHDtype”
$dskrst = New-VHD -ParentPath $VHDpath -Differencing -Path $target
if ($dskrst) {
Write-Host “New Disk "$("$VMNameTemp" + "$i" + "." + "$VHDtype")” has been created”
} else {
Write-Host “No new disk has been created!”
}
}

############# VM Config would be applied after creation #############
$CPUQty = Read-Host “nHow many vCPU VMs should have"
#$CPUQty = 2
$MinMemB = 256MB # MemoryMinimumBytes in MB/GB
$MaxMemB = 384MB # MemoryMaximumBytes in MB/GB
$SupMemB = 256MB # MemoryStartupBytes in MB/GB
$Note = Read-Host "Provide note for VMs (
“VMCD`” would add date of VM creation as note)”
#$Note = “$Name created on $VMCD” #It’s possible to replace this note with static note for all VMs

Write-Host “”
for ($i=1; $i -le $VMQty; $i++) {
$Name = “$VMNameTemp” + “$i”
$VHDP = “$VHDdest” + “$VMNameTemp” + “$i” + “.” + “$VHDtype”

$result = New-VM -Name $Name -VHDPath $VHDP -MemoryStartupBytes $SupMemB -SwitchName $Switch.Name -BootDevice IDE
if ($result) {
Write-Host “New VM "$($result.Name)” is created successfuly.”
if ($Note -eq “VMCD”) {
$VMCD = Get-date -DisplayHint DateTime
$Note = “$($VMCD)”
}
Set-VM -Name $result.Name -ProcessorCount $CPUQty -DynamicMemory:$true -Notes $Note -MemoryMinimumBytes $MinMemB -MemoryMaximumBytes $MaxMemB -MemoryStartupBytes $SupMemB
} else {
Write-Host “No VM has been created!”
}
}[/crayon]

I’m working on “Mass vSwitch Creator (Ver 0.1)” to make it easier and more friendly and in the end, the update would be posted here.

Download

Mass linked VHD-VHDX (Ver 0.1)
Mass vSwitch Creator (Ver 0.1)
Mass Linked VMs (Ver 0.1)
Mass Linked VMs (Ver 0.2)

Screenshots

Below are four screenshots from “Mass Linked VMs (Ver 0.2)” script

PS&HV-01-01 PS&HV-01-02 PS&HV-01-03 PS&HV-01-04

It's your kindness to leave a reply/feedback

Kasraeian © 2016
%d bloggers like this: