Skip to content

Instantly share code, notes, and snippets.

@rgl
Last active June 4, 2026 20:25
Show Gist options
  • Select an option

  • Save rgl/19ae3a6b0a5d6f30bd9baa89a2d457c0 to your computer and use it in GitHub Desktop.

Select an option

Save rgl/19ae3a6b0a5d6f30bd9baa89a2d457c0 to your computer and use it in GitHub Desktop.
register a scheduled task that triggers when the user rdp session is (re)connected (an idea for https://github.com/thisnick/agent-rdp/pull/74)
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host "ERROR: $_"
($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1' | Write-Host
($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1' | Write-Host
Exit 1
}
$taskName = 'agent-rdp-events-handler'
$scriptPath = "C:\$taskName.ps1"
@'
Set-StrictMode -Version Latest
$ProgressPreference = 'SilentlyContinue'
$ErrorActionPreference = 'Stop'
trap {
Write-Host "ERROR: $_"
($_.ScriptStackTrace -split '\r?\n') -replace '^(.*)$','ERROR: $1' | Write-Host
($_.Exception.ToString() -split '\r?\n') -replace '^(.*)$','ERROR EXCEPTION: $1' | Write-Host
Write-Host -NoNewLine 'Press any key to continue...'
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') | Out-Null
Exit 1
}
Write-Host -NoNewLine 'Press any key to continue...'
$Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown') | Out-Null
'@ | Out-File -FilePath $scriptPath -Encoding UTF8
$subscriptionXml = @'
<QueryList>
<Query Id="0" Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational">
<Select Path="Microsoft-Windows-TerminalServices-LocalSessionManager/Operational">
*[System[Provider[@Name='Microsoft-Windows-TerminalServices-LocalSessionManager'] and EventID=25]]
</Select>
</Query>
</QueryList>
'@
$taskXml = @"
<Task version="1.4" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
</LogonTrigger>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription>$([System.Security.SecurityElement]::Escape($subscriptionXml))</Subscription>
</EventTrigger>
</Triggers>
<Principals>
<Principal>
<UserId>Administrator</UserId>
<LogonType>InteractiveToken</LogonType>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>pwsh.exe</Command>
<Arguments>-NoProfile -ExecutionPolicy Bypass -File "$scriptPath"</Arguments>
</Exec>
</Actions>
</Task>
"@
$scheduledTask = Register-ScheduledTask `
-Force `
-TaskName $taskName `
-Xml $taskXml
if (-not $scheduledTask) {
throw "failed to register the scheduled task"
}
Write-Host "Scheduled Task '$taskName' registered successfully!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment