Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieve email address from LDAP with a VBS\HTA 3

Status
Not open for further replies.

RNF0528

MIS
Apr 4, 2006
115
US
Hello ALL,


I am tring to create a way for users to send an email to our helpdesk with out using an email client. The Result is the email gets sent to the helpdesk, Picks up there IP, MAC address and usernamer and creates a workorder and emails them back. What my script does is takes the username of the user logged in and replys to it.
However I have some users that do not have the same username and email address. Is there a variable i can set to retrieve an email address from LDAP. I have tried mail but i getting an error. Script is below. I wish i could just change them to be the same...But lets just say thats not possible. My line in question is

objEmail.From = WshNetwork.UserName & "@mydomain.com"

ie...

Username = John.doe

Email = john.doe1@mydomain.com


___________________________________________________________


<head>
<title>HelpDesk</title>
<HTA:APPLICATION
APPLICATIONNAME="HelpDesk"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>

<script language="VBScript">

'__________________________________________________________phone_____________________________________________

Sub ticket

Set objEmail = CreateObject("CDO.Message")
Set FileSystem = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")
Set WshNetwork = CreateObject("WScript.Network")

'Input BOx.

strAnswer = window.prompt ("PLEASE EXPLAIN WHAT YOUR PROBLEM IS BELOW")

'Email Heading

objEmail.From = WshNetwork.UserName & "@mydomain.com"
objEmail.To = "helpdesk@mydomain.com"
objEmail.Subject = "Automated Ticket from the Public folder"


'Collectiong Info

strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colAdapters = objWMIService.ExecQuery _
("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
n = 1
For Each objAdapter in colAdapters
If Not IsNull(objAdapter.IPAddress) Then
For i = 0 To UBound(objAdapter.IPAddress)

objEmail.Textbody = strAnswer & " Physical (MAC) address: " & objAdapter.MACAddress & " IP address: " & objAdapter.IPAddress(i) & " Host name:" & objAdapter.DNSHostName
Next
End If
Next


'Email Server Config

objEmail.Configuration.Fields.Item _
(" = 2
objEmail.Configuration.Fields.Item _
(" = _
"Myserver"
objEmail.Configuration.Fields.Item _
(" = 25
objEmail.Configuration.Fields.Update
objEmail.Send

msgbox "Your work order has been Sent. If you need anything further please Call Extension. An email has been sent to your account with the work order number."

End Sub

'___________________________________________________________________________________________________________________________
</script>

</script>

<body background = "wp2.jpg">
<body>
<font color="blue" face="Times New Roman" size="4"><center>*Please click below to open a ticket with the HelpDesk*</center></font>
<p>
<p><center>
<input type="button" value=" Open a Ticket " name="run_buttonticket" onClick="ticket"></center>
<p><center>
</body>
 
Easy enough. Give this a try:
Code:
Set WSHNetwork = CreateObject("Wscript.Network")
UserString = WSHNetwork.UserName
Set objUser = GetObject("LDAP://" & SearchDistinguishedName(UserString))
emailAddress = objUser.mail
WScript.Echo emailAddress


Public Function SearchDistinguishedName(ByVal vSAN)
    ' Function:     SearchDistinguishedName
    ' Description:  Searches the DistinguishedName for a given SamAccountName
    ' Parameters:   ByVal vSAN - The SamAccountName to search
    ' Returns:      The DistinguishedName Name
    Dim oRootDSE, oConnection, oCommand, oRecordSet

    Set oRootDSE = GetObject("LDAP://rootDSE")
    Set oConnection = CreateObject("ADODB.Connection")
    oConnection.Open "Provider=ADsDSOObject;"
    Set oCommand = CreateObject("ADODB.Command")
    oCommand.ActiveConnection = oConnection
    oCommand.CommandText = "<LDAP://" & oRootDSE.get("defaultNamingContext") & _
        ">;(&(objectCategory=User)(samAccountName=" & vSAN & "));distinguishedName;subtree"
    Set oRecordSet = oCommand.Execute
    On Error Resume Next
    SearchDistinguishedName = oRecordSet.Fields("DistinguishedName")
    On Error GoTo 0
    oConnection.Close
    Set oRecordSet = Nothing
    Set oCommand = Nothing
    Set oConnection = Nothing
    Set oRootDSE = Nothing
End Function

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
One question

I am getting an ADO Security Warning

The Website uses a data provider that might be unsafe.if ok click ok.

Then it says Error :safety settings on this computer prohibit accessing data source on another domain.

It points to this line

oConnection.Open "Provider=ADsDSOObject;"

Any help would be greatful
 
This is an option in IE.

Choose Tools, Internet Settings. Click the Security Tab.
Add your site to the trusted sites list then choose Custom Level. Look for a setting that says Access Data Across Domains and set this to Enabled.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
these sorts of manips always interest me. i think the below will give you the same result and is pretty quick, not sure of its dependancies though...

Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
Wscript.Echo objUSer.DistinguishedName
 
beg your pardon

Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
Wscript.Echo objUSer.Mail

;-)
 
Mr. Movie, a star from me. Great little code snippet.

I like that this is so streamlined. I need to read up on ADSystemInfo, do you know if there is a way to provide it an ID to use besides the current user?

Note for those following the thread: A quick lookup revealed that ADSystemInfo can only be used with Windows 2000 and newer clients.

The code I posted above is flexible in that you can supply any ID to the function to get the binding to the user object, so it may be more flexible, but I really like this solution. Thanks for sharing MrMovie.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
thanks for the star Mark, i was in the process of posting a response then changed my mind.
i personally prefer your post in some respects, i hate things that are hidden, lord knows how expensive the ADSystemInfo is and as you have pointed out it doesnt work for all clients (not much does but anyway).
you example offers more flexibility in the long run and offer insight into using LDAP queries with ADO which real power comes when you want to return large recordsets of lots of users you want to manip (i have to admit i prefer the SQL query to the LDAP but there you go)


here are some other posts by others on the ADsysteminfo which might be of interest (the last one is another way of getting from netbios to ldap adspath...i think)

Here is some stuff to get you started.

'Bind to Active Directory System Information
Set adsinfo = CreateObject("adsysteminfo")

'Bind to the Domain
Set DomainObj = GetObject("LDAP://" & adsinfo.DomainDNSname)

'Bind to the active directory computer object
Set ADCompObj = GetObject("LDAP://" & adsinfo.ComputerName)


'You can determine the OU of the computer you are currently running the script on by using the parent property.
Set ADCompOU = GetObject(AdcompObj.Parent)



Dim adsinfo,nw
Set adsinfo = CreateObject("adsysteminfo")
Set nw = CreateObject("Wscript.Network")

Set ThisComp = GetObject("LDAP://" & adsinfo.ComputerName)
wscript.echo ThisComp.Description
Thiscomp.put "description",nw.username
ThisComp.Setinfo


Const ADS_NAME_INITTYPE_GC = 3
Const ADS_NAME_TYPE_NT4 = 3
Const ADS_NAME_TYPE_1779 = 1

set fs=CreateObject("Scripting.FileSystemObject")
set objTextFile = fs.OpenTextFile("c:\vbscripts\users.txt")
strNothing = objTextFile.SkipLine
Do while NOT objTextFile.AtEndOfStream
arrStr = split(objTextFile.ReadLine,vbcrlf)
strUserName = arrStr(0)

strNTName = "Your_Domain\" & arrStr(0)

Set objTrans = CreateObject("NameTranslate")

objTrans.Init ADS_NAME_INITTYPE_GC, ""
objTrans.Set ADS_NAME_TYPE_NT4, strNTName

strUserDN = objTrans.Get(ADS_NAME_TYPE_1779)

Set objUser = GetObject("LDAP://" & strUserDN)
objUser.extensionAttribute1 = "KEEP"
objUser.SetInfo

Loop

'Delete Process

set objParent = GetObject("LDAP://OU=Users,DC=your,DC=domain")
objParent.Filter = Array("user")
for each objUser2 in objParent
If objUser2.extensionAttribute1 <> "KEEP" Then
objContainer.Delete "user","cn=" & objUser2.cn
End If
Next
 
ONe question...

Does that mean i don't need to use the function that is posted above???
 
depends if you have clients that come before w2k i suppose. but then i cant warrant for the use of

Set oRootDSE = GetObject("LDAP://rootDSE")
Set oConnection = CreateObject("ADODB.Connection")
oConnection.Open "Provider=ADsDSOObject;"
Set oCommand = CreateObject("ADODB.Command")

on clients before w2k, but i would guess you would be safe which these on windows 95 if you tried
 
Using the script you guys have given me and this website below I am starting to understand the retrevial process alot better. Pretty good web site below. Thank you both for the help...



Code:
Set objSysInfo = CreateObject("ADSystemInfo")
strUserPath = "LDAP://" & objSysInfo.UserName
Set objUser = GetObject(strUserPath)
Wscript.Echo objUSer.Mail
 
have a look at you AD using adsiedit i sometimes find this useful for a list of attributes etc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top