From 8ce9a1fe4e462a742fc11785bfdde2399d147086 Mon Sep 17 00:00:00 2001 From: James Stringer <38541878+jamesstringerparsec@users.noreply.github.com> Date: Sun, 12 Dec 2021 17:06:15 +0100 Subject: [PATCH] Move Add-VMGpuPartitionAdapterFiles to module --- Add-VMGpuPartitionAdapterFiles.psm1 | 77 +++++++++++++++++++++++++++ CopyFilesToVM.ps1 | 80 +---------------------------- Update-VMGpuPartitionDriver.ps1 | 73 +------------------------- 3 files changed, 80 insertions(+), 150 deletions(-) create mode 100644 Add-VMGpuPartitionAdapterFiles.psm1 diff --git a/Add-VMGpuPartitionAdapterFiles.psm1 b/Add-VMGpuPartitionAdapterFiles.psm1 new file mode 100644 index 0000000..488ebb1 --- /dev/null +++ b/Add-VMGpuPartitionAdapterFiles.psm1 @@ -0,0 +1,77 @@ +Function Add-VMGpuPartitionAdapterFiles { +param( +[string]$hostname = $ENV:COMPUTERNAME, +[string]$DriveLetter, +[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 + $GPU = Get-PnpDevice | Where-Object {($_.DeviceID -like "*$($DevicePathName.Substring(8,16))*") -and ($_.Status -eq "OK")} | Select-Object -First 1 + $GPUName = $GPU.Friendlyname + $GPUServiceName = $GPU.Service + } +Else { + $GPU = Get-PnpDevice | Where-Object {($_.Name -eq "$GPUName") -and ($_.Status -eq "OK")} | Select-Object -First 1 + $GPUServiceName = $GPU.Service + } +# Get Third Party drivers used, that are not provided by Microsoft and presumably included in the OS + +Write-Host "INFO : Finding and copying driver files for $GPUName to VM. This could take a while..." + +$Drivers = Get-WmiObject Win32_PNPSignedDriver | where {$_.DeviceName -eq "$GPUName"} + +New-Item -ItemType Directory -Path "$DriveLetter\windows\system32\HostDriverStore" -Force | Out-Null + +#copy directory associated with sys file +$servicePath = (Get-WmiObject Win32_SystemDriver | Where-Object {$_.Name -eq "$GPUServiceName"}).Pathname + $ServiceDriverDir = $servicepath.split('\')[0..5] -join('\') + $ServicedriverDest = ("$driveletter" + "\" + $($servicepath.split('\')[1..5] -join('\'))).Replace("DriverStore","HostDriverStore") + if (!(Test-Path $ServicedriverDest)) { + Copy-item -path "$ServiceDriverDir" -Destination "$ServicedriverDest" -Recurse + } + +# Initialize the list of detected driver packages as an array +$DriverFolders = @() +foreach ($d in $drivers) { + + $DriverFiles = @() + $ModifiedDeviceID = $d.DeviceID -replace "\\", "\\" + $Antecedent = "\\" + $hostname + "\ROOT\cimv2:Win32_PNPSignedDriver.DeviceID=""$ModifiedDeviceID""" + $DriverFiles += Get-WmiObject Win32_PNPSignedDriverCIMDataFile | where {$_.Antecedent -eq $Antecedent} + $DriverName = $d.DeviceName + $DriverID = $d.DeviceID + if ($DriverName -like "NVIDIA*") { + New-Item -ItemType Directory -Path "$driveletter\Windows\System32\drivers\Nvidia Corporation\" -Force | Out-Null + } + foreach ($i in $DriverFiles) { + $path = $i.Dependent.Split("=")[1] -replace '\\\\', '\' + $path2 = $path.Substring(1,$path.Length-2) + $InfItem = Get-Item -Path $path2 + $Version = $InfItem.VersionInfo.FileVersion + If ($path2 -like "c:\windows\system32\driverstore\*") { + $DriverDir = $path2.split('\')[0..5] -join('\') + $driverDest = ("$driveletter" + "\" + $($path2.split('\')[1..5] -join('\'))).Replace("driverstore","HostDriverStore") + if (!(Test-Path $driverDest)) { + Copy-item -path "$DriverDir" -Destination "$driverDest" -Recurse + } + } + Else { + $ParseDestination = $path2.Replace("c:", "$driveletter") + $Destination = $ParseDestination.Substring(0, $ParseDestination.LastIndexOf('\')) + if (!$(Test-Path -Path $Destination)) { + New-Item -ItemType Directory -Path $Destination -Force | Out-Null + } + Copy-Item $path2 -Destination $Destination -Force + + } + + } + } + +} \ No newline at end of file diff --git a/CopyFilesToVM.ps1 b/CopyFilesToVM.ps1 index f5d6620..0f78d3d 100644 --- a/CopyFilesToVM.ps1 +++ b/CopyFilesToVM.ps1 @@ -17,6 +17,8 @@ Autologon = "true" } +Import-Module $PSSCriptRoot\Add-VMGpuPartitionAdapterFiles.psm1 + Function Setup-ParsecInstall { param( [string]$DriveLetter, @@ -49,83 +51,6 @@ param( Copy-Item -Path $psscriptroot\Machine\Install.ps1 -Destination $DriveLetter\Windows\system32\GroupPolicy\Machine\Scripts\Startup } -Function Add-VMGpuPartitionAdapterFiles { -param( -[string]$hostname = $ENV:COMPUTERNAME, -[string]$DriveLetter, -[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 - $GPU = Get-PnpDevice | Where-Object {($_.DeviceID -like "*$($DevicePathName.Substring(8,16))*") -and ($_.Status -eq "OK")} | Select-Object -First 1 - $GPUName = $GPU.Friendlyname - $GPUServiceName = $GPU.Service - } -Else { - $GPU = Get-PnpDevice | Where-Object {($_.Name -eq "$GPUName") -and ($_.Status -eq "OK")} | Select-Object -First 1 - $GPUServiceName = $GPU.Service - } -# 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"} - -New-Item -ItemType Directory -Path "$DriveLetter\windows\system32\HostDriverStore" -Force | Out-Null - -#copy directory associated with sys file -$servicePath = (Get-WmiObject Win32_SystemDriver | Where-Object {$_.Name -eq "$GPUServiceName"}).Pathname - $ServiceDriverDir = $servicepath.split('\')[0..5] -join('\') - $ServicedriverDest = ("$driveletter" + "\" + $($servicepath.split('\')[1..5] -join('\'))).Replace("DriverStore","HostDriverStore") - if (!(Test-Path $ServicedriverDest)) { - Copy-item -path "$ServiceDriverDir" -Destination "$ServicedriverDest" -Recurse - } - -# Initialize the list of detected driver packages as an array -$DriverFolders = @() -foreach ($d in $drivers) { - - $DriverFiles = @() - $ModifiedDeviceID = $d.DeviceID -replace "\\", "\\" - $Antecedent = "\\" + $hostname + "\ROOT\cimv2:Win32_PNPSignedDriver.DeviceID=""$ModifiedDeviceID""" - $DriverFiles += Get-WmiObject Win32_PNPSignedDriverCIMDataFile | where {$_.Antecedent -eq $Antecedent} - $DriverName = $d.DeviceName - $DriverID = $d.DeviceID - if ($DriverName -like "NVIDIA*") { - New-Item -ItemType Directory -Path "$driveletter\Windows\System32\drivers\Nvidia Corporation\" -Force | Out-Null - } - foreach ($i in $DriverFiles) { - $path = $i.Dependent.Split("=")[1] -replace '\\\\', '\' - $path2 = $path.Substring(1,$path.Length-2) - $InfItem = Get-Item -Path $path2 - $Version = $InfItem.VersionInfo.FileVersion - If ($path2 -like "c:\windows\system32\driverstore\*") { - $DriverDir = $path2.split('\')[0..5] -join('\') - $driverDest = ("$driveletter" + "\" + $($path2.split('\')[1..5] -join('\'))).Replace("driverstore","HostDriverStore") - if (!(Test-Path $driverDest)) { - Copy-item -path "$DriverDir" -Destination "$driverDest" -Recurse - } - } - Else { - $ParseDestination = $path2.Replace("c:", "$driveletter") - $Destination = $ParseDestination.Substring(0, $ParseDestination.LastIndexOf('\')) - if (!$(Test-Path -Path $Destination)) { - New-Item -ItemType Directory -Path $Destination -Force | Out-Null - "New Item $Destination" - } - Copy-Item $path2 -Destination $Destination -Force - "Copy Item $Path2 to $Destination" - - } - - } - } - -} - function Convert-WindowsImage { <# .NOTES @@ -2435,7 +2360,6 @@ You can use the fields below to configure the VHD or VHDX that you want to creat # if (($GPUName)) { - Write-W2VInfo "Copying GPU Files. This could take a while..." Add-VMGpuPartitionAdapterFiles -GPUName $GPUName -DriveLetter $windowsDrive } diff --git a/Update-VMGpuPartitionDriver.ps1 b/Update-VMGpuPartitionDriver.ps1 index 9a9a52f..f4664ee 100644 --- a/Update-VMGpuPartitionDriver.ps1 +++ b/Update-VMGpuPartitionDriver.ps1 @@ -4,78 +4,7 @@ [string]$Hostname = $ENV:Computername ) -Function Add-VMGpuPartitionAdapterFiles { -param( -[string]$hostname = $ENV:COMPUTERNAME, -[string]$DriveLetter, -[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"} - -New-Item -ItemType Directory -Path "$DriveLetter\windows\system32\HostDriverStore" -Force | Out-Null - -#copy directory associated with sys file -$service = Get-PnpDevice | Where-Object {$_.Name -eq "$GPUName"} | Select-Object Service -ExpandProperty Service -$servicePath = (Get-WmiObject Win32_SystemDriver | Where-Object {$_.Name -eq "$service"}).Pathname - $ServiceDriverDir = $servicepath.split('\')[0..5] -join('\') - $ServicedriverDest = ("$driveletter" + "\" + $($servicepath.split('\')[1..5] -join('\'))).Replace("DriverStore","HostDriverStore") - if (!(Test-Path $ServicedriverDest)) { - Copy-item -path "$ServiceDriverDir" -Destination "$ServicedriverDest" -Recurse - } - -# Initialize the list of detected driver packages as an array -$DriverFolders = @() -foreach ($d in $drivers) { - - $DriverFiles = @() - $ModifiedDeviceID = $d.DeviceID -replace "\\", "\\" - $Antecedent = "\\" + $hostname + "\ROOT\cimv2:Win32_PNPSignedDriver.DeviceID=""$ModifiedDeviceID""" - $DriverFiles += Get-WmiObject Win32_PNPSignedDriverCIMDataFile | where {$_.Antecedent -eq $Antecedent} - $DriverName = $d.DeviceName - $DriverID = $d.DeviceID - if ($DriverName -like "NVIDIA*") { - New-Item -ItemType Directory -Path "$driveletter\Windows\System32\drivers\Nvidia Corporation\" -Force | Out-Null - } - foreach ($i in $DriverFiles) { - $path = $i.Dependent.Split("=")[1] -replace '\\\\', '\' - $path2 = $path.Substring(1,$path.Length-2) - $InfItem = Get-Item -Path $path2 - $Version = $InfItem.VersionInfo.FileVersion - If ($path2 -like "c:\windows\system32\driverstore\*") { - $DriverDir = $path2.split('\')[0..5] -join('\') - $driverDest = ("$driveletter" + "\" + $($path2.split('\')[1..5] -join('\'))).Replace("driverstore","HostDriverStore") - if (!(Test-Path $driverDest)) { - Copy-item -path "$DriverDir" -Destination "$driverDest" -Recurse - } - } - Else { - $ParseDestination = $path2.Replace("c:", "$driveletter") - $Destination = $ParseDestination.Substring(0, $ParseDestination.LastIndexOf('\')) - if (!$(Test-Path -Path $Destination)) { - New-Item -ItemType Directory -Path $Destination -Force | Out-Null - "New Item $Destination" - } - Copy-Item $path2 -Destination $Destination -Force - "Copy Item $Path2 to $Destination" - - } - - } - } - -} +Import-Module $PSSCriptRoot\Add-VMGpuPartitionAdapterFiles.psm1 $VM = Get-VM -VMName $VMName $VHD = Get-VHD -VMId $VM.VMId