?
Solved

Install java silent on Terminal server, logoff all but me script?

Posted on 2014-04-22
7
Medium Priority
?
684 Views
Last Modified: 2014-04-28
Hi
I need a script to logoff all users, drain server, install java, and then reboot.
I can't figure out how to logoff all but me.
I know "logoff 65536" should log off all sessions. But as far as I can tell, that will also stop my script.
I imagined script looking something like this:
logoff 65536
change logon /disable
change logon /drainuntilrestart
change user /install
c:\temp\java\java.exe /s
shutdown /r
0
Comment
Question by:mathiesen-data
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 29

Expert Comment

by:matrixnz
ID: 40016581
You could use PowerShell http://poshcode.org/3688
0
 
LVL 25

Expert Comment

by:Coralon
ID: 40016622
logging off the listener is definitely *not* the way to go.. and you can see exactly why.

You'll want to do something like this:

change logon /disable
for /f "tokens=1,2,3,4,5 skip=3" %%f in ('quser') do if %%f==%username% (echo .) else (logoff %%h)
waitfor /t 120 MYJAVASIGNAL
change user /install
c:\temp\java\java.exe /s
change user /enable
shutdown /r /t 0

Open in new window


The for command will skip to the 3rd line of the output of quser, and then it will check the first "token" (which is the user name) and compare it to the current username (which is you).  If they match, it does nothing, otherwise, it attempts to logoff the user sessions.
Once that is done, it will wait for 2 minutes (cheater way of doing a sleep type command without downloading the actual sleep.exe command from Microsoft., but it works like a champ).
Then it starts install mode.. and then installs java.  (Obviously you'll need to make sure your java installation syntax is correct).
Then it re-enables the logon mode - very very important.  If you reboot without re-enabling, after it reboots, it will come back up still disabled.  
Finally, it reboots.  Your time delay between re-enabling logons and the reboot should be less than 1 second, so there is virtually no chance of killing a connection.

I've never seen or heard of a drain option for change user.. (maybe it exists in 2012?), but I'm going with the assumption it doesn't.  Also, the fact that you are already logging the user sessions out sequentially means they should logout with no problem.  

If you need error checking (like maybe the sessions are not autoending, then you could do something like this..

change logon /disable
for /f "tokens=1,2,3,4,5 skip=3" %%f in ('quser') do if %%f==%username% (echo .) else (logoff %%h)
waitfor /t 120 MYJAVASIGNAL
for /f "tokens=1,2,3,4,5 skip=3" %%f in ('quser') do if %%f==%username% (echo .) else (reset %%g)
waitfor /t 120 MYJAVASIGNAL
change user /install
c:\temp\java\java.exe /s
change user /enable
shutdown /r /t 0

Open in new window


The addition is the extra line to forcibly reset any sessions that did not logoff properly.  

Coralon
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40017167
I'd rather not use Powershell, as I have to go to every machine on several customers to enable it.

I still get logged of with the error checking script when logged in using a rdp client.
The problem with token 3 is that disconnected sessions is token 2...

 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
>admin                 console             0  Active          .  23-04-2014 12:53
 user                                 2  Disc        none   22-04-2014 16:34
 admin                                     3  Disc        none   22-04-2014 16:34
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 2

Author Comment

by:mathiesen-data
ID: 40017190
Output, I changed logoff to Echo to see what output was.

for /F "tokens=1,2,3,4,5 skip=3" %f in ('quser') do if %f == administrator (echo . )  else (echo %h )

C:\>if mdata == administrator (echo . )  else (echo Disc )
Disc

C:\>if >administrator == administrator (echo . )  else (echo 1 )
1

So it seems that it takes the State instead of ID. I tried changing skip=3 to 2, but if didnt seem to help.
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40017387
Found this script on the site http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/Q_24189994.html
This script seems to Work.
Searhing for all sessions with rdp in it, except for the session that starts with >rdp (current session) and logs off.

Afterwards it logs off all disconnected sessions.


@echo on
@CLS
cd\
for /f "tokens=3" %%a in ('QWinSta ^| find /i "rdp" ^| find /v ">rdp"') do (if %%a LSS 65536 logoff.exe %%a)
for /f "tokens=2" %%i in ('QWinSta ^| Find /i "Disc"') Do logoff.exe %%i

Open in new window

0
 
LVL 25

Accepted Solution

by:
Coralon earned 2000 total points
ID: 40019186
Very similar technique.  I didn't have a chance to debug it.. I was just writing it on the fly from my home machine.  (Windows 8.1, so I don't even have the q* commands anymore.

So you would just need to modify the parameters of the FOR commands.

The way it works, is you specify a variable (%f, %A etc. and they are case sensitive).
When you use the /f option, you can specify the number of tokens.  These are white space delimited character groupings. Each of the subsequent tokens is assigned the next variable in sequence.  So, if I do a for /f "tokens=1,2,3" %f in....  then on each line the first token will be %f, the second token will be %g, the 3rd will be %h.  (And again, because they are case sensitive, %f is not the same as %F.

The skip simply tells it to skip a few lines before it begins processing.  I can't remember if it skips # number of lines, or skips to line number #.  

It is interesting telling it to use multiple tokens with just a single number..   I'll have to do some testing with that for myself :-).

My concern with the script you used however -- It doesn't take into account a few possible factors:
1. If you have ICA sessions - I know you didn't mention Citrix, but I always try to assume it's a possibility.
2. If you run it from an RDP session.. you will potentially screw yourself with that one since it is logging off all of the RDP sessions.
3. It also does not take into account any sessions that refuse to logoff & get hung in the process.
4. It does not take into account a session that goes into a down state.. (this used to be a huge problem).
5. It does not wait to be sure the sessions logout -- if you start your install instantly after the logoffs, then there is a chance your java install may have issues.


But, combining the two, you should be able to take it all into account..
change logon /disable
for /f "tokens=3 skip=3" %%f in ('quser ^| findstr /i "disc"'') do logoff %%f
waitfor /t 120 MYJAVASIGNAL
for /f "tokens=3 skip=3" %%f in ('quser') do if %%f==%username% (echo .) else (logoff %%h)
waitfor /t 120 MYJAVASIGNAL
for /f "tokens=3 skip=3" %%f in ('quser') do if %%f==%username% (echo .) else (reset %%g)

change user /install
c:\temp\java\java.exe /s
change user /enable
shutdown /r /t 0

Open in new window


I also use quser instead of qwinsta, because qwinsta shows you the listeners, while quser does not.  Either one of them is fine, you just have to adjust the skip lines and the tokens being used.


Coralon
0
 
LVL 2

Author Comment

by:mathiesen-data
ID: 40026994
My concern with the script you used however -- It doesn't take into account a few possible factors:1. If you have ICA sessions - I know you didn't mention Citrix, but I always try to assume it's a possibility.
1. Not using Citrix
2. If you run it from an RDP session.. you will potentially screw yourself with that one since it is logging off all of the RDP sessions.
2. I am not screwing myself, because actually the session running this script has an < in front of it, thereby not being logged off Cool eh :)

3. It also does not take into account any sessions that refuse to logoff & get hung in the process.

3. True.
4. It does not take into account a session that goes into a down state.. (this used to be a huge problem).
4. Never seen those fortunatly.

5. It does not wait to be sure the sessions logout -- if you start your install instantly after the logoffs, then there is a chance your java install may have issues.
5. Ah yes, the script is much longer than posted here. I have some more checks.

I'll Award you the points for spending the time with me on this.
I'll might end up using the extra error checks.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This tutorial will walk an individual through locating and launching the BEUtility application and how to execute it on the appropriate database. Log onto the server running the Backup Exec database. In a larger environment, this would generally be …
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question