Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Extracting user name (sorry, I'm new!)

73 views
Skip to first unread message

Foss

unread,
Sep 15, 2004, 9:33:01 AM9/15/04
to
Hi there all,

I'm using this:

Set objSysInfo = CreateObject("ADSystemInfo")
lUser = lcase(right(left(objSysInfo.UserName,11),8))

to extract the username of the person logged onto a machine.

A username is the first five letters of the persons last name, then an
underscore, then their initials.

So this is bound to go wrong when people don't have a middle name or have
little last names!

Can anyone suggest how else I can extract the username from a string?

Cheers,
Foss

Torgeir Bakken (MVP)

unread,
Sep 15, 2004, 2:00:18 PM9/15/04
to
Foss wrote:

Hi


Use the Split command:


'--------------------8<----------------------

'Set objSysInfo = CreateObject("ADSystemInfo")

' Distinguished name of the current user
'sDN = objSysInfo.UserName

' used for test
strDN = "CN=Tor_TB,OU=Test,DC=corp,OU=Test,DC=com"

' if the users are not in CN=Users:
arrDN = Split(strDN, ",OU=")
strDN = Mid(arrDN(0), 4)
WScript.Echo strDN


' used for test
strDN = "CN=Tor_TB,CN=Users,DC=corp,OU=Test,DC=com"

' if the users are in CN=Users:
arrDN = Split(strDN, ",CN=")
strDN = Mid(arrDN(0), 4)
WScript.Echo strDN
'--------------------8<----------------------


--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Al Dunbar [MS-MVP]

unread,
Sep 15, 2004, 10:08:29 PM9/15/04
to

"Foss" <Fo...@discussions.microsoft.com> wrote in message
news:DD620739-E6CF-4B8F...@microsoft.com...

> Hi there all,
>
> I'm using this:
>
> Set objSysInfo = CreateObject("ADSystemInfo")
> lUser = lcase(right(left(objSysInfo.UserName,11),8))
>
> to extract the username of the person logged onto a machine.

I think your terminology is a little bit misleading. Surely the username is
objSysInfo.UserName, and it is the user's actual name (surname?) you are
trying to extract.

> A username is the first five letters of the persons last name, then an
> underscore, then their initials.
>
> So this is bound to go wrong when people don't have a middle name or have
> little last names!
>
> Can anyone suggest how else I can extract the username from a string?

Give a couple of actual examples...

/Al


Foss

unread,
Sep 20, 2004, 8:37:02 AM9/20/04
to
Thanks very much, I'll give that a shot!

Foss

unread,
Sep 20, 2004, 8:39:03 AM9/20/04
to
Hi there,

The objSysInfo.UserName returns this:
CN=FOSSE_PJ,OU=???,OU=Administrators,OU=User
Accounts,DC=int,DC=sussex,DC=police,DC=cjx,DC=gov,DC=uk

I want to return this:
fosse_pj

Sorry if that was a bit confusing!

Al Dunbar [MS-MVP]

unread,
Sep 20, 2004, 9:37:04 PM9/20/04
to

"Foss" <Fo...@discussions.microsoft.com> wrote in message
news:245DCDAD-F168-45D7...@microsoft.com...

> Hi there,
>
> The objSysInfo.UserName returns this:
> CN=FOSSE_PJ,OU=???,OU=Administrators,OU=User
> Accounts,DC=int,DC=sussex,DC=police,DC=cjx,DC=gov,DC=uk
>
> I want to return this:
> fosse_pj
>
> Sorry if that was a bit confusing!

No problem, I think I catch on now.

The "username" value you have retrieved is the "distinguished name" of the
account in active directory. What you want to extract out of it is called by
a number of names, including CN (or canonical name), and, confusingly
enough, just "name".

Although you could indeed attempt to parse out the distinguished name to
figure out which part of it is the canonical name, the most correct way to
do this would be to get AD to do that for you.

One way to do this would be to bind to the user object using the
"distinguished name", and examine the "name" attribute. Others more adept at
this stuff here will probably be able to give you some simpler shortcuts,
but this is (approximately) how it would be done in vbscript:

set objUser = createobject("LDAP://" & objSysInfo)
theName = objUser.name

Having said all this, it just occurs to me that this still might not be
exactly what you are looking for. If your user accounts exist in more than
one OU, there is a possibility that more than one account has the canonical
name FOSSE_PJ. Although you likely see this as being the same as the good
old WinNT account name, that is only coincidental.

If what you want is a unique identifier such as the account name, you cannot
rely on this always being the same as the canonical name. If so, there are a
couple of ways to derive it in vbscript:

using ADSI:

set objUser = createobject("LDAP://" & objSysInfo)
theName = objUser.SAMAccountName

using the network object:

Set WshNetwork = WScript.CreateObject("WScript.Network")
theName = WshNetwork.UserName


/Al

Foss

unread,
Sep 21, 2004, 6:15:04 AM9/21/04
to
Mornin' Al,

Thanks very much! That worked a treat, the network one that is. It now works
on more peoples computers as well!

Cheers,
Foss

0 new messages