Skip to content

Instantly share code, notes, and snippets.

@dantmnf
Last active June 16, 2021 16:36
Show Gist options
  • Save dantmnf/9bf9972c1ad49029cbdc2e40f1b7ac51 to your computer and use it in GitHub Desktop.
Save dantmnf/9bf9972c1ad49029cbdc2e40f1b7ac51 to your computer and use it in GitHub Desktop.
GPU-P setup script
$ErrorActionPreference = "Stop"
$vmobject = Get-VM | Out-GridView -Title "Select VM to setup GPU-P" -OutputMode Single
$vmid = $vmobject.VMId
Write-Host "Stopping VM"
$vmobject | Stop-VM
Write-Host "Disabling checkpoints for VM"
$vmobject | Set-VM -CheckpointType Disabled
Write-Host "Enabling heartbeat service for VM"
$vmobject | Enable-VMIntegrationService -Name Heartbeat
if ($vmobject | Get-VMGpuPartitionAdapter) { $vmobject | Remove-VMGpuPartitionAdapter }
Write-Host "Starting VM"
$vmobject | Start-VM
do { Start-Sleep 2 } while(($vmobject | Get-VMIntegrationService -Name HeartBeat).PrimaryStatusDescription -ne "OK")
Write-Host "Connecting to VM"
$vmsess = New-PSSession -VMId $vmid
Write-Host "Copying display drivers to VM"
$displays = Get-PnpDevice -Class Display -Status OK
Invoke-Command -Session $vmsess {$prologWritten = $false}
foreach($dev in $displays) {
$props = $dev | Get-PnpDeviceProperty
$pnpinf = ($props | where {$_.KeyName -eq "DEVPKEY_Device_DriverInfPath"}).Data
$infsection = ($props | where {$_.KeyName -eq "DEVPKEY_Device_DriverInfSection"}).Data
$cbsinf = (Get-WindowsDriver -Online | where {$_.Driver -eq $pnpinf}).OriginalFileName
$inffile = (Get-Item -LiteralPath $cbsinf)
$drvpkg = $inffile.Directory
Write-Host "Copying driver package $drvpkg to VM"
$hostdrvstorepath = Invoke-Command {
New-Item -ItemType Directory "$env:SystemRoot\System32\HostDriverStore\FileRepository" -Force
} -Session $vmsess
# $hostdrvstorepath = Join-Path $remoteSystemRoot "System32\HostDriverStore\FileRepository"
Copy-Item -LiteralPath $drvpkg.FullName -ToSession $vmsess -Destination "$hostdrvstorepath" -Recurse -Force
$vminfpath = join-path (Join-Path $hostdrvstorepath $drvpkg.name) $inffile.Name
Invoke-Command -Session $vmsess -ScriptBlock {
if (-not $prologWritten) {
Set-Content -LiteralPath "$env:SystemDrive\GPUPAdditionalSetup.bat" -Encoding utf8 -Value @"
cd /d %TEMP%
set dirname=gpupaddlsetup%RANDOM%
mkdir %dirname%
cd %dirname%
"@
$prologWritten = $true
}
Add-Content -LiteralPath "$env:SystemDrive\GPUPAdditionalSetup.bat" -Encoding utf8 -Value "start `"`" /wait rundll32 advpack.dll,LaunchINFSectionEx $using:vminfpath,$using:infsection,,4"
}
}
$scriptWritten = Invoke-Command -Session $vmsess -ScriptBlock {
if ($prologWritten) {
Add-Content -LiteralPath "$env:SystemDrive\GPUPAdditionalSetup.bat" -Encoding utf8 -Value @"
cd ..
rmdir /s /q %dirname%
"@
}
$prologWritten
}
Write-Host "Stopping VM"
$vmobject | Stop-VM
Write-Host "Configuring GPU-P for VM"
$vmobject | Add-VMGpuPartitionAdapter -MinPartitionVRAM 80000000 -MaxPartitionVRAM 100000000 -OptimalPartitionVRAM 100000000 -MinPartitionEncode 80000000 -MaxPartitionEncode 100000000 -OptimalPartitionEncode 100000000 -MinPartitionDecode 80000000 -MaxPartitionDecode 100000000 -OptimalPartitionDecode 100000000 -MinPartitionCompute 80000000 -MaxPartitionCompute 100000000 -OptimalPartitionCompute 100000000
$vmobject | Set-VM -GuestControlledCacheTypes $true -LowMemoryMappedIoSpace 1Gb –HighMemoryMappedIoSpace 32GB
Write-host "Done"
if ($scriptWritten) {
Write-Host "Run GPUPAdditionalSetup.bat in system drive as administrator for additional setup. This may enable video codec and alternative graphics/compute APIs."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment