1

I'm trying to have a simple PowerShell script start at Windows session login or logoff...

the script runs ok with manual launch the script runs ok when launched by scheduled task at a specific time (and I'm logged in as admin) But it doesn't run with a launch at logon or logoff.

In the task scheduler I have the following :

my account (with admin rights) is designated to run the script

the button "run if not connected" is on

the box "execute with highest priv" is checked

trigger : "when connecting to a win session" / any user / local connexion / Advanced parameters "Activate" box is checked

action : powershell.exe with arguments : -noprofile -executionpolicy Bypass -File .\wlem.ps1 / start in c:\public

no condition box is checked

Thanks for the help :-)

  • 1
    In task scheduler, are there any errors in the history tab? – Narzard Jan 19 '17 at 21:49
  • Are you SURE it's not starting at all, and not just starting and then failing? – Ƭᴇcʜιᴇ007 Jan 19 '17 at 21:52
  • 1
    Do you actually want log on (use the trigger At log on)? I think On connection to user session only applies connecting to an existing session. For logoff, use On an event with the System log, Winlogon source and event ID 7002. – Bob Jan 19 '17 at 22:46
0

This is a easy fix:

navigate to here: C:\Users\ YOURUSERNAMEHERE \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup

Drop a shortcut to your script (notice it has to be a shortcut)

You can make a shortcut of your script by sending it desktop, and use that shortcut on desktop. place that shortcut in the startup folder.

That's it! Simple startup trick i learned a while back.

  • 1
    This doesn't help with logoff, and also doesn't help if there are other reasons to use task scheduler (reporting, AD, etc.). – Bob Jan 19 '17 at 22:33
  • i understand a network solution is needed, you could always try robocopy to get the script to the user logging on, or after a user environment is created. This is a solution that works, but isnt a foolproof method. – DeerSpotter Jan 19 '17 at 22:45
0

I can only get this to work with the following changes:

  • Run the task as SYSTEM.
  • Specify the full path to the script file with the -File switch, even though you already set the starting directory.

If you're using a laptop, make sure the task isn't set to only start on AC power.

You can specify -WindowStyle Hidden as well if you like, though it isn't strictly necessary. Also, you only need to check Activate and set a date if you don't want the task to start firing until that date/time.

As Bob mentioned in the comments, you might consider using the logon trigger and the event trigger for logoffs instead of these connection and disconnection triggers. The ones you're currently using will fire every time user switching happens, not just at logon and logoff. If you're willing to abandon Task Scheduler, you could switch to Group Policy logon/logoff scripts, which are fairly easy to manage.

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.