Skip to content

Instantly share code, notes, and snippets.

@john-shine
Forked from neggles/Set-VGpuEternalTrial.ps1
Created February 27, 2023 01:56
Show Gist options
  • Save john-shine/a20ef4e5cbacdde14f2525cbb09ca435 to your computer and use it in GitHub Desktop.
Save john-shine/a20ef4e5cbacdde14f2525cbb09ca435 to your computer and use it in GitHub Desktop.
Sets nVidia vGPU unlicensed state timeout to 24 hours and adds a daily scheduled task to restart the GPU drivers and reset the clock.
@echo off
REM Wrapper to run a PowerShell script as administrator without changing ExecutionPolicy.
REM I feel like this shouldn't work - why else have ExecutionPolicy? - but it does, soooo...
REM This should pass through arguments untouched, but cmd argument parsing is iffy; no promises.
setlocal
set ps_script_name=Set-VGpuEternalTrial.ps1
goto check_admin
REM Check if we have admin rights by running 'net session' and checking the return code.
REM Works on everything from Windows XP thru Windows 11.
:check_admin
echo Checking for admin rights...
net session >nul 2>&1
if %errorLevel% == 0 (
echo Success: Admin permissions available, proceeding with installation.
goto get_script_dir
) else (
echo Failure: This script requires admin rights to function.
echo Please right-click 'install.bat' and click 'Run as administrator'.
pause
exit /b 1
)
REM Get the directory this file is located in. Resolves relative paths.
:get_script_dir
echo Getting script path...
pushd %~dp0
set script_dir=%CD%
popd
echo Script directory: %script_dir%
goto run_ps_script
REM Actually execute the script.
:run_ps_script
echo "Running PowerShell script %script_dir%\%ps_script_name% with args %*"
powershell.exe -NoProfile -NonInteractive -ExecutionPolicy Bypass -File "%script_dir%\%ps_script_name%" %*
exit /b %errorlevel%
<#
.SYNOPSIS
Set vGPU VM instance into eternal trial.
.DESCRIPTION
Configures a Windows vGPU client for a 24-hour trial period and automatic daily driver restarts.
.EXAMPLE
Set-VGpuEternalTrial -RestartTime 2AM
.EXAMPLE
Set-VGpuEternalTrial -RestartTime 3AM -Filter '*GRID*'
.INPUTS
None.
.OUTPUTS
None
.NOTES
None
.FUNCTIONALITY
Adds two registry keys and a scheduled task.
#>
[CmdletBinding()]
[OutputType([String])]
Param (
# Restart time
[Parameter(Mandatory = $false,
HelpMessage = 'Time of day to auto-restart the driver, defaults to 3:00am local')]
[Alias('Time')]
[ValidateNotNullOrEmpty()]
[string]
$RestartTime = '3AM',
# Device friendly name filter.
[Parameter(Mandatory = $false,
HelpMessage = "Filter for FriendlyName of devices to restart, defaults to 'nVidia*'")]
[ValidateNotNullOrEmpty()]
[String]
$Filter = 'nVidia*'
)
process {
$RegistryPath = 'HKLM:\SOFTWARE\NVIDIA Corporation\Global\GridLicensing'
$RegistryProps = @(
@{
Name = 'UnlicensedUnrestrictedStateTimeout'
PropertyType = 'DWORD'
Value = 0x5a0
}
@{
Name = 'UnlicensedRestricted1StateTimeout'
PropertyType = 'DWORD'
Value = 0x5a0
}
@{
Name = 'UnlicensedRestricted2StateTimeout'
PropertyType = 'DWORD'
Value = 0x5a0
}
@{
Name = 'DisableExpirationPopups'
PropertyType = 'DWORD'
Value = 0x1
}
@{
Name = 'DisableSpecificPopups'
PropertyType = 'DWORD'
Value = 0x1
}
)
$taskName = 'Restart vGPU Driver'
$taskDescr = 'Restart nVidia vGPU device drivers daily at {0}' -f $RestartTime
$taskScript = ( '& { Get-PnpDevice -PresentOnly -Class Display -FriendlyName ' + ('"{0}"' -f $Filter) + ' | Foreach-Object -Process { Disable-PnpDevice -InstanceId $_.InstanceId -Confirm:$false; Start-Sleep -Seconds 7; Enable-PnpDevice -InstanceId $_.InstanceId -Confirm:$false} }')
try {
Write-Output -InputObject ('Setting unlicensed state timeout registry properties to 24 hours')
# Make sure the registry key exists
(New-Item -ItemType Directory -Path $RegistryPath -Force -ErrorAction SilentlyContinue | Out-Null)
# Add/overwrite the properties
foreach ($RegistryProp in $RegistryProps) { New-ItemProperty -Path $RegistryPath @RegistryProp -Force -InformationAction SilentlyContinue }
# check for existing task and remove if present
Write-Output -InputObject ('Checking for existing scheduled task and removing if present')
if (Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false
Write-Output -InputObject ('Found and unregistered existing scheduled task')
}
# Create daily restart scheduled task
Write-Output -InputObject ('Adding new scheduled task "{0}", daily at {1}' -f $taskName, $RestartTime)
$taskPrincipal = New-ScheduledTaskPrincipal -UserId 'SYSTEM' -LogonType 'ServiceAccount' -RunLevel 'Highest' -ProcessTokenSidType 'Default'
$taskSettings = New-ScheduledTaskSettingsSet # don't need any specifics here
$taskTrigger = New-ScheduledTaskTrigger -Daily -At $RestartTime
$taskAction = New-ScheduledTaskAction -Execute 'powershell.exe' -Argument ('-WindowStyle Hidden -NonInteractive -NoProfile -Command "{0}" ' -f $taskScript)
$task = Register-ScheduledTask -TaskName $taskName -Action $taskAction -Trigger $taskTrigger -Description $taskDescr -Principal $taskPrincipal -Settings $taskSettings
Write-Output -InputObject ('Registered scheduled task "{0}"' -f $task.TaskName)
} catch {
throw $PSItem
} finally {
Write-Output -InputObject ('Done!')
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment