$ErrorActionPreference = 'Stop'
$taskName = 'TibiaPilotNG-OSK'

Unregister-ScheduledTask `
    -TaskName $taskName `
    -Confirm:$false `
    -ErrorAction SilentlyContinue

$action = New-ScheduledTaskAction `
    -Execute (Join-Path $env:WINDIR 'System32\osk.exe')
$principal = New-ScheduledTaskPrincipal `
    -UserId $env:USERNAME `
    -LogonType Interactive `
    -RunLevel Limited

Register-ScheduledTask `
    -TaskName $taskName `
    -Action $action `
    -Principal $principal | Out-Null
Start-ScheduledTask -TaskName $taskName
Start-Sleep -Seconds 3

Get-Process osk -ErrorAction Stop |
    Select-Object ProcessName, Id, MainWindowTitle, MainWindowHandle |
    ConvertTo-Json -Compress
