This script can be used with Azure Automation Runbook and a webhook to trigger the install and configuration. You can also remove modify the script to fit your needs. If you use Azure Automation you will need to add your credential and configure a webhook.
param (
[Parameter(Mandatory=$false)]
[object] $WebhookData,
[Parameter(Mandatory=$false)]
[string] $vmName,
[Parameter(Mandatory=$false)]
[string] $UPN,
[Parameter(Mandatory=$false)]
[string] $TICKET
)
if ($WebhookData.RequestBody) {
$payload = $WebhookData.RequestBody | ConvertFrom-Json
if ($payload.vmName) {
$vmName = $payload.vmName
}
if ($payload.UPN) {
$UPN = $payload.UPN
}
if ($payload.SNOW_TICKET) {
$TICKET = $payload.TICKET
}
}
Write-Output "vmName: $vmName"
Write-Output "UPN: $UPN"
Write-Output "TICKET: $TICKET"
# Check Payload
Write-Output $vmName
#Testing Manually Add Computer Name here and uncomment.
#$vmName = "AVD"
# Add FQDN to the VMName
$domainName = ".domain.com"
$fqdn = $vmName + $domainName
# Verify Host Name is correct
Write-Output $fqdn
# Check if the VM is reachable
$testConnection = Test-NetConnection -ComputerName $fqdn -CommonTCPPort RDP
if ($testConnection.TcpTestSucceeded) {
# Get Credentials from Azure Automation and Use to Run script
$creds = Get-AutomationPSCredential -Name "<Account>"
# Create a new PSSession to the target VM
$session = New-PSSession -ComputerName $fqdn -Credential $creds
# Invoke the scriptblock on the remote session
Invoke-Command -Session $session -ScriptBlock {
# Download
$url = "https://go.microsoft.com/fwlink/?linkid=874181"
$outputPath = "C:\Temp\NvidiaDriver.exe"
Invoke-WebRequest -Uri $url -OutFile $outputPath
# Unblock file
Get-ChildItem -Path "C:\Temp\NvidiaDriver.exe" | Unblock-File
# Run the installer with the -s (silent) flag
Start-Process -FilePath "C:\Temp\NvidiaDriver.exe" -ArgumentList "-s" -Wait
# Use hardware graphics adapters for all Remote Desktop Services sessions
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name
"bEnumerateHWBeforeSW" -PropertyType "DWord" -Value 1
# Configure H.264/AVC hardware encoding for Remote Desktop connections and set this policy to
#Enabled to enable hardware encoding for AVC/H.264 in the remote session
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name
"AVCHardwareEncodePreferred" -PropertyType "DWord" -Value 1
# Prioritize H.264/AVC 444 Graphics mode for Remote Desktop connections and set this policy to
#Enabled to force H.264/AVC 444 codec in the remote session
New-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\Terminal Services" -Name
"AVC444ModePreferred" -PropertyType "DWord" -Value 1
# Restart VM
Restart-Computer -Force
}
# Close the session
Remove-PSSession -Session $session
} else {
Write-Output "VM is not reachable. Exiting."
#Send Alert to Teams or Email that manual install required.
}
# Write Logfile GPUDriverInstall.txt
# Define the log file path
$logFilePath = "C:\Temp\GPUDriverInstall.txt"
# Function to write log entries to the file
function Write-LogEntry {
param(
[Parameter(Mandatory = $true)]
[string]$Message,
[Parameter(Mandatory = $true)]
[string]$Status
)
# Get the current timestamp
$timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
# Create the log entry string
$logEntry = "$timestamp - $Status - $Message"
# Append the log entry to the file
Add-Content -Path $logFilePath -Value $logEntry
}
# Write a log entry for successful operations
Write-LogEntry -Message "GPU Software and Settings completed successfully." -Status "Success"
# Write a log entry for failed operations
Write-LogEntry -Message "GPU Software and Settings Failed." -Status "Failure"