diff --git a/CopyFilesToVM.ps1 b/CopyFilesToVM.ps1 new file mode 100644 index 0000000..99ef23f --- /dev/null +++ b/CopyFilesToVM.ps1 @@ -0,0 +1,48 @@ +$hostname = $ENV:COMPUTERNAME +$DriveLetter = "F:" + +# Get Third Party drivers used, that are not provided by Microsoft and presumably included in the OS +$drivers = Get-WmiObject Win32_PNPSignedDriver | where {$_.DriverProviderName -eq "NVIDIA"} + +New-Item -ItemType Directory -Path "$DriveLetter\windows\system32\HostDriverStore" -Force | Out-Null + +# Initialize the list of detected driver packages as an array +$DriverFolders = @() +foreach ($d in $drivers) { + # We initialize the list of driver files for each driver + $DriverFiles = @() + # For each driver instance from WMI class Win32_PNPSignedDriver, we compose the related WMI object name from the other WMI driver class, Win32_PNPSignedDriverCIMDataFile + $ModifiedDeviceID = $d.DeviceID -replace "\\", "\\" + $Antecedent = "\\" + $hostname + "\ROOT\cimv2:Win32_PNPSignedDriver.DeviceID=""$ModifiedDeviceID""" + # Get all related driver files for each driver listed in WMI class Win32_PNPSignedDriver + $DriverFiles += Get-WmiObject Win32_PNPSignedDriverCIMDataFile | where {$_.Antecedent -eq $Antecedent} + $DriverName = $d.DeviceName + $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} + foreach ($i in $DriverFiles) { + # We elliminate double backslashes from the file paths + $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\*") { + $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 $DestinationNormal -Force + Write-Host "Copied $path2 to $Destination" -ForegroundColor Green + } + + } + } \ No newline at end of file