This comprehensive post will explore the benefits of using PowerShell to efficiently automate your Nvidia vGPU driver upgrade. By the end of this guide, you will have gained the knowledge and confidence to streamline your upgrade process, saving time and effort while ensuring a seamless transition to the latest supported version of the Nvidia vGPU driver for your VMware Horizon environment.
Prerequisites
- 7-zip installed on the Local system.
- Powershell 3.0 or newer on both the Local system and the Remote VM.
- Enable PS-Remote on the Remote VM.
- Use an account with Administrative Permissions on the Remote VM.
- Make sure the local system and the Remote VM allow connectivity to each other.
- Make sure to set an exception in your Antivirus/Security software.
- Download the Nvidia vGPU driver supported by your deployment of VMware Horizon.
- A network share that the Local system has write access to and the Remote VM has at least read access to.
- The FQDN or IP Address of each Remote VM.
- The fully qualified path, including the file name where the Nvidia vGPU driver installer was downloaded.
Powershell Script
- Open your preferred Powershell text editor. I use Visual Studio Code.
- Start a new file and save it using the name “NvidiavGPUDriverUpgrade.ps1”, make sure you know the location of the file.
- Now paste the text from the code block below into the file.
# Remote VMs
$computers = @("Computer1","Computer2")
# Specify the path to the exe file and the target directory to extract files
$exeFilePath = "C:\path\to\file\file.exe"
$targetDirectory = "\\path\to\directory\NvidiavGPUDriver\"
# Specify the path to 7-Zip executable
$sevenZipExe = "C:\Program Files\7-Zip\7z.exe"
# Create the target directory if it doesn't exist
if (-not (Test-Path -Path $targetDirectory)) {
New-Item -Path $targetDirectory -ItemType Directory | Out-Null
}
# Extract files from the exe using 7-Zip
try {
$zipExtractionArguments = "x `"$exeFilePath`" -o`"$targetDirectory`" -aoa"
Start-Process -FilePath $sevenZipExe -ArgumentList $zipExtractionArguments -Wait
Write-Host "Files extracted successfully to: $targetDirectory"
} catch {
Write-Host "An error occurred while extracting: $($_.Exception.Message)"
exit
}
foreach ($computer in $computers) {
try {
# Copy file to remote host
$source = "$targetDirectory"
$destination = "\\$computer\c$\NvidiavGPUDriver"
# Verify if the destination path exists on the target computer, and create it if needed
if (!(Test-Path -Path $destination)) {
New-Item -ItemType Directory -Path $destination | Out-Null
}
# Copy files to the remote computer
Get-ChildItem -Path $source | Copy-Item -Destination $destination -Recurse -Force
# Create Session
$session = New-PSSession -ComputerName $computer
# Install the application on the remote computer
Invoke-Command -Session $session -ScriptBlock {
$driverInstallerPath = "C:\NvidiavGPUDriver\setup.exe"
$installationOptions = "-s -n"
Start-Process -FilePath $driverInstallerPath -ArgumentList $installationOptions -Wait
Remove-Item -Path C:\NvidiavGPUDriver\ -Recurse -ErrorAction SilentlyContinue
Restart-Computer -Force
}
}
catch {
Write-Host "An error occurred: $_.Exception.Message"
}
finally {
# Close the session
if ($session) {
Remove-PSSession $session
}
}
}
- We need to edit the following variables.
- $computers
- This should be the FQDN or the IP address of each computer you would like to target. Make sure that each entry in the array is formatted using the following syntax @(“FQDN”,”IP address”,”FQDN”).
- $exeFilePath
- Change this to the full path, including the file name of the Nvidia vGPU driver installer you noted earlier.
- $targetDirectory
- Replace this part of the string “\\path\to\directory\” with the directory where you would like to extract the Nvidia vGPU driver installer’s contents.
- $computers
- Save the script, and then we are ready to run it.
Running The Upgrade
- Open a Powershell window as Administrator and set the working directory to the location of your new script using the Set-Location or CD commands.
- Type the command ‘PowerShell -ExecutionPolicy Bypass -File “NvidiavGPUDriverUpgrade.ps1″‘ and sit back while it upgrades and reboots each Remote VM.
If you found this helpful, check out my other post https://techcollective.blog/automate-horizon-agent-upgrade-using-powershell/.