1

I have this powershell script scheduled (technically, it is run by a cmd file, but please see below). The script is

(New-Object -ComObject 'Shell.Application').Windows() | ForEach-Object {
   $localPath = $_.Document.Folder.Self.Path
   "C:\WINDOWS\explorer.exe /e, `"$localPath`"" 
} > reabrir.cmd

whoami >> reabrir2.cmd
pwd >> reabrir2.cmd

The first command I copied from this very site. It writes the list of (paths of) open explorer windows.

  • The script runs as intended, both when run directly from powershell and also when run from the cmd script: it writes the list of paths corresponding the open explorer windows to reabrir.cmd, and the username and working directory to reabrir2.cmd.

  • The script does run when scheduled, as it does write both the correct user name and working directory to the file reabrir2.cmd each time.

  • But, when running under the scheduler, it does not touch the file reabrir.cmd; that is, the first command fails for some reason.

  • The same problem occurs (script does run but it does not touch the file reabrir.cmd) if I set the script to be run directly by the scheduler instead of via the cmd script.

Any idea what could the reason be for the script not to run properly under the scheduler?

1

Task Scheduler tasks, when run in a different user context than the ongoing interactive session, cannot interface with that session.

The isolation rules of scheduled tasks change with each new Windows version, but try and define the task with "Run only when user is logged on". When this option is selected, your user account should be displayed under "When running the task, use the following user account:".

There is no way, in case you so wish, to interface with the desktop session of a different user account than yours.

5
  • Thanks. The task already has "Run whether the user is logged on or not" . It set to run under the user whom I want to list the windows from, and it does ask me for said user's credentials. And, like I mentioned, the script does output the right username (with whoami) and the right path (with pwd). – Martin Argerami Jun 24 '20 at 20:21
  • 1
    "Run whether the user is logged on or not" is exactly the wrong option. You need "Run only when user is logged on". – harrymc Jun 24 '20 at 20:23
  • Right. That works. Thanks a lot. I got on the wrong track by trying for the script not to open a window ("logged or not" seems to be the proposed solution to this). Now I need to figure out how to not have the powershell window open every time. – Martin Argerami Jun 24 '20 at 20:27
  • For anyone reading this, the soon to the problem with the window showing when running under the scheduler is here. – Martin Argerami Jun 24 '20 at 21:44
  • I posted my solution on the above "here" link, since none worked for me. – Leathan Dec 6 '20 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.