# MUST RUN AS ADMINISTRATOR$StartTime = $(get-date);$HOSTNAME = "ROUTER"# $ISO = "C:\Users\maharris\Downloads\OPNsense-21.1-OpenSSL-dvd-amd64.iso"#$ISO = "C:\Users\maharris\Downloads\OPNsense-22.1.2-OpenSSL-dvd-amd64.iso"# $ISO = "C:\Users\maharris\Downloads\OPNsense-23.1-OpenSSL-dvd-amd64.iso"$ISO = "C:\Users\maharris\Downloads\OPNsense-23.7-dvd-amd64.iso"$VMName = $HOSTNAME;$Notes = ""$vhdSize = 512GB;$ProcessorCount = 8;$Gen = 1;$StartupRAM = 4GB;$MinRAM = 2GB;$MaxRAM = 8GB;$EnableDynamicMemory = $true;$EnableSecureBoot = $false;$SwitchName1 = "Default Switch";$SwitchName2 = "Internal-LAN1";$ROOT = Get-VMHost | Select-Object -ExpandProperty VirtualMachinePath;# Set up VM directory structure to put all VM resources in the ROOT path# VM ROOT ($LABROOT)# ├── Smart Paging# │ └── Virtual Machines# │ └── <GUID># ├── Snapshots# ├── Virtual Hard Disks# │ └── <$VMName>_Disk.vhdx# └── Virtual Machines# ├── <GUID># ├── <GUID>.vmcx# ├── <GUID>.vmgs# └── <GUID>.VMRSif (!(Test-Path $ROOT)){ mkdir $ROOT;}$LABROOT = "$ROOT\$VMName";if (!(Test-Path $LABROOT)){ mkdir $LABROOT;}$LABDISKROOT = $LABROOT + '\Virtual Hard Disks\';if (!(Test-Path $LABDISKROOT)){ mkdir $LABDISKROOT;}$LABVMROOT = $LABROOT + '\Virtual Machines\';if (!(Test-Path $LABVMROOT)){ mkdir $LABVMROOT;}$PAGINGROOT = $LABROOT + '\Smart Paging\';if (!(Test-Path $PAGINGROOT)){ mkdir $PAGINGROOT;}$SNAPSHOTROOT = $LABROOT + '\Snapshots\';if (!(Test-Path $SNAPSHOTROOT)){ mkdir $SNAPSHOTROOT;}# https://docs.microsoft.com/en-us/powershell/module/hyper-v/get-vmswitch?view=windowsserver2022-ps$switch1 = Get-VMSwitch -Name $SwitchName1;$switch2 = Get-VMSwitch -Name $SwitchName2;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/new-vhd?view=windowsserver2022-ps$DISK01PATH = $LABDISKROOT + "\$VMName"+"_DISK01.vhdx"if (!(Test-Path $DISK01PATH)) { # https://docs.microsoft.com/en-us/windows-server/virtualization/hyper-v/best-practices-for-running-linux-on-hyper-v # When creating the VHDX, use 1MB BlockSizeBytes (from the default 32MB) $vhdx = New-VHD -Path $DISK01PATH -SizeBytes $vhdSize -Dynamic -BlockSizeBytes 1MB;}else { $vhdx = Get-VHD -Path $DISK01PATH;}# https://docs.microsoft.com/en-us/powershell/module/hyper-v/new-vm?view=windowsserver2022-ps$vm = New-VM -Name $VMName -MemoryStartupBytes $StartupRAM -SwitchName $switch1.Name -Generation $Gen -Path $ROOT -VHDPath $vhdx.Path;if ($switch2 -ne $null) { Add-VMNetworkAdapter -VMName $VMName -SwitchName $switch2.Name}# https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmprocessor?view=windowsserver2022-ps$vm | Set-VMProcessor -Count $ProcessorCount;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/enable-vmintegrationservice?view=windowsserver2022-ps$vm | Get-VMIntegrationService -Name "Guest Service Interface" | Enable-VMIntegrationService;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmmemory?view=windowsserver2022-ps$vm | Set-VMMemory -DynamicMemoryEnabled $EnableDynamicMemory -MinimumBytes $MinRAM -MaximumBytes $MaxRAM;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmcomport?view=windowsserver2022-ps$vm | Set-VMComPort -Number 2 -Path "\\.\pipe\dbg1";# https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vm?view=windowsserver2022-ps$vm | Set-VM -AutomaticCheckpointsEnabled $false -AutomaticStartAction StartIfRunning -DynamicMemory -SmartPagingFilePath $PAGINGROOT -SnapshotFileLocation $SNAPSHOTROOT -EnhancedSessionTransportType HvSocket -Notes $Notes;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/add-vmdvddrive?view=windowsserver2022-ps$dvd = $vm | Add-VMDvdDrive -Path $ISO -Passthru;# https://docs.microsoft.com/en-us/powershell/module/hyper-v/set-vmfirmware?view=windowsserver2022-psif ($Gen -eq 2){ $vm | Set-VMFirmware -FirstBootDevice $dvd; if ($EnableSecureBoot) { # Sets Secure Boot Template. $vm | Set-VMFirmware -SecureBootTemplate 'MicrosoftUEFICertificateAuthority'; } else { # Disables Secure Boot. $vm | Set-VMFirmware -EnableSecureBoot:Off; }}if ($Gen -eq 2){ $vmNIC = Get-VMNetworkAdapter -VM $vm | Where SwitchName -eq $SwitchName1; $vmDVD = Get-VMDvdDrive -VM $vm; $vmDisk1 = Get-VMHardDiskDrive -VM $vm -ControllerType SCSI -ControllerNumber 0 -ControllerLocation 0; Set-VMFirmware -VM $vm -BootOrder $vmDVD, $vmDisk1, $vmNIC;}# Disable-VMIntegrationService -Name "Time Synchronization" -VM $vm# Disable-VMIntegrationService -Name "Guest Service Interface" -VM $vm$StopTime = $(get-date);$ElapsedTime = $StopTime - $StartTime;$TotalTime = "{0:HH:mm:ss}" -f ([datetime]$ElapsedTime.Ticks);Write-HostWrite-Host $TotalTime;
I'm not using anything out of the ordinary as far as I know. ...I use the following powershell script to create my VM.
If you want to try a pre-built vhdx, see my signature.
2. I mount the .ISO in Hyper-V but it seems to run on the ISO and not offering an installation option?