Skip to content

Commit

Permalink
Properly stop vgc and vgtray.exe, wait times parameterized
Browse files Browse the repository at this point in the history
  • Loading branch information
tmarsteel committed Feb 18, 2022
1 parent 4fe5392 commit a6e6b90
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 18 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Vanguard Control Script

Disables Vanguard by default. If it is enabled at startup, gives you 120 seconds top stop it from deactivating vanguard.
You can then enable Vanguard for a single boot (see below).
Disables Vanguard by default. If it is enabled at startup, gives you 120 seconds (configurable)
top stop it from deactivating vanguard. You can then enable Vanguard for a single boot (see below).

## Requirements

Expand All @@ -27,3 +27,11 @@ If you want to play Valorant:
* After the reboot, vanguard will be enabled.
* For the boots after that it'll be disabled again.

## Parameters

|Name |Default |Description |
|------------------|--------------------|--------------------------------------|
|Mode |`RebootWithVanguard`|`RebootWithVanguard` or `AfterStartup`|
|AllowFile |`ALLOW VANGUARD` |Name of the file, relative to the script, whichs presence allows vanguard to keep running.|
|StopDelay |120 |In AfterStartup mode: number of seconds after logon to wait until stopping/killing vanguard.|
|VgTrayStartTimeout|45 |In Afterstartup mode: number of seconds after logon to wait for vgtray.exe to start, so it can be stopped. Increase this if your PC is slow and vgtray.exe keeps running.|
73 changes: 57 additions & 16 deletions vanguard-control.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,86 @@ Param(
[ValidateSet("AfterStartup", "RebootWithVanguard")]
$Mode = "RebootWithVanguard",

[parameter(ParameterSetName="Allowfile")]
[String]
[ValidateNotNullOrEmpty()]
$AllowFile = "ALLOW VANGUARD"
$AllowFile = "ALLOW VANGUARD",

[int]
$StopDelay = 120,

[int]
$VgTrayStartTimeout = 45
)

$AbsAllowFile = "$($PSScriptRoot)\$AllowFile"

function Assure-Service-Enabled {
param([parameter(Position=0)] [String] [ValidateNotNullorEmpty()] $Name)

sc.exe config $Name "start=" "system" # ps' Set-Service can't do this
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to activate the vanguard service $Name"
Write-Host "Press enter to close the window"
Read-Host
exit 1
}
}

if ($Mode -eq "AfterStartup") {
if (Test-Path -Path $AbsAllowFile -PathType Leaf) {
Remove-Item -Path $AbsAllowFile
Exit
}

$vgkService = Get-Service -Name vgk
$stopWatch = [system.diagnostics.stopwatch]::StartNew()

$vgkService = Get-Service -Name "vgk"
$vgcService = Get-Service -Name "vgc"

if (($vgkService.StartType -ne "Disabled") -or ($vgcService.StartType -ne "Disabled")) {
if ($StopDelay -gt 0) {
Write-Host "Stopping Vanguard in $StopDelay seconds. Interrupt with Ctrl+C."
Start-Sleep -Seconds $StopDelay
}

if ($vgkService.StartType -ne "Disabled") {
Write-Host "Stopping Vanguard in 120 seconds. Interrupt with Ctrl+C."
Start-Sleep -Seconds 120
Set-Service -Name vgk -StartupType Disabled
Set-Service -Name vgc -StartupType Disabled

if ($vgkService.Status -ne "Stopped") {
$vgkService.Stop()
}
if ($vgcService.Status -ne "Stopped") {
$vgcService.Stop()
}


$vgkService.WaitForStatus("Stopped", (New-TimeSpan -Seconds 10))
} else {
Write-Host "Vanguard is disabled. Run 'sc config vgk start= system' to enable it."
$vgcService.WaitForStatus("Stopped", (New-TimeSpan -Seconds 10))
}

taskkill /IM vgtray.exe
$SecondsRemaining = [Math]::Ceiling($VgTrayStartTimeout - ($stopWatch.ElapsedMilliseconds / 1000))
Write-Host "Waiting up to $SecondsRemaining seconds for vgtray to start, so it can be stopped."
while ($stopWatch.ElapsedMilliseconds -le $VgTrayStartTimeout * 1000) {
try {
$vgtrayProcess = Get-Process -Name "vgtray" -ErrorAction Stop
}
catch [Microsoft.PowerShell.Commands.ProcessCommandException] {
Start-Sleep -Seconds 5
continue
}

Write-Host "vgtray has started, stopping..."
Stop-Process -InputObject $vgtrayProcess
Wait-Process -InputObject $vgtrayProcess
Write-Host "vgtray stopped."
break
}
}

if ($Mode -eq "RebootWithVanguard") {
sc.exe config vgk "start=" "system" # ps' Set-Service can't do this
if ($LASTEXITCODE -ne 0) {
Write-Error "Failed to activate the vanguard service vgk."
Write-Host "Press enter to close the window"
Read-Host
exit 1
}
Assure-Service-Enabled "vgk"
Assure-Service-Enabled "vgc"

echo $null >> $AbsAllowFile
Write-Host "Rebooting in 5 Seconds. Interrupt with Ctrl+C."
Start-Sleep -Seconds 5
Expand Down

0 comments on commit a6e6b90

Please sign in to comment.