forked from jamesstringer90/Easy-GPU-PV
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAdd-GPUPartitiontoExistingVM.psm1
More file actions
77 lines (63 loc) · 3.52 KB
/
Add-GPUPartitiontoExistingVM.psm1
File metadata and controls
77 lines (63 loc) · 3.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
Function Add-GPUPartitiontoExistingVM {
param(
[string]$VMName,
[string]$GPUName,
[string]$Hostname = $ENV:COMPUTERNAME,
[decimal]$GPUResourceAllocationPercentage
)
Import-Module $PSSCriptRoot\Add-VMGpuPartitionAdapterFiles.psm1
$CurrentUser = [Security.Principal.WindowsIdentity]::GetCurrent();
(New-Object Security.Principal.WindowsPrincipal $CurrentUser).IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
$VM = Get-VM -VMName $VMName
$VHD = Get-VHD -VMId $VM.VMId
If ($VM.state -eq "Running") {
[bool]$state_was_running = $true
}
if ($VM.state -ne "Off"){
"Attemping to shutdown VM..."
Stop-VM -Name $VMName -Force
}
While ($VM.State -ne "Off") {
Start-Sleep -s 3
"Waiting for VM to shutdown - make sure there are no unsaved documents..."
}
"Adding VM GPU Partition..."
$PartitionableGPUList = Get-WmiObject -Class "Msvm_PartitionableGpu" -ComputerName $env:COMPUTERNAME -Namespace "ROOT\virtualization\v2"
if ($GPUName -eq "AUTO" -or [Environment]::OSVersion.Version.Build -lt 22000) {
$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 ([math]::round($(1000000000 / $devider))) -MaxPartitionVRAM ([math]::round($(1000000000 / $devider))) -OptimalPartitionVRAM ([math]::round($(1000000000 / $devider)))
Set-VMGPUPartitionAdapter -VMName $VMName -MinPartitionEncode ([math]::round($(18446744073709551615 / $devider))) -MaxPartitionEncode ([math]::round($(18446744073709551615 / $devider))) -OptimalPartitionEncode ([math]::round($(18446744073709551615 / $devider)))
Set-VMGpuPartitionAdapter -VMName $VMName -MinPartitionDecode ([math]::round($(1000000000 / $devider))) -MaxPartitionDecode ([math]::round($(1000000000 / $devider))) -OptimalPartitionDecode ([math]::round($(1000000000 / $devider)))
Set-VMGpuPartitionAdapter -VMName $VMName -MinPartitionCompute ([math]::round($(1000000000 / $devider))) -MaxPartitionCompute ([math]::round($(1000000000 / $devider))) -OptimalPartitionCompute ([math]::round($(1000000000 / $devider)))
Set-VM -GuestControlledCacheTypes $true -VMName $VMName
Set-VM -LowMemoryMappedIoSpace 1Gb -VMName $VMName
Set-VM -HighMemoryMappedIoSpace 32GB -VMName $VMName
if ($VHD -is [array]) {
$DiskPath = $VHD.Path[0]
} else {
$DiskPath = $VHD.Path
}
"Mounting Drive..."
$DriveLetter = (Mount-VHD -Path $DiskPath -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object {$_.DriveLetter -and $_.FileSystemType -eq "NTFS"} | ForEach-Object DriveLetter)
if (-Not $DriveLetter) {
'Drive is not mounted'
Read-Host -Prompt "Press Enter to Exit"
}
"Copying GPU Files - this could take a while..."
Add-VMGPUPartitionAdapterFiles -hostname $Hostname -DriveLetter $DriveLetter -GPUName $GPUName
"Dismounting Drive..."
Dismount-VHD -Path $DiskPath
If ($state_was_running){
"Previous State was running so starting VM..."
Start-VM $VMName
}
"Done..."
}