Add-Type -AssemblyName System.Windows.Forms $codeFile = "C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute\oauth_code.txt" $exitFile = "C:\Users\juanm\Documents\Github\INFRAESTRUCTURA\omniroute\oauth_helper.exit" $last = "" try { $last = [System.Windows.Forms.Clipboard]::GetText() } catch {} $deadline = (Get-Date).AddMinutes(20) while ((Get-Date) -lt $deadline) { if (Test-Path $exitFile) { break } $c = "" try { $c = [System.Windows.Forms.Clipboard]::GetText() } catch {} if ($c -and $c -ne $last -and $c.Length -ge 20 -and ($c -match "#" -or $c -match "code=")) { # Limpiar espacios/saltos por si copia con formato $clean = $c.Trim() if ($clean -match "https?://") { $m = [regex]::Match($clean, "code=([^&#\s]+)") $s = [regex]::Match($clean, "state=([^&#\s]+)") if ($m.Success -and $s.Success) { $clean = $m.Groups[1].Value + "#" + $s.Groups[1].Value } } Set-Content -Path $codeFile -Value $clean -NoNewline break } $last = $c Start-Sleep -Milliseconds 700 }