Solved

How to get username into a batch file on Windows 98/95

Posted on 2002-03-13
15
698 Views
Last Modified: 2013-12-28
Our users log on to our Windows NT domain whenever they begin using a Windows 95/98/NT or 2000 computer. Normally our logon script, running on the PDC, maps certain drives to shares on our NT server whenever someone logs on to the network. However, we have been seeing, intermittent, seemingly random cases where this automatic mapping is not occuring. It is difficult to troubleshoot because when we WANT the problem to happen so we can analyze it, mapping always works fine.

As a workaround, I want to make a batch file that the users can run when this happens. The batch file would use the net use command to map these drives. I could make a separate batch file for each user who logs on at each computer but that would be very tedious for the number of users I support. I would prefer to use the sytax below:

net use X: \\servername\%username%

so that the actual username would automatically be filled in. This works on Windows NT and 2000, but Windows 98 and 95 do not automatically set this variable. Does anyone know of a way or utility that will allow a batch file running on a Windows 98/95 machine to get the username?
0
Comment
Question by:donander
  • 4
  • 3
  • 3
  • +4
15 Comments
 
LVL 3

Expert Comment

by:pleasenospam
ID: 6862647
Try this from the DOS prompt and look at the date:

cd c:\
dir /s *.pwl /p

0
 
LVL 3

Expert Comment

by:CyberStretch
ID: 6862779
Ref: Windows 95 Logon Script does not Process Correctly (Q169227)
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q169227

=== Quote ===

There is an environment variable such as %USERNAME% specified in the logon script. Windows 95 does not understand all of the environment variables that Windows NT does. In order to have this type of functionality, you must use a third-party script application, such as KiXtart.

=== End Quote ===

Looks like W9x does not support environment variables.

I also tried please's suggestion and, unfortunately, for me it did not work. The date shown, even though I logged in today, is from February not March. So, it would seem the only time the date changes is when the user's credentials change. (I changed my password on the date of the file.)

However, if you use the NET CONFIG command from the batch file and search for the string after User Name, it does supply the currently logged on user. Therefore, it must be stored somewhere.

It has been awhile since I created batch files, but someone else may know how you can extract this information. You may have to pipe it into a file and have the batch search through the file for the string right after User Name.

0
 
LVL 6

Expert Comment

