Hi, Powershell script to create a VM after prompting the user for various settings. The script then attaches an ISO which is a boot media for an MDT server that is built tp push a particular task sequence resulting in a failry automated VM deployment. I normally do this type of stuff with VMM etc but the client only has Hyper-V and wanted it automated as much as possible.

 

 

 

$VMName = Read-Host "Enter VM Name (Press [Enter] to choose Server01)"
if ($VMName -eq "")
    {
    $VMName="Server01"
    } ;
if ($VMName -eq $NULL)
    {
    $VMName="Server01"
    }

$StartupMem = Read-Host "Enter VM Startup Memory in GB’s (Press [Enter] to choose 1GB)"
if ($StartupMem -eq "")
    {
    $StartupMem=1024MB
    } ;
if ($StartupMem -eq $NULL)
    {
    $StartupMem=1024MB
    }
if ($StartupMem -ne 1024MB)
    {
    $StartupMem=[int]$StartupMem * 1073741824
    }

$MinimumMem = Read-Host "Enter VM Minimum Memory in GB’s (Press [Enter] to choose 512MB)"
if ($MinimumMem -eq "")
    {
    $MinimumMem=512MB
    } ;
if ($MinimumMem -eq $NULL)
    {
    $MinimumMem=512MB
    }
if ($MinimumMem -ne "512MB")
    {
    $MinimumMem=[int]$MinimumMem * 1073741824
    }

$MaximumMem = Read-Host "Enter VM Maximum Memory in GB’s (Press [Enter] to choose 4GB)"
if ($MaximumMem -eq "")
    {
    $MaximumMem=4096MB
    } ;
if ($MaximumMem -eq $NULL)
    {
    $MaximumMem=4096MB
    }
if ($MaximumMem -ne 4096MB)
    {
    $MaximumMem=[int]$MaximumMem * 1073741824
    }

do
    {
    $Storage = Read-Host "Which storage do you want, SATA ‘D’ or SSD ‘H’ (Press [Enter] to choose ‘D’)"
    if ($Storage -eq "")
        {
        $Storage="D"
        } ;
    if ($Storage -eq $NULL)
        {
        $Storage="D"
        }
    if (($Storage -ne "D") -and ($Storage -ne "H"))
        {
        Write-Host "Incorrect valude entered, please enter ‘D’ or ‘H’"
        }
    }
until ($Storage -eq "D" -or $storage -eq "H")
$VDisk1 = Read-Host "Enter VM System (C:) Hard Drive in GB’s (Press [Enter] to choose 60GB)"
if ($VDisk1 -eq "")
    {
    $VDisk1=60GB} ; if ($VDisk1 -eq $NULL){$VDisk1=60GB
    }
if ($VDisk1 -ne 60GB)
    {
    $VDisk1=[int]$VDisk1 * 1073741824
    }

$VDisk2 = Read-Host "Enter VM Data (D:) Hard Drive in GB’s (Press [Enter] to choose 60GB)"
if ($VDisk2 -eq "")
    {
    $VDisk2=60GB} ; if ($VDisk2 -eq $NULL){$VDisk2=60GB
    }
if ($VDisk2 -ne 60GB)
    {
    $VDisk2=[int]$VDisk2 * 1073741824
    }

$VMSwitches = @(get-vmswitch)

#clear variable for testing
$VMSwitch = ""
$VMNetwork = ""

do
    {
    For($i = 0; $i -le $VMSwitches.GetUpperBound(0);$i++)
        {
        Write-Output (("Enter ‘" + ($i+1) + "’ for Virtual Switch:") + ($VMSwitches[$i].Name))
        }
    $VMSwitch = Read-Host "Choose a switch"
    $VMNetwork = ($VMSwitches[$VMSwitch-1].Name)
    if (($VMNetwork -eq "") -or ($VMNetwork -eq $NULL))
        {
        Write-Host -Foreground Red "Invalid selection made, please enter a valid selection:"
        }
    }
until ($VMSwitch -le $VMSwitches.GetUpperBound(0)+1)

