Link to home
Start Free TrialLog in
Avatar of wylde342
wylde342

asked on

machine login script for each user

Hello,

We're running Terminal Services on Windows 2003 Std.  I have an application that requires an environment variable "station" to be set to a 2 or 3 digit number for tracking.  I need some way to run "setx ##" for each user that logs on to the terminal server and have the ## be different for each.

How can this be done?
Avatar of Jeffrey Kane - TechSoEasy
Jeffrey Kane - TechSoEasy
Flag of United States of America image

That's no problem at all Server 2003 TS has this capability natively.  Please see the "Editing User-Specific Logon Information" seciton in this TechNet article:  http://technet2.microsoft.com/WindowsServer/en/Library/6caf87bf-3d70-4801-9485-87e9ec3df0171033.mspx

Jeff
TechSoEasy
try:
 set x=%sessionname:~8,9%
Should equal a 2 digit unique number

Sessionname is a default variable assigned to the session. Usually  returns  RDP-tcp#**  with ** being the session number. The :~8,9  will return only the last 2 (8th - 9th) characters
Avatar of wylde342
wylde342

ASKER

Tech - how would you set the usrlogon.cmd on a per user basis?  Again, I need to set this variable to a different # for each user when they login.  Could I use something like "If user x, set x station ##?"  Please forgive my lack of terminology, I suck with programming.
As I was looking for some example code to give you for this, I was thinking that if this is an application that has sold more than two or three copies, I am sure they would have addressed this issue already.  What I've seen is that when a unique station number must be assigned to a user, that information is kept in an .ini file within the application's folder.

I don't know if this is the application you are trying to use, but as you can see by their Terminal Server instructions, this is how it's handled:  http://www.legalmaster.com/lm04011.htm

I would suggest that you contact the software manufacturer or look at their support site to determine the appropriate method for that particular program.  Because if it does read an .ini file when a user logs on you definitely want to go that route.

Jeff
TechSoEasy
The :
set station should work fine for you if you add it to the AutoExcec.nt file in  C:\Windows\System32\AUTOEXEC.NT
We have a couple of older applications that require a station 'name' and accomplish it with the command in that application.
To test the output, at a command line enter:
echo %sessionname:~8,9%

Thanks for the tip; but again, how can I run this on a per user basis?  Each instance needs to have it's own "setx" variable assigned.
>>"how can I run this on a per user basis?  "
Sorry overlooking the fact that this is a TS session. You could not use AutoExec.nt, however, I believe adding it to the users logon script, it should remain native to the session similar to the tmp, username, etc. variables. I cannot test here right now.
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Kane - TechSoEasy
Jeffrey Kane - TechSoEasy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Jeff, I tried the variable on a couple of TS's when I was the only user, so I assumed it was always 2 digit, however.....never assume.
I did however try using set=%sessionname:~8,10%  looking for 3 digit and it returned only the two. That simply proves you wouldn't get an error if less than the requested number of digits.
If necessary to have 2 or 3 digits, I could modify to always have a false first digit. However, I like Jeff's idea of modifying each user's logon script to have a unique ID number. Would be a consistent #, better for troubleshooting/tracking, and less of a risk of failure. However, I can appreciate if you have dozens of users you don't want to have to create a different logon script for each.
Just an FYI to wylde342,
The difference in using set vs setx is that you need an "=" between the command and the variable.  Otherwise they do the same thing.

Rob... I was thinking you could change the name of the RDP session (which is possible) but it will stick the "#" in there no matter what.

Jeff
TechSoEasy
Jeff, Tech

I'm giving you the points for the amount of work you did.  However, I did figure it out a different way and it's detailed below.  Yeah, it was a bit of a pain to setup, and it's quite manual, but it works exactly how I wanted.  Plus, I figured it out myself (with tips from EE) which is always key for the future.

C:\windows\system32\usrlogon.cmd has a "Call c:\windows\system32\users.cmd"

users.cmd

@echo off
IF "%username%" == "user1" goto user1
IF "%username%" == "user2" goto user2
(etc up to 25)

:user1
setx station 50
exit

:user2
setx station 51
exit

(etc, etc)
Oh, that's brilliant!  If you (or I, or Rob) were a bit more skilled in scripting, I know you could have used a formula instead of the manual format.  In fact you may want to post that over to the http:VB_DOT_NET TA as well as the http:Visual_Basic TA because there are a number of scripting folks over there that may be able to tune it up for you.  Because I think to have it parse that more manual instruction may slow things down a millisecond or two.  :-)

You also could send it to the Microsoft Scripting Guy to see if there's a better way... because now that you have this actually working and in writing, it's much easier to understand what you are trying to do and I'm sure that someone can come up with the "best" way.

Hey, Scripting guy! http://www.microsoft.com/technet/scriptcenter/resources/qanda/default.mspx

Jeff
TechSoEasy