by:bartsmit
ID: 6862809
Convert your login script to Kixtart (http://www.kixtart.org/) this offers much more functionality on both Win9x and NT clients.

Kixtart was introduced as part of the Microsoft Windows NT Resource Kit but has been steadily improved since then. It is freeware.
0
 
LVL 16

Expert Comment

by:Kyle Schroeder
ID: 6862817
CyberStretch mentioned a handy addon util, Kixtart, for your NT Logon script, I would highly advise checking it out.  In addition to pulling env. variables, it can apply registry settings, etc.  From the homepage:

The KiXtart free-format scripting language can be used to display information, set environment variables, start programs, connect to network drives, read or edit the registry change the current drive and directory and much more.

http://www.kixtart.org

-dog*
0
 
LVL 6

Expert Comment

by:bartsmit
ID: 6863809
well spotted, dogztar, I missed CyberStretch's reference to kixtart.

CyberStretch; apologies, no poach intended.
0
 
LVL 7

Expert Comment

by:WiZaRd
ID: 6865038
we have a similar problem at work. we turn on all the computers (including the one with the mapped drives)when we start in the morning. occasionally we forget & log in to a "workstation" first and of course get the couldn't log a mapped drive error. if the machine with the mapped drives is logged in first then there is usually no problem provided there are no other network problems.
0
 

Author Comment

by:donander
ID: 6865495
First I must explain that the logon script that runs on our PDC is a Kixtart script. I believe the Kixtart script gets the username from the master account domain computer, which authenticates the user before passing control on to the PDC of our resource domain.

I too tried pleasenospam's suggestion but only found default.pwl with a date of 4/23/99.

Regarding CyberStretch's comment "However, if you use the NET CONFIG command from the batch file and search for the string after User Name, it does supply the currently logged on user," this does in fact supply the name of the currently logged on user but also supplies much other info. How would my DOS batch file parse the username from this output so that the batch file could put that info into the net use command?

I think the crux of this issue remains, how can my DOS batch file, which the user would run in the event that the drives don't automatically get mapped by the Kixtart logon script, get the username (called userid by Kixtart) from Windows 95/98?
0
Integrate social media with email signatures

Is your company active on social media? Do you also use email signatures? Including social media icons in your email signature is a great way to get fans for free. Let all your email users know you’re on social media quickly and easily, in a single click.

 
LVL 28

Expert Comment

by:vinnyd79
ID: 6867156
What about using a vbscript file to launch your bat file?

You could create a file and name it with a .vbs extension:

Dim objNet
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
If  Err.Number <> 0 Then
     MsgBox Err.Description          
Else
     Dim strUser
     strUser = objNet.UserName
End If
Set WShell = CreateObject("WScript.Shell")
WShell.Run "c:\mapit.bat " & strUser    



Then create mapit.bat using %1 variable :

net use Z: \\servername\%1


0
 

Author Comment

by:donander
ID: 6867199
Forgive my lack of vbscript knowledge. Is the vbscript able to get the logged on user's username from the network instead of from Windows 95/98? Is vb pre-existing in Windows or do I have to install that application to get this functionality? I don't see in the vbscript where the variable %1 gets set to the username. Perhaps you could provide a little more detail.
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 6867705
you don't need vb,you just need the windows scripting hosts on your system.You can do a test by creating a text file with the following:

Dim objNet
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
If  Err.Number <> 0 Then
    MsgBox Err.Description          
Else
    Dim strUser
    strUser = objNet.UserName
    msgbox strUser
End If

-------
Then re-name this file with a .vbs extension and run it.It should pop up a message box with the logged in username.

In the vbscript above I am running mapit.bat with the username(strUser) as an argument

Inside the batch file I am using %1 as a variable to get the first argument which is the logged in username.

net use Z: \\servername\%1


Hope this helps.

0
 

Author Comment

by:donander
ID: 6870301
Regarding vinnyd79's comment which starts "you don't need vb,you just need..." This works. I also want to log the username, the computer name, the current date, and the current time to a text file on the network so I can track when and where this file is being used to get an idea of the scope and frequency of the drive mapping problem.

I am currently getting the date and time by invoking the date and time commands in my batch file (mapit.bat in your example) but this requires the user to hit the enter key once for each of those commands and I would prefer this to run "silently" with no additional input required from the user after they run the file. It also puts

Current date is Fri 03-15-2002
Enter new date (mm-dd-yy):
Current time is  7:26:38.42p
Enter new time:

in the log file when I really just want

3/15/02
19:26:38

in the log file. Is there a way in vbscript to get the date and time in separate variables, as well as the computer name then get these into the batch file in the same way %1 was retrieved? I would then append them to the log file with something like "echo %2 >> logfile.txt." I'd like to end up with the following in the log file each time the vbs/batch file combination was run:

username
comutername
date
time

If you feel this constitutes a new question, I can accept your answer to the first question then post this one separately, or I could increase the points when accepting the answer to the additional question (I think I can do this can't I?)
0
 
LVL 28

Accepted Solution

by:
vinnyd79 earned 300 total points
ID: 6870986
I don't think this constitutes a new question or requires more points. This should work:

Dim objNet
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
If  Err.Number <> 0 Then
    MsgBox Err.Description          
Else
    Dim strUser
    Dim strComp
strUser = objNet.UserName
strComp = objNet.ComputerName
End If
Set WShell = CreateObject("WScript.Shell")
WShell.Run "c:\mapit.bat " & strUser & " " & strComp & " " & Date & " " & Time  


This will make:
username %1
computername %2
Date %3
Time %4




0
 
LVL 16

Expert Comment

by:Kyle Schroeder
ID: 6880587
Of note when using the VBScript approach...I believe you need to have Internet Explorer 4.0 at least installed to get VB scripting support in Win95 (and NT) but I would be suprised if you didn't already have it.  Actually it may be available as a packaged download also (I can find it at Microsoft if you're interested).

-dog*
0
 

Author Comment

by:donander
ID: 6880846
I tested the proposed solution and it works very well. I'm increasing the points by 100 for the "extra" information because I think people who provide clear, concise solutions and explanations should be fairly rewarded.

Since I know nothing about vbscripting, I needed a cookie-cutter solution and this is just what vinnyd79 provided. This save me a lot of time that I otherwise would have had to spend researching a solution.

Thanks,
donander
0
 
LVL 16

Expert Comment

by:Kyle Schroeder
ID: 6880990
Just FYI, the current logged on username (or last logged on if the MS Network login screen is cancelled) is stored in the registry at:

HKEY_LOCAL_MACHINE\System\Network\

(er, somewhere around there, very close to the registry root anyway...no Win9x machine around to check on, but that is where its stored).

I was considering some sort of regedit.exe option to extract the username, but the vbScript is certainly easier.

-dog*
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

For a variety of reasons, it sometimes makes sense to reboot a Windows-based computer on a regular, perhaps daily basis. This "cures" a lot of ills by resetting processes, flushing caches, refreshing memory, and reestablish network connections. In a…
Possible fixes for Windows 7 and Windows Server 2008 updating problem. Solutions mentioned are from Microsoft themselves. I started a case with them from our Microsoft Silver Partner option to open a case and get direct support from Microsoft. If s…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now