Re: user and workstation name

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Richard Mueller [MVP] (rlmueller-NOSPAM_at_ameritech.NOSPAM.net)
Date: 11/25/04


Date: Thu, 25 Nov 2004 09:57:41 -0600

martin wrote:

> i have some simple script what is using:
>
> set Net = CreateObject("WScript.Network")
> uname = LCase(Net.UserName)
> wname = LCase(Net.ComputerName)
>
> Sometimes i get both correct information, sometimes only one and sometimes
> nothing.

Hi,

Your code snippet looks fine. It should work as written on any client with
Windows 2000 or above, and on any NT, Windows 98, or Windows 95 client with
DSClient installed.

However, the user must be logged into a domain in order to retrieve
Net.UserName. An error is raised if the user is not authenticated to a
domain. If you have "On Error Resume Next", this error is ignored but uname
is blank. The script will continue and retrieve Net.ComputerName. I cannot
think of a way for Net.ComputerName to return nothing unless you have a
client with no machine name.

If this code snippet is in a logon script, it will work as written on any
client with NT or above (if the NT client has DSClient). On Windows 95 and
Windows 98 Net.UserName will most likely be blank, but this can be fixed.
The problem in a logon script is that the user has not yet been
authenticated when the script runs. This can be fixed by using a loop to
continually retrieve Net.UserName until it is not blank. For example, I use
the following in a logon script that will run on Windows 95/98. I make sure
the Wscript version is greater than 5 because the Sleep command requires
version 1.5. The Sleep is recommended because it prevents cpu usage going to
100%.

------------------
Set objNetwork = CreateObject("Wscript.Network")

' Loop required for Win9x clients during logon.
strNTName = ""
On Error Resume Next
Do While strNTName = ""
  strNTName = objNetwork.userName
  Err.Clear
  If Wscript.Version > 5 Then
    Wscript.Sleep 100
  End If
Loop
On Error GoTo 0
-------------------

I hope this helps.

Richard Mueller
Hilltop Lab - http://www.rlmueller.net



Relevant Pages

  • Can you force a client to re-authenticate to a DC after local logo
    ... Is there a way to force a client to re-authenticate against the domain to ... force the logon script to be run, after they have already logged on locally? ... scenario typically happens for mobile users, ... This applies to Windows 2000 AD and Windows 2003 AD. ...
    (microsoft.public.windows.server.active_directory)
  • Re: XP client to NT 4 Server - Logon Scripts
    ... I am having same exact problem (XP client logging into NT4 domain). ... > Windows NT server. ... I have recently added another drive mapping to ... It seems like the NT logon script is not running ...
    (microsoft.public.windowsxp.network_web)
  • RE: Printing from Win9x clients stops
    ... Open Server Management. ... then right-click the name of the computer running Windows Small Business ... >From the client computer: ... The Select Network Component Type ...
    (microsoft.public.windows.server.sbs)
  • RE: Fax service on W2003Sbs - client dont send fax
    ... follow the steps to Update the Windows Small Business Server ClientApps ... Please paste the full content of the file to the Newsgroup. ... Microsoft CSS Online Newsgroup Support ... >the same day I do a system restore to monday;-) and client can send fax. ...
    (microsoft.public.windows.server.sbs)
  • Re: Changes in 2005.
    ... The client base I currently have makes transitioning to new technologies ... out over the UK which is Windows 2000 with SP4. ... > VS.NET 2003 does not compile to unmanaged code. ... > the JIT compiler handles the compilation to unmanaged code from IL. ...
    (microsoft.public.dotnet.languages.csharp)