Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Hyper-V VM creation and boot [closed]

I am attempting to use PowerShell to create and start a VM:

$vmName = "vm" + (Get-Date -Format "yyyy-MM-dd-HH-mm")
New-VM -Name $vmName -NewVHDPath "$vmName.vhdx" -NewVHDSizeBytes 64GB -MemoryStartupBytes 8GB -Path $vmName -Generation 2

# Attach the Windows 10 ISO as a DVD drive to the VM
Add-VMDvdDrive -VMName $vmName -Path win.iso

# Set correct boot order (DVD drive first)
$dvd = Get-VMDVDDrive -VMName $vmName
Set-VMFirmware -VMName $vmName -FirstBootDevice $dvd

# Start the VM and connect to it
Start-VM -Name $vmName
vmconnect $env:COMPUTERNAME $vmName

This works well, creates the VM, attaches the Windows 10 ISO, sets the correct boot order, starts the VM and connects to it.

However, right after starting up, the VM fails to boot. First, a black screen prompting me to press any key to boot from the DVD pops up, however, it is only for a brief second and before I manage to do it, I get this screen:

enter image description here

The boot order is correct in the VM settings: DVD first, then network, then the VHD. I want to boot from the DVD, but I don't have a chance to press a key to do so before the white screen appears.

Morever, I want to make the script so that it automatically enters the DVD/ISO boot without me having to press a key to enter that boot option. How can I do that? My ultimate goal is a completely unattended installation which starts by invoking the scripts and ends by the installed Windows 10 guest executing a PowerShell script shared from the host. That means I can't ask the user to press a key at a certain time to help the boot sequence along.

Edit: Bounty of 50 points for someone who can demonstrate a PowerShell script which creates, starts, connects and boots a Hyper-V VM with a Windows 10 ISO so that at the end there is a running VM on the first screen of the Windows 10 installer.

like image 234
Tomáš Hübelbauer Avatar asked Oct 23 '25 15:10

Tomáš Hübelbauer


2 Answers

I think your issue is not a powershell issue. Your issue is that you are using a regular boot iso, but in your case you need to create a custom WIM (Windows Imaging File). If you create one you can start directly the installation process without human interaction.

I don't want to duplicate the text written already on superuser.com; it is long and takes many steps. I recommend using the second approach - Creating custom ISO from Windows 10 as it makes sense and takes you step-by-step over the creating custom WIM.

like image 123
tukan Avatar answered Oct 26 '25 04:10

tukan


Try this:

# Start the VM and connect to it
vmconnect $env:COMPUTERNAME $vmName
Start-VM -Name $vmName
Start-Sleep -Seconds 1
[System.Windows.Forms.SendKeys]::SendWait("{ENTER}")

You might have to adjust the sleep time, so a custom wim is probably the better option.

like image 37
antonyoni Avatar answered Oct 26 '25 04:10

antonyoni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!