Windows Server - string manipulation in vbs or vb.net

Asked By Jesper Ravn
09-Nov-09 05:57 AM
Hello

Im retriving users out from a security group in Active Directory.
The output is like the string below.

StrMember = "CN=ctxadmin,OU=ITservices,OU=RootDomain,DC=company,DC=com"

How can I clean the string, so it always will list only the username.
StrMember = "ctxadmin"

Thanks,

/Jesper
Active Directory
(1)
NetBIOS
(1)
SAMAccountName
(1)
StrUserDN
(1)
VBScript
(1)
NameTranslate
(1)
CreateObject
(1)
StrUserName
(1)
  Tom Lavedas replied to Jesper Ravn
09-Nov-09 08:37 AM
,DC=3Dcom"

Maybe ...

StrMember =3D
strMember=3DReplace(Split(strMember. ",")(0), "CN=3D", "", -1, 1)
_____________________
Tom Lavedas
  Jesper Ravn replied to Tom Lavedas
09-Nov-09 08:37 AM
Hi Tom

Thanks, for your suggestion, but the code sample fails. I also tried to
change "StrMember. " to "StrMember," without any luck.
I just ran the code in a VBScript file.

/Jesper
  Tom Lavedas replied to Jesper Ravn
09-Nov-09 06:57 PM
2f-a4b5-c6d8145c2526@k4g2000yqb.googlegroups.com...
any,DC=3Dcom"

Sorry, I did that from memory and without testing.  There is a typo
and a missing argument (which you could have researched), but the
concept is valid.  Try this ..

strMember =3D Replace(Split(strMember, ",")(0), "CN=3D", "", 1, -1, 1)

_____________________
Tom Lavedas
  Jesper Ravn replied to Tom Lavedas
09-Nov-09 09:19 AM
Hi Tom

Great, it is working now. Thanks a lot.

/Jesper
  Richard Mueller [MVP] replied to Jesper Ravn
09-Nov-09 09:36 AM
However, note that the result is the "Common Name" of the user, not what
most people call the username, unless the two values have been made always
the same in your environment.

The value of the "cn" attribute is the Common Name of the object. It must be
unique in the OU, but there can be several objects with the same Common Name
as long as they are in different OU's or containers. The value of the
sAMAccountName attribute is the "pre-Windows 2000 logon name" (on the
NT name of the user. This must be unique in the domain.

If the cn and sAMAccountName attributes are always the same in your domain,
you can parse the Distinguished Name of the user. Otherwise, you should use
the NameTranslate object to convert the Distinguished Name into the
sAMAccountName. See this link for details:

http://www.rlmueller.net/NameTranslateFAQ.htm

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
  Jesper Ravn replied to Richard Mueller [MVP]
09-Nov-09 10:01 AM
Hi Richard

Thanks for the heads up. I will take a look at your great FAQ ressource.

/Jesper
  Richard Mueller [MVP] replied to Jesper Ravn
09-Nov-09 11:06 AM
A quick example converting a DN into sAMAccountName:
========
Option Explicit

Dim strUserDN, objTrans, strUserName

' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

' Specify user Distinguished Name.
strUserDN = "CN=ctxadmin,OU=ITservices,OU=RootDomain,DC=company,DC=com"

' Use the NameTranslate object to convert Distinguished Name
' into sAMAccountName.
Set objTrans = CreateObject("NameTranslate")

' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the Distinguished Name of the object.
objTrans.Set ADS_NAME_TYPE_1779, strUserDN
' Use the Get method to retrieve the NT format of the name.
' This format is <NetBIOS-Domain-Name>\<sAMAccountName>
strUserName = objTrans.Get(ADS_NAME_TYPE_NT4)
' Remove NetBIOS name of domain.
strUserName = Mid(strUserName, InStr(strUserName, "\") + 1)

Wscript.Echo strUserName

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--
  Jesper Ravn replied to Richard Mueller [MVP]
10-Nov-09 06:54 AM
Hi Richard

Nice, thanks for all your help.

/Jesper
Create New Account
help
creating user from Active Directory Windows Server Hi, I like to know how to write a script to create a user home directory from Active Directory and assign permission with it. Windows Server Active Directory Discussions Active Directory (1) VBScript (1) ObjFSO (1) CreateObject (1) StrHomeDrive (1) IntRunError (1) StrHomeDir (1) GetObject
how to register non-active directory computer in a active directory integrated DNS server Windows Server Hi, I have a windows 2003 server active directory with 3 DCs being also DNS servers. They have a forward lookup zone integrated with of zone should I add? Thank you very much. David Fernandez. Windows Server DNS Discussions NetBIOS (1) WSHShell (1) CreateObject (1) CSci (1) Achiever (1) Trainer (1) Month (1) June (1 again (If needed). http: / / technet.microsoft.com / en-us / library / cc959611.aspx - - Paul Bergson MVP - Directory Services MCTS, MCT, MCSE, MCSA, Security+, BS CSci 2008, 2003, 2000 (Early Achiever), NT4 Microsoft
How to script the (ADUC) user properties \ member of: Name and Active Directory Folder Windows Server I've been trying to write a vbscript that will list the groups that a user is a member of and the "Active Directory Folder" as it's displayed in the Active Directory Users and Computers / User properties / member of / name and Active Directory Folder. I've managed to get a list of groups with the users primary group
Can not connect to Active Directory Printer Windows Server We have a multi server Active Directory Domain Domain (one tree) all our Servers are WIndows Server 2003 R2 running Vital QIP a Windows 2000 Server for our File and Print Server, all printers are registered in Active Directory. When we try to add the printer through the Printer Wizzard using "Find a printer in the directory" option we get the following error message: name correctly; and that the printer is connected the server using the \ servername \ printer name command. Thank you for your help. Windows Server Active Directory Discussions Active Directory (1) NetBIOS (1) NBTStat (1) NSLookup (1) HerbThank (1) WIndows (1
Adding a Second Domain Controller with Active Directory Windows Server I'm new to Windows 2003 Server so this question may sound pretty basic. I have a domain controller with active directory installed. I want to add a second domain contoller to this domain. When I try to install active directory, I am asked the name of the existing domain. When I provide the name, I it cannot find the domain. This only seems to happen when I try to install active directory. I can add servers without active directory without incident. Any assistance would truly be appreciated