|
|
|
date: 26 Feb 2006 00:20:18 -0800,
group: microsoft.public.platformsdk.active.directory
back
Obtaining the username that is right-clicked on in Active Directory Users and Computers
'd like to obtain the username for the userobject that is
right-clicked on in Active Directory users and computers so that I can
use it in scripts that I've put on the context menu via ADSI edit.
What I have done is taken the user's Full Name and used string
functions to attempt to build the username. The issue is that if the
user's Full Name is not "Last name, First name" then it doesn't work.
This is obviously not the best way to find the user name; plus it only
conforms to the username convention that our network follows [First
initial][Last name].
Anyone have any ideas? There must be a proper method.
Here is the current script: (watch the wrap)
dim wshArguments
Set wshArguments = WScript.Arguments
strTempLDAPPath = wshArguments(0)
'----setting the last name as strlastname----
'taking this value and adding 1 is the beginning of the computer name
(refer to line 41)
CharIn = InStr(strTempLDAPPath, "=")
'this value is the last character to count out of the total length of
strTempLDAPPath
CharEnd = instr(strTempLDAPPath, "\, ")
CharEnd = CharEnd - 1
LastChar = CharEnd - CharIn
CharIn = CharIn + 1
strLastName = mid(strTempLDAPPath,CharIn,LastChar)
'setting the first initial of the first name to strfirstinitial
CharIn = InStr(strTempLDAPPath, "\, ") + 3
CharEnd = instr(strTempLDAPPath, ",OU")
strfirstinitial = mid(strTempLDAPPath, charin, 1)
'Username suspected (following [First initial][Last Name]
strUserName = strfirstinitial + strlastname
Example of success:
If the user's Full Name is Doe, John; the the above script will set
strUserName to JDoe.
Example of failure (common):
If the user's Full Name is Test; the above script fails at first string
function, since the string is one word.
If the user's Full Name is blank; the above script fails at first
string function, since the string is null.
Thanks for your help,
Matt
date: 26 Feb 2006 00:20:18 -0800
author: unknown
Re: Obtaining the username that is right-clicked on in Active Directory Users and Computers
Hi,
I'm not sure which name attribute you want. What many people call the
username or userID is referred to as the "Pre-Windows 2000 logon name" on
the Account Tab of the user properties dialog in ADUC. This is the value of
the sAMAccountName attribute. I discuss the various "Name" attributes in
this link:
http://www.rlmueller.net/Name_Attributes.htm
You might be able to use the NameTranslate object to convert from one form
to another. See this link:
http://www.rlmueller.net/NameTranslateFAQ.htm
You can assign values to any of the "Name" attributes except cn (the Common
Name). Since this is the relative distinguished name, you must use the
MoveHere method to rename the object to change cn.
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
wrote in message
news:1140942018.506246.204380@i40g2000cwc.googlegroups.com...
> 'd like to obtain the username for the userobject that is
> right-clicked on in Active Directory users and computers so that I can
> use it in scripts that I've put on the context menu via ADSI edit.
>
> What I have done is taken the user's Full Name and used string
> functions to attempt to build the username. The issue is that if the
> user's Full Name is not "Last name, First name" then it doesn't work.
> This is obviously not the best way to find the user name; plus it only
> conforms to the username convention that our network follows [First
> initial][Last name].
>
> Anyone have any ideas? There must be a proper method.
>
> Here is the current script: (watch the wrap)
>
> dim wshArguments
> Set wshArguments = WScript.Arguments
>
> strTempLDAPPath = wshArguments(0)
>
> '----setting the last name as strlastname----
>
> 'taking this value and adding 1 is the beginning of the computer name
> (refer to line 41)
> CharIn = InStr(strTempLDAPPath, "=")
>
> 'this value is the last character to count out of the total length of
> strTempLDAPPath
> CharEnd = instr(strTempLDAPPath, "\, ")
>
> CharEnd = CharEnd - 1
>
> LastChar = CharEnd - CharIn
>
> CharIn = CharIn + 1
>
> strLastName = mid(strTempLDAPPath,CharIn,LastChar)
>
> 'setting the first initial of the first name to strfirstinitial
>
> CharIn = InStr(strTempLDAPPath, "\, ") + 3
>
> CharEnd = instr(strTempLDAPPath, ",OU")
>
> strfirstinitial = mid(strTempLDAPPath, charin, 1)
>
> 'Username suspected (following [First initial][Last Name]
> strUserName = strfirstinitial + strlastname
>
> Example of success:
> If the user's Full Name is Doe, John; the the above script will set
> strUserName to JDoe.
>
> Example of failure (common):
> If the user's Full Name is Test; the above script fails at first string
> function, since the string is one word.
> If the user's Full Name is blank; the above script fails at first
> string function, since the string is null.
>
> Thanks for your help,
>
> Matt
>
date: Sun, 26 Feb 2006 19:34:25 -0600
author: Richard Mueller
Re: Obtaining the username that is right-clicked on in Active Directory Users and Computers
so using step 11:
11. How do I convert a Distinguished Name to an NT name?
The VBScript example below converts the Distinguished Name of any user
object to the NT form, which includes the NetBIOS name of the domain
and the NT name of the user.
' Constants for the NameTranslate object.
Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1
' Specify the Distinguished Name of the user.
strUserDN = "cn=TestUser,ou=Sales,dc=MyDomain,dc=com"
' Use the NameTranslate object to convert the Distinguished Name
' of the user to the NT Name required for the WinNT provider.
Set objTrans = CreateObject("NameTranslate")
' Initialize NameTranslate by locating the Global Catalog.
objTrans.Init ADS_NAME_INITTYPE_GC, ""
' Use the Set method to specify the RPC 1779 format of the object name.
objTrans.Set ADS_NAME_TYPE_1779, strUserDN
' Use the Get method to retrieve the NT Name.
strNTName = objTrans.Get(ADS_NAME_TYPE_NT4)
' Parse for the NetBIOS name of the domain and the NT name of the user.
strNetBIOSDomain = Mid(strNTName, 1, InStr(strNTName, "\") - 1)
strUserName = Mid(strNTName, InStr(strNTName, "\") + 1)
' Bind to the user object in Active Directory with the WinNT provider.
Set objUser = GetObject("WinNT://" & strNetBIOSDomain & "/" &
strUserName)
If the object with the specified Distinguished Name does not exist, the
Set method of the NameTranslate object will raise an erro
Should work for what I want, as long as I set strUserDN = Right
(Len(wshargument(0)) - 7), to remove LDAP://?
date: 27 Feb 2006 13:53:34 -0800
author: unknown
Re: Obtaining the username that is right-clicked on in Active Directory Users and Computers
Hi,
Yes, this snippet should convert your Distinguished Name to the NT Name. I
assume that wshArgument(0) is similar to:
LDAP://cn=Doe, Joe,ou=West,dc=MyDomain,dc=com
This is called the AdsPath of the user object (The DN with the provider
moniker "LDAP://" appended). You can use
strUserDN = Right(wshArgument(0), Len(wshArgument(0)) - 7)
or
strUserDN = Mid(wshArgument(0), 8)
Then NameTranslate object returns the NT name if the form
"MyDomain\UserName", where "MyDomain" is the NetBIOS name of the domain and
"UserName" is the value of the sAMAccountName, also called the "Pre-Windows
2000 logon name". The snippet you posted shows how to parse this into the
two strings "MyDomain" and "UserName", then bind to the object with the
WinNT provider (if desired).
--
Richard
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
wrote in message
news:1141077214.757294.181000@i39g2000cwa.googlegroups.com...
> so using step 11:
> 11. How do I convert a Distinguished Name to an NT name?
>
> The VBScript example below converts the Distinguished Name of any user
> object to the NT form, which includes the NetBIOS name of the domain
> and the NT name of the user.
>
> ' Constants for the NameTranslate object.
>
> Const ADS_NAME_INITTYPE_GC = 3
> Const ADS_NAME_TYPE_NT4 = 3
> Const ADS_NAME_TYPE_1779 = 1
>
> ' Specify the Distinguished Name of the user.
>
> strUserDN = "cn=TestUser,ou=Sales,dc=MyDomain,dc=com"
>
>
>
> ' Use the NameTranslate object to convert the Distinguished Name
>
> ' of the user to the NT Name required for the WinNT provider.
> Set objTrans = CreateObject("NameTranslate")
>
> ' Initialize NameTranslate by locating the Global Catalog.
> objTrans.Init ADS_NAME_INITTYPE_GC, ""
> ' Use the Set method to specify the RPC 1779 format of the object name.
> objTrans.Set ADS_NAME_TYPE_1779, strUserDN
>
> ' Use the Get method to retrieve the NT Name.
> strNTName = objTrans.Get(ADS_NAME_TYPE_NT4)
>
>
>
> ' Parse for the NetBIOS name of the domain and the NT name of the user.
>
> strNetBIOSDomain = Mid(strNTName, 1, InStr(strNTName, "\") - 1)
>
> strUserName = Mid(strNTName, InStr(strNTName, "\") + 1)
>
> ' Bind to the user object in Active Directory with the WinNT provider.
> Set objUser = GetObject("WinNT://" & strNetBIOSDomain & "/" &
> strUserName)
>
> If the object with the specified Distinguished Name does not exist, the
> Set method of the NameTranslate object will raise an erro
>
>
>
> Should work for what I want, as long as I set strUserDN = Right
> (Len(wshargument(0)) - 7), to remove LDAP://?
>
date: Tue, 28 Feb 2006 11:20:13 -0600
author: Richard Mueller
|
|