How to run something after Windows logon is fully done? - shell

I have a system service running on my Windows machine that can impersonate the currently logged on user and launch applications on their behalf (including UI apps).
It works fine when the user is already logged on into their interactive session, their Desktop is created, and so on.
The startup of the service is Automatic, so it is started automatically after each reboot. If it attempts to run some program (that needs UI access) immediately after the service is started, that program may fail - possibly because the autologon process is still in progress, the Desktop is not created yet, etc.
The question is: if the service starts "early", how can it wait for the interactive session to be fully initialized (apart from waiting some arbitrary time, which is not optimal)?
Or, can the service be started "late"? Is there a registry key, or a folder, or something else, that I can use to delay the service startup to the moment when the Shell is ready and it is safe to launch UI applications?

The easiest two ways to "execute some code when a user logs on" is to write a .bat file, and either:
1) Put the .bat file in the startup folder
<= Note: Windows 7/Vista has a new location for "Startup folder"
... or ...
2) Create a new Task that calls the .bat file at login
I option "2)" gives you finer control. It also allows the .bat file to "Run as Administrator" if needed.
If you'd like to make the .bat file pause briefly (e.g. to make sure things are completely initialized", you can add "ping -w" to your .bat file.
EXAMPLE:
#rem Waits 5 seconds before continuing
ping 1.1.1.1 -n 1 -w 5000 > nul

Related

Windows Batch File for Login Shortcut

I know you can use tsdiscon.exe and tscon.exe to connect to a user account via the command line.
I want to write a simple batch file to accomplish this, so I can have a shortcut on my desktop that will quickly and easily login to another user account on this system.
I share this computer with my in-laws, and they are not very computer literate. Currently there are many steps in switching to their account, and I want to make it more streamlined for them.
It's been awhile since I've written a batch file, but I am sure there is a way to run a simple IF/THEN statement, that would check to see if the user is logged in, then either log them in with the given credentials, or switch to that users profile.
I want to know the syntax to make this happen, so I can put the .bat file on the desktop for them to click on.
Is this possible?
Logging out to your account? Suppose impossible.
Suppose your user is usr1 and you save your batch file to, say, your desktop.
Then, by running this batch file, you run it as usr1.
If your batch file contains the log out statement, it therefore ends the user account, therefore terminating the batch file, thus not being able to run the batch file successfully.
However, switching to a different account could be possible.
Try this out, if it works.
Essentially, you're doing the following:
#echo off
taskkill /IM explorer.exe /F
runas /user:COMPUTERNAME\ACCOUNTNAME explorer.exe
(Replace COMPUTERNAME and ACCOUNTNAME with the current computer name and account name (i.e. C:\Users\ACCOUNTNAME) of your in-laws account)
If that's not what you wish, then, sorry, I can't help you.
The batch script I previously mentioned here
Just give your machine a simple name customise one line in script. Test from command prompt:
fus
If it works with win10 you should be able to create two shortcuts with commands:
fus.bat 1
and
fus.bat 2

Automating user interaction from within Session 0

I am running on Windows Server 2012. I have a service that creates several processes, one of them is a GUI client. The process in high level is as follows:
Service runs a launcher exe
The launcher exe launches an AutoIT compiled exe
The AutoIT compiled exe launches a winform exe
The winform exe does two things:
a. It write a file to the disk on program startup (test.log)
b. It waits for a keyboard press "c" to write a second file to the disk (test2.log)
The AutoIT script does two things:
a. It launches the said winform exe
b. It sleeps for 5 seconds and then send "c" (Send command)
If I start running this chain manually (from step 2) - everything works fine and 2 files are being created. I guess this happens because I have an interactive session (specifically 2) so it's a no brainer here. However, if I run it from the service, everything runs in session 0 (I verified this with ProcessExplorer) but only the first file is being written. Meaning, the keystroke message was not read by the winform exe.
I read about Session 0 isolation which means that user input from other sessions can't interact with programs (services) running in session 0. However, all of the processes mentioned now run in the same session 0. So I'm not sure why my application doesn't get the keyboard strokes? Any workaround for this? Please note that running under a different session is probably not possible, as the server must run without user logged on and as far as I know, you can't launch a session yourself without logging into the system.
Any help is appreciated.
Thanks!
Your service probably runs under Local System or Local Service account (it's always "logged in" in some sense). But you can change this behavior to allow the service running under any specified account with access to the desktop.
Go to menu "Start->Computer->Manage".
In the "Computer Management" window select "Services and Applications->Services".
Find your service and call "Properties" by popup menu.
Open "Log on" tab.
You have 2 options here:
a. Choose "Local System account" radio button and check "Allo&w service to interact with desktop".
b. Choose "This account" and type or "Browse..." the account you need.
Option (b) could make sense if you set up an auto logon on the machine (in this case you may run as Administrator and have an access to the desktop simultaneously).
Option (a) is applicable only if you don't need running as Administrator. Hope it helps.

VBS: Display message on logon screen

It is possible to set up Windows so that it displays verbose messages during startup or shutdown.
I'm writing a VBScript procedure which will be ran at startup and I would like to display a meaning message instead of the default "Running startup scripts".
How to achieve this in VBS?
You need to use Windows administrative tools which allow you to configure start up for specific users or users group to execute anything on start-up, shutdown even change their backgrounds. Network policy maybe useful for that as well if you are on domain controlled environment.
Also you can use MSCONFIG on a single system to add or define what ever you want to run at startup, for as long as it is valid windows program.
Once you configured what script to run use Windows console (run that under wscript.exe) to execute your VBS file with what ever you want to put there (with wscript you will get dialog with your text in it).
Usually it should look like
Wscript.Echo "Meaningful message goes here"
If you run that under cscript.exe you'll get output in your console window.

How to display the main window of a process in another Windows session?

When running a Scheduled Task under Windows to launched batch processes under a dedicated account, you could want to see output of theses processes.
These sub-processes could be either or both graphical or character based applications
As there are running in a different Windows Logon session, I cannot find way to display their output. There are like ghost processes
I would expect being able to use the show Window command from Process Explorer, but even via running Process Explorer under the dedicated account used to run the task (via runas), it is not working
Is there a way to do it ?
References : http://www.brianbondy.com/blog/id/100/
I believe that once created, a window cannot be transferred to another window station. Also, I doubt it is possible to open windows in another session. For character-based applications you are better off writing the output to some file and examining that file. For graphical applications you are out of luck with task scheduler. You may want to write a simple VBS script that wakes up at a certain time and runs your graphical task(s) and run it upon user logon, e.g. along the lines of a script from this page:
http://social.technet.microsoft.com/Forums/scriptcenter/en-US/d9465815-87e2-43df-a0fe-4a23c16dca99/need-a-time-schedule-in-vbs-script-to-execute-something

Launch batch script before windows login window

I'm working on Windows Server 2003 and Windows server 2008.
I would like to start a batch script just before the login window is displayed.
I read about adding the script in the Group Policy editor (startup/shutdown) but it's not working (it worked for logon and logoff though).
An alternative would be to use the task scheduler service but it's not working either. Plus, I want to be able to see the results in a cmd window. That means the last alternative - creating a service to do the job - is not an option.
Do you have any idea ?
Thanks.
Control Panel -> Scheduled Tasks -> < follow the prompts>
Not as nice as a service, which you can start and stop through well-behaved commands, but if it is a service you want running at all times, it does the job.
Try adding your script as a user defined service
That way it will get executed before the login prompt;
http://support.microsoft.com/default.aspx?kbid=137890
Though this article says it can't be used for batch scripts, you can call the script from an executable.

Resources