Convert to function

This commit is contained in:
James Stringer 2021-06-23 15:01:27 +02:00 committed by GitHub
parent 4eddc093a3
commit e2e04937c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,7 +1,9 @@
$VMname = "VM Name HERE"
$hostname = $ENV:COMPUTERNAME
$DriveLetter = "X:"
Function Add-VMGpuPartitionAdapterFiles {
param(
[Parameter(Mandatory=$true)][string]$VMname = "GPU-P",
[string]$hostname = $ENV:COMPUTERNAME,
[string]$DriveLetter = "X:"
)
$path = (Get-VM -VMName $VMname | Select-Object -Property VMId | Get-VHD).path
$Unique = (Mount-VHD -Path $path -PassThru | Get-Disk | Get-Partition | Get-Volume | Where-Object size -GT 10GB)
@ -26,7 +28,9 @@ foreach ($d in $drivers) {
$DriverID = $d.DeviceID
Write-Host "####Driver files for driver with name: $DriverName" -ForegroundColor Green
Write-Host "and with DriverID: $DriverID" -ForegroundColor Green
if ($DriverName -like "NVIDIA*") {New-Item -ItemType Directory -Path "$driveletter\Windows\System32\drivers\Nvidia Corporation\" -Force | Out-Null}
if ($DriverName -like "NVIDIA*") {
New-Item -ItemType Directory -Path "$driveletter\Windows\System32\drivers\Nvidia Corporation\" -Force | Out-Null
}
foreach ($i in $DriverFiles) {
# We elliminate double backslashes from the file paths
$path = $i.Dependent.Split("=")[1] -replace '\\\\', '\'
@ -34,23 +38,25 @@ foreach ($d in $drivers) {
$InfItem = Get-Item -Path $path2
$Version = $InfItem.VersionInfo.FileVersion
If ($path2 -like "c:\windows\system32\driverstore\*") {
$ParseDestination = $path2.Replace("c:","$driveletter").Replace("driverstore","HostDriverStore")
$Destination = $ParseDestination.SubString(0, $ParseDestination.LastIndexOf('\'))
New-Item -ItemType Directory -Path "$Destination" -Force | Out-Null
Copy-Item $path2 -Destination $Destination -Force
Write-Host "Copied $path2 to $Destination" -ForegroundColor Green
$ParseDestination = $path2.Replace("c:","$driveletter").Replace("driverstore","HostDriverStore")
$Destination = $ParseDestination.SubString(0, $ParseDestination.LastIndexOf('\'))
New-Item -ItemType Directory -Path "$Destination" -Force | Out-Null
Copy-Item $path2 -Destination $Destination -Force
Write-Host "Copied $path2 to $Destination" -ForegroundColor Green
}
Else {
$ParseDestination = $path2.Replace("c:", "$driveletter")
$Destination = $ParseDestination.Substring(0, $ParseDestination.LastIndexOf('\'))
if (!$(Get-Item -Path $Destination -ErrorAction SilentlyContinue).Exists ) {
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
}
Copy-Item $path2 -Destination $Destination -Force
Write-Host "Copied $path2 to $Destination" -ForegroundColor Green
$ParseDestination = $path2.Replace("c:", "$driveletter")
$Destination = $ParseDestination.Substring(0, $ParseDestination.LastIndexOf('\'))
if (!$(Get-Item -Path $Destination -ErrorAction SilentlyContinue).Exists ) {
New-Item -ItemType Directory -Path $Destination -Force | Out-Null
}
Copy-Item $path2 -Destination $Destination -Force
Write-Host "Copied $path2 to $Destination" -ForegroundColor Green
}
}
}
Get-Volume -UniqueId $unique.UniqueId | Get-Partition | Remove-PartitionAccessPath -AccessPath $driveLetter
Dismount-vhd -Path $path
}