# Set ISO File to boot from
$ISOFILE =
$ISOFILE="D:\Shared\ISO\PXE Booters\LiteTouchPE_x64.iso"
$TestISO = Test-Path $ISOFILE
If ($TestISO -eq $False)
    {
    cls
    Write-Host ""
    Write-Host -Foreground Red "Warning: ISO File missing"
    Write-Host ""
    Write-Host "No ISO file will be mounted"
    Write-Host ""
    Write-Host "Path tested = $ISOFILE"
    Write-Host ""
    Pause
    }

$VMPath = $Storage + ":\Hyper-V"
#$VMSwitch = "VMSwitch"

# Check to make sure there are no file conflicts
$TestPath = Test-Path $VMPath\$VMName
If ($TestPath -eq $True)
    {
    cls
    Write-Host ""
    Write-Host -Foreground Red "Error: VM Folder already exists!!!"
    Write-Host ""
    Write-Host "Choose a new VM name and start again."
    Write-Host ""
    Write-Host "Path tested = $VMPath\$VMName"
    Write-Host ""
    Pause
    }
Else
    {
    # Create Virtual Machine
    MD $VMPath -ErrorAction SilentlyContinue
    New-VM $VMName -path $VMPath -SwitchName $VMNetwork -Generation 1
    New-VHD -Path $VMPath\$VMName\$VMName.C.vhdx -size $VDisk1
    New-VHD -Path $VMPath\$VMName\$VMName.D.vhdx -size $VDisk2
    Add-VMHardDiskDrive -VMName $VMName -Path $VMPath\$VMName\$VMName.C.vhdx
    Add-VMHardDiskDrive -VMName $VMName -Path $VMPath\$VMName\$VMName.D.vhdx
    If ($ISOFILE -ne $NULL)
        {
        Set-VMDvdDrive -VMName $VMName -ControllerLocation 0 -ControllerNumber 1 -Path $ISOFILE
        }
    Get-VM $VMName | Set-VMMemory -DynamicMemoryEnabled $True -MaximumBytes $MaximumMem -MinimumBytes $MinimumMem -StartupBytes $StartupMem
    Get-VM $VMName | Set-VM -ProcessorCount 2 -AutomaticStartAction Start -AutomaticStartDelay 60 -AutomaticStopAction ShutDown
    Get-VM $VMName | Start-VM
    echo off
    $VDisk1 = [int64]$VDisk1 / 1073741824
    $VDisk2 = [int64]$VDisk2 / 1073741824
    $StartupMem = [int64]$StartupMem / 1048576
    $MinimumMem = [int64]$MinimumMem / 1048576
    $MaximumMem = [int64]$MaximumMem / 1048576
    #cls
    $NewVM = Get-VM -Name $VMName
    If ($NewVM -eq $NULL)
        {
        Write-Host -Foreground Red "*** Oh no, something went wrong. ***"
        Write-Host -Foreground Red "*** VM Not found. Check script.. ***"
        Pause
        }
    Else
        {
        Write-Host -foreground Green "Created a new virtual machine called ‘$VMName’ "
        Write-Host ""
        Write-Host -foreground Yellow "$VMName has the following properties:"
        Write-Host "VM Location = $VMPath\$VMName"
        Write-Host "Virtual CPU’s = 2"
        Write-Host "Dymanic Memory = ‘Enabled’"
        Write-Host "Startup Memory = $StartupMem MB"
        Write-Host "Minimum Memory = $MinimumMem MB"
        Write-Host "Maximum Memory = $MaximumMem MB"
        Write-Host "Virtual Disk 1 = $VMName.C.vhdx | $VDisk1 GB"
        Write-Host "Virtual Disk 2 = $VMName.D.vhdx | $VDisk2 GB"
        Write-Host "Virtual Network = $VMNetwork"
        Write-Host "Mounted ISO = $ISOFILE"
        Write-Host ""
        Write-Host "The VM will now start a new MDT Build"
        Write-Host "using the Server 2012 R2 Task Sequence"
        Write-Host ""
        Pause
        }
}

 

Cheers’ Dan

Leave A Comment