log off command

C

C.M.G.

I use the following command in a batchfile to force shutdown of my computer
60 minutes after issuing the command, and to reboot the computer:

shutdown -r -f -t 3600

My question is: is there a command to log off another user a certain time
after the command is issued, and force logoff of that user?

Thank you,

Clem.
 
C

C.M.G.

To clarify my question below: I am looking for a command or script to log
off another specific user at a specific time, rather than shutting off the
whole computer.

Thanks for your help.

Clem.
 
M

Matija Hrovat

Hi C.M.G,
there are two (well at least two) possibilities.
One is using the command you mentioned "shutdown"
and the other is using a free Sysinternals utility: PsShutdown
(http://www.sysinternals.com/Utilities/PsShutdown.html).

The main problem with both utilities is the timeout.
I am almost sure that there is no way to set the logoff timeout with
shutdown (there is supposed to be a way in earlier versions of shutdown, but
I didn't find it) and I could not find a way to "persuade" the PsShutdown to
use the timeout switch with the logoff switch.


So I propose that you use the "at" command to set the time;

The whole command should look something like this:
at 21:00 /interactive /every:M,T,W,Th,F shutdown -l -f

The command will log off a user ever weekday at 9 pm.


The other way to go is with the "soon.exe" utility which you can download
from the following link:
(http://www.microsoft.com/downloads/...e6-9eef-4975-b51c-8d670748ca8e&displaylang=en)
With "soon" you can set the amount of time after a specific command will
execute.
e.g.
soon /interactive 3600 shutdown -l -f

The command will log off a user after 3600 seconds.


Of course there is a lot of different ways to logoff a user using a command
line or a script. If you are familiar with vbs scripts you can create that
or you can use one of the 3rd party utilities (e.g.
http://www.robvanderwoude.com/shutdown.html) that do exactly what you want.


VBS Example:
'---Start Script
Const LOGOFF = 0
Const SHUTDOWN = 1
Const REBOOT = 2
Const fLOGOFF = 4
Const fSHUTDOWN = 5
Const fREBOOT = 6

strComputer = "."
Set objWMIService = GetObject _
("winmgmts:{impersonationLevel=impersonate,(Shutdown)}\\" & _
strComputer & "\root\cimv2")

Set colOperatingSystems = objWMIService.ExecQuery _
("Select * from Win32_OperatingSystem")

For Each objOperatingSystem in colOperatingSystems
Wscript.Sleep 5000
objOperatingSystem.Win32Shutdown(fLOGOFF)
Next

'----End Script

The user will be logged off after 5 seconds.


I hope the mentioned helps and please let us know if this is the information
you were looking for and let me know if anything is unclear in the post.

Regards,
Matija Hrovat
 
C

C.M.G.

Dear Matija,

Thanks for your comprehensive answer!

I downloaded psshutdown.exe, but I cannot make it do what I am trying to
achieve:

I have two users on my local machine. I would like to log off user2, but not
user1, 60 minutes after issuing the command, and I want the logoff to force
running applications under user2 to close. I do not want to shutdown the
computer, and I do not want to log off user1, or stop applications running
under user1.

Matija, do you think you could help me with the syntax for that command?

Thanks again,

Clem.
 
M

Matija Hrovat

Hi CMG,
sorry for a late response.

Save the following code into a .bat file.
+++++++++++

@echo off
quser %1 >user.txt
for /f "skip=1 tokens=2," %%u in (user.txt) DO SET ID=%%u
IF ID==console GOTO CONSOLE

ECHO NOT CONSOLE
SOON 3600 LOGOFF %ID%

GOTO END

:CONSOLE
ECHO IS CONSOLE
SOON 3600 LOGOFF

:END
del user.txt


+++++++++++++
Usage:
Filename.bat username
Every line starts with two spaces so correct the code if they don't.

Let us know if this is what you are looking for.

Regards,
Matija Hrovat
 
C

C.M.G.

Matija:

Thanks for writing.

I copied the text into a file that I called logoff.bat. I omitted the ++++
and the blank lines at the beginning and end, and I made sure there were two
blank spaces at the beginning of each line. I did not change the text in the
batch file at all.

I then copied the logoff.bat file to C:\WINDOWS\system32.

I then went to start/run and typed logoff.bat username

For username I used that name that appears in taskmanager under users.

Unfortunately, this has no effect on the other user who is logged on!

Any idea what I am doing wrong?

Thanks again,

Clem.
 
M

Matija Hrovat

Hey,
you should also copy quser.exe from c:\windows\system32\dllcache to
c:\windows\system32

Let us know if it works.
If it doesn't try running the script from cmd and look what errors you get
there.

Regards,
Matija Hrovat
 
C

C.M.G.

Matija,

I did as you suggested, no luck. I ran it form cmd and got this message:

NOT CONSOLE
'SOON' is not recognized as an internal or external command,
operable program or batch file.

Any other ideas?

Clem.
 
M

Matija Hrovat

CMG,
we did talk about this at the begining of your tread...

You have to download soon.exe:

(http://www.microsoft.com/downloads/...e6-9eef-4975-b51c-8d670748ca8e&displaylang=en)

or

http://tinyurl.com/wodya

Copy it to c:\windows\system32


Bellow I attached a corrected/shorter version of the code:

++++++++++++++

@echo off
quser %1 >user.txt
for /f "skip=1 tokens=2" %%u in (user.txt) DO SET ID=%%u

ECHO %ID%
SOON 3600 LOGOFF %ID%

del user.txt

++++++++++++++

Let us know if it works now.

Regards,
Matija Hrovat
 
C

C.M.G.

Matija:

I downloaded and installed soon.exe, and put a copy into
c:\windows\system32.

I've tried both the old and the new version of the code (I changed 3600 to
20, so I would not have to wait so long for testing purposes, and I added
two spaces in front of every line that contained text).

Each time I execute the command, a task is generated in taskmanager.
However, user2 is only rarely logged off. I've tried deleting all the tasks
from taskmanager and rebooting, but it still does not work consistently (it
works about 1 out of 10 times, but there is no predictable pattern). When
issuing the command from a cmd window, the following text is displayed:

1

SOON : AT 18:04:45 LOGOFF 1
Added a new job with job ID = 1

Matija, do you have any more ideas? Thanks for your help,

Clem.
 
M

Matija Hrovat

Hi CMG,
one thing to try is that you set the delay to 60 seconds or more. It does
not work correctly if it is set to less.
Let us know if that did the trick.

Regards,
Matija Hrovat
 
C

C.M.G.

Matija,

Here is an interesting little addendum that is a little perplexing:

If user1 logs off user2 from user1's logon, everything works fine. But if
user2 logs off user2 from user2's logon, then user1 gets logged off.

Any idea why this happens, and how to fix it?

Clem.
 
M

Matija Hrovat

Hi CMG,
which batch are you using ?
What are the exact usernames of the users ?
What do you get if you type:
- quser User1
- quser User2

Regards,
Matija Hrovat
 
C

C.M.G.

Matija:

I am using the following batch:

@echo off
quser %1 >user.txt
for /f "skip=1 tokens=2" %%u in (user.txt) DO SET ID=%%u

ECHO %ID%
SOON 3600 LOGOFF %ID%

del user.txt

usernames are

cmg

and

NoraTimCharlotte

The quser responses are:

USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
cmg console 0 Active . 11/16/2006
8:05
PM

and

USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME
noratimcharlotte 1 Disc . 11/17/2006
2:50
PM

Regards,

Clem.
 
M

Matija Hrovat

Hi CMG,
I'm sorry I could not reply earlier.

So if I understand you correctly;

You log into cmg
You switch to NoraTimCharlotte
You use the batch file:
logoff.bat NoraTimCharlotte
and cmd user gets logged off ?

Please tell me what command gets written in "at" after you run the batch ?

e.g.
"Logoff.bat matijah" returns:

Status ID Day Time Command Line
------------------------------------------------------------------
1 Today 9:29 AM LOGOFF console


Regards,
Matija Hrovat
 
C

C.M.G.

Matija:

You understood my most recent post correctly.

Now, I am not entirely sure what you mean by "what command gets written in
"at" after you run the batch," however, if I go to run and type cmd and
enter, I get a "dos window." If I do this from the NoraTimCharlotte logon
and if, in the "dos window" I type "logoff.bat noratimcharlotte," the
following text is displayed:

SOON : AT 22:33:00 LOGOFF console
Added a new job with job ID = 1

Is that the information you were looking for?

Thanks again,

Clem.
 
M

Matija Hrovat

Hi CMG,
is it possible that you switch the user after you run the batch file ?
If you do I am sorry but I did not know you will use it in that way.

Here is another script (which will hopefully do the trick);
If the line doesn't start with two spaces the lines must be corrected.

-----------START-----------

@echo off
quser %1 >user.txt
for /f "skip=1 tokens=2,3" %%u in (user.txt) DO (
SET ID=%%u & SET ID2=%%v
)

IF %ID%==console GOTO CONSOLE
ECHO NOT CONSOLE
SOON 3600 LOGOFF %ID%
GOTO END

:CONSOLE
ECHO CONSOLE
SOON 3600 LOGOFF %ID2%
GOTO END

:END
SET ID=
SET ID2=



-----------END-----------


If it doesn't work please post what the "at" command returns if you run it
after the batch file and please again describe when it does not work.


Thank you,
Matija Hrovat
 
C

C.M.G.

Matija:

That does the trick. I can issue the command from either user1 or user2, and
in both cases user2 gets logged off. I am sorry I did not make that clear
from the beginning.

I have two additional questions:

1, Where can I learn the scripting language? Is there a reference I can look
up?

2, Is it possible to issue the command from user1's logon to logoff user2,
even *before* user 2 has logged on? For example, if user1 issues the command
to logoff user2 in 1 hour, and user2 logs on 1/4 an hour after the command
was issued, user2 should then be logged off 45 minutes after having logged
on. (With the current script, the task to log off user2 is not created if
user2 is not logged on at the time that the command is issued).

Thanks again, and best regards,

Clem.
 
M

Matija Hrovat

Hi CMG,
about learning the script language;
There are a lot of great sites that describe script language in detail.
To list a few:

http://www.ss64.com/nt/index.html

http://www.robvanderwoude.com/

http://www.microsoft.com/technet/scriptcenter/resources/qanda/hsgarch.mspx

I suggest you start here.



Regarding the second question:

The main problem is that you are actually logging out a session.
You can logoff a sessionname or a session ID in each case you have to know
them first.
The second problem is that the not active user does not have a session name
or better yet it's empty so it's easier to work with session IDs.
I could right you (or you can try writing it your self) a script which would
get a current session ID and add 1 to it but it won't be foolproof (like the
first two scripts weren't).
Please let us know what are you trying to accomplish ? Are you trying to
control other user account so it can logon at certain hours ?
There are other means of achieving that.

It would be easier to help you if we know the whole scenario that you are
trying to do.

Let us know and I hope this helps.

Regards,
Matija Hrovat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top