From 47ac9e9f86f2e0bae1ff8a29c26753fcd6221191 Mon Sep 17 00:00:00 2001 From: James Stringer <38541878+jamesstringerparsec@users.noreply.github.com> Date: Thu, 25 Nov 2021 03:57:40 -0800 Subject: [PATCH] Add Auto GPU mode for Win10 / 11, fix HDD size bug --- CopyFilesToVM.ps1 | 31 +++++++++++++++++++++++++------ Update-VMGpuPartitionDriver.ps1 | 12 +++++++++++- 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/CopyFilesToVM.ps1 b/CopyFilesToVM.ps1 index 8cd4323..41825d3 100644 --- a/CopyFilesToVM.ps1 +++ b/CopyFilesToVM.ps1 @@ -4,11 +4,11 @@ Edition = 6 VhdFormat = "VHDX" DiskLayout = "UEFI" - SizeBytes = 40gb + SizeBytes = 40GB MemoryAmount = 8GB CPUCores = 4 UnattendPath = "$PSScriptRoot"+"\autounattend.xml" - GPUName = "NVIDIA Geforce RTX 2060 SUPER" + GPUName = "AUTO" GPUResourceAllocationPercentage = 50 Team_ID = "" Key = "" @@ -56,6 +56,16 @@ param( [string]$GPUName ) +If (!($DriveLetter -like "*:*")) { + $DriveLetter = $Driveletter + ":" + } + +If ($GPUName -eq "AUTO") { + $PartitionableGPUList = Get-WmiObject -Class "Msvm_PartitionableGpu" -ComputerName $env:COMPUTERNAME -Namespace "ROOT\virtualization\v2" + $DevicePathName = $PartitionableGPUList.Name | Select-Object -First 1 + $GPUName = Get-PnpDevice | Where-Object {$_.DeviceID -like "*$($DevicePathName.Substring(8,16))*"} | Select-Object FriendlyName -ExpandProperty FriendlyName + } + # Get Third Party drivers used, that are not provided by Microsoft and presumably included in the OS $drivers = Get-WmiObject Win32_PNPSignedDriver | where {$_.DeviceName -eq "$GPUName"} @@ -4245,11 +4255,20 @@ param( [string]$GPUName, [decimal]$GPUResourceAllocationPercentage = 100 ) + $PartitionableGPUList = Get-WmiObject -Class "Msvm_PartitionableGpu" -ComputerName $env:COMPUTERNAME -Namespace "ROOT\virtualization\v2" - $DeviceID = ((Get-WmiObject Win32_PNPSignedDriver | where {($_.Devicename -eq "$GPUNAME")}).hardwareid).split('\')[1] - $DevicePathName = ($PartitionableGPUList | Where-Object name -like "*$deviceid*").Name - Add-VMGpuPartitionAdapter -VMName $VMName -InstancePath $DevicePathName + if ($GPUName -eq "AUTO") { + $DevicePathName = $PartitionableGPUList.Name[0] + Add-VMGpuPartitionAdapter -VMName $VMName + } + else { + $DeviceID = ((Get-WmiObject Win32_PNPSignedDriver | where {($_.Devicename -eq "$GPUNAME")}).hardwareid).split('\')[1] + $DevicePathName = ($PartitionableGPUList | Where-Object name -like "*$deviceid*").Name + Add-VMGpuPartitionAdapter -VMName $VMName -InstancePath $DevicePathName + } + [float]$devider = [math]::round($(100 / $GPUResourceAllocationPercentage),2) + Set-VMGpuPartitionAdapter -VMName $VMName -MinPartitionVRAM 0 -MaxPartitionVRAM 1000000000 -OptimalPartitionVRAM ([math]::round($(1000000000 / $devider))) Set-VMGPUPartitionAdapter -VMName $VMName -MinPartitionEncode 0 -MaxPartitionEncode 18446744073709551615 -OptimalPartitionEncode ([math]::round($(18446744073709551615 / $devider))) Set-VMGpuPartitionAdapter -VMName $VMName -MinPartitionDecode 0 -MaxPartitionDecode 1000000000 -OptimalPartitionDecode ([math]::round($(1000000000 / $devider))) @@ -4278,7 +4297,7 @@ param( [string]$autologon ) Modify-AutoUnattend -username "$username" -password "$password" -autologon $autologon -hostname $VMName -UnattendPath $UnattendPath - Convert-WindowsImage -SourcePath $SourcePath -Edition $Edition -VHDFormat $Vhdformat -VHDPath $VhdPath -DiskLayout $DiskLayout -UnattendPath $UnattendPath -GPUName $GPUName -Team_ID $Team_ID -Key $Key| Out-Null + Convert-WindowsImage -SourcePath $SourcePath -Edition $Edition -VHDFormat $Vhdformat -VHDPath $VhdPath -DiskLayout $DiskLayout -UnattendPath $UnattendPath -GPUName $GPUName -Team_ID $Team_ID -Key $Key -SizeBytes $SizeBytes| Out-Null New-VM -Name $VMName -MemoryStartupBytes $MemoryAmount -VHDPath $VhdPath -Generation 2 -SwitchName "Default Switch" | Out-Null Set-VM -Name $VMName -ProcessorCount $CPUCores -CheckpointType Disabled -LowMemoryMappedIoSpace 3GB -HighMemoryMappedIoSpace 32GB -GuestControlledCacheTypes $true -AutomaticStopAction ShutDown Set-VMMemory -VMName $VMName -DynamicMemoryEnabled $false diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index a74a8ea..9a9a52f 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -11,6 +11,16 @@ param( [string]$GPUName ) +If (!($DriveLetter -like "*:*")) { + $DriveLetter = $Driveletter + ":" + } + +If ($GPUName -eq "AUTO") { + $PartitionableGPUList = Get-WmiObject -Class "Msvm_PartitionableGpu" -ComputerName $env:COMPUTERNAME -Namespace "ROOT\virtualization\v2" + $DevicePathName = $PartitionableGPUList.Name | Select-Object -First 1 + $GPUName = Get-PnpDevice | Where-Object {$_.DeviceID -like "*$($DevicePathName.Substring(8,16))*"} | Select-Object FriendlyName -ExpandProperty FriendlyName + } + # Get Third Party drivers used, that are not provided by Microsoft and presumably included in the OS $drivers = Get-WmiObject Win32_PNPSignedDriver | where {$_.DeviceName -eq "$GPUName"} @@ -85,7 +95,7 @@ While ($VM.State -ne "Off") { } "Mounting Drive..." -$DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) + ":" +$DriveLetter = (Mount-VHD -Path $VHD.Path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter} | ForEach-Object DriveLetter) "Copying GPU Files - this could take a while..." Add-VMGPUPartitionAdapterFiles -hostname $Hostname -DriveLetter $DriveLetter -GPUName $GPUName