Created
January 6, 2026 07:58
-
-
Save notruri/14f94f69cab93d618e01091e4ae4277b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $Config = @{ | |
| VMName = 'GPU-P' | |
| VMMemory = 4096MB | |
| VMCores = 2 | |
| HWThreadPerCore = 2 | |
| MinRsrc = 0 | |
| MaxRsrc = 500000000 | |
| OptimalRsrc = 500000000 | |
| } | |
| ### actual execution | |
| try { | |
| # Get VM host capabilities | |
| $VMHost = Get-VMHost | |
| # Get highest VM config version available | |
| $VMVersion = $VMHost.SupportedVmVersions[$VMHost.SupportedVmVersions.Count - 1] | |
| # Get existing VM if it exists | |
| $VMObject = (Get-VM -Name $Config.VMName -ErrorAction SilentlyContinue) | |
| # Create VM if it doesn't already exist | |
| if (-not $VMObject) { | |
| $NewVM = @{ | |
| Name = $Config.VMName | |
| MemoryStartupBytes = $Config.VMMemory | |
| Generation = 2 | |
| Version = $VMVersion | |
| } | |
| New-VM @NewVM | |
| } | |
| # Enable VM features required for this to work | |
| $SetParams = @{ | |
| VMName = $Config.VMName | |
| ProcessorCount = $Config.VMCores | |
| MemoryStartupBytes = $Config.VMMemory | |
| GuestControlledCacheTypes = $true | |
| LowMemoryMappedIoSpace = 2Gb | |
| HighMemoryMappedIoSpace = 32GB | |
| AutomaticStopAction = 'TurnOff' | |
| CheckpointType = 'Disabled' | |
| DynamicMemory = $false | |
| } | |
| Set-VM @SetParams | |
| Set-VMProcessor -VMName $Config.VMName -HwThreadCountPerCore $Config.HWThreadPerCore | |
| # Disable secure boot | |
| Set-VMFirmware -VMName $Config.VMName -EnableSecureBoot 'Off' | |
| # Parameters for vAdapter | |
| $GPUParams = @{ | |
| VMName = $Config.VMName | |
| MinPartitionVRAM = $Config.MinRsrc | |
| MaxPartitionVRAM = $Config.MaxRsrc | |
| OptimalPartitionVRAM = $Config.OptimalRsrc | |
| MinPartitionEncode = $Config.MinRsrc | |
| MaxPartitionEncode = $Config.MaxRsrc | |
| OptimalPartitionEncode = $Config.OptimalRsrc | |
| MinPartitionDecode = $Config.MinRsrc | |
| MaxPartitionDecode = $Config.MaxRsrc | |
| OptimalPartitionDecode = $Config.OptimalRsrc | |
| MinPartitionCompute = $Config.MinRsrc | |
| MaxPartitionCompute = $Config.MaxRsrc | |
| OptimalPartitionCompute = $Config.OptimalRsrc | |
| } | |
| # Get adapter if it exists | |
| $VMAdapter = (Get-VMGpuPartitionAdapter -VMName $Config.VMName -ErrorAction SilentlyContinue) | |
| # Add adapter if not present, update if present | |
| if ($VMAdapter) { | |
| Set-VMGpuPartitionAdapter @GPUParams | |
| } else { | |
| Add-VMGpuPartitionAdapter @GPUParams | |
| } | |
| } catch { | |
| Write-Error "Something went wrong with creation. Error details below:" | |
| Write-Error $PSItem.ErrorDetails | |
| throw $PSItem | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment