I wrote a script that does some stuff with Selenium. This script runs every 3 hours. Most of the time, my client wants to log in to the server on a screen in his office to show the script doing its job. But when my client isn't logged in, it should still run.
I wrote some powershell code that, as far as I've understood, should launch the script in the session of my client but only if my client is logged in:
$user = "..."
$password = "..."
$scriptPath = "C:\Users\$user\Desktop\myscript.py"
$userSession = (query user $user 2>&1)
if ($userSession -notmatch "no User exists for $user") {
write-host "running in user session"
$sessionId = ($userSession | Where-Object { $_ -match "$user\s+\d+" } | ForEach-Object { ($_ -split '\s+')[2] })
./psexec.exe -i $sessionId -u $user -p $password python $scriptPath
} else {
write-host "running headless"
python $scriptPath --headless
}
If I run this script manually while logged in as the user, it will take the first path, run on my screen (like it should since I'm logged in), and selenium will pop up on my screen and start doing it's thing
If I trigger this script through windows task manager, no such luck. The log file confirms "running in user session" and I can see that the script is running, but nothing pops up on my screen.
What am I missing?