Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

NEED a VBScript to query users in an AD Security Group and then send an email to the users manager

Posted on 2009-03-30
13
593 Views
Last Modified: 2012-05-06
I am needing a VBScript that query all the members a security group from active directory, and then email each of the members assigned manager.  

The subject will be the same, however the body of the email will need to have the users name at the begining and then the message will be the same for all. This way they know who the email is regarding.

So I will need to know where to specify the security group the script needs to query, and then where to customize the subject and the body of the email.
0
Comment
Question by:seaninman
  • 8
  • 5
13 Comments
 
LVL 14

Expert Comment

by:rejoinder
ID: 24056784
Can you clarify something... did you want the script to loop through a security group and list the members, then for each member, an email will be sent to the manager to include a static subject line and a body which will include the users name?
0
 
LVL 4

Author Comment

by:seaninman
ID: 24059605
Exactly correct
0
 
LVL 14

Accepted Solution

by:
rejoinder earned 500 total points
ID: 24062222
Can you try this.  It will need you to edit some of the variables so that the right group is selected and that emails are being sent through the right server etc.
I commented the areas that need your attention.
'Enter the LDAP path to the security group in question
Set objGroup = GetObject("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
 
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    strUserName = objUser.CN
    strManagerDN = objUser.Manager
    strUsersEmailAddress = objUser.mail
    if strManagerDN <> "" then
        Set objManager =  GetObject("LDAP://" & strManagerDN)
        strManagerEmailAddress = objManager.mail
        if strManagerEmailAddress <> "" then
            EmailManager strUserName,"your.email@domain.com",strManagerEmailAddress
        end if
    end if
Next
 
Sub EmailManager(UserName,FromField,ManagerEmailAddress)
    On Error Resume Next
    Dim objMessage
    Set objMessage = CreateObject("CDO.Message")
    with objMessage
        .From = FromField				'<--From field being pulled from sub variable
        .To = ManagerEmailAddress			'<--To field being pulled from sub variable
        .Subject = "Here is the subject line to change"	'<--Subject line for you to change
        .TextBody = "Here is the email body followed by the username '" & UserName & "' and finishes with more email body."	'<--Email body for you to change.  Note the break in the line for inserting the user name.
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver"	'<--Enter you email server name here
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        .Configuration.Fields.Update
        .Send
    end with 
    Set objMessage = Nothing
End Sub

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 4

Author Comment

by:seaninman
ID: 24062741
I am getting an error on Line 4
Error: Object not a collection
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24062803
Is line 2 pointing to the correct security group?
("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
For instance it might look like this&
("LDAP://CN=Test Group,OU=Test,OU=My Groups,DC=domain,DC=com")
Also, can you peek inside the group and double check that it has members and that the membership has propagated to other DC's.
0
 
LVL 4

Author Comment

by:seaninman
ID: 24062924
Okay, so I'm not getting any errors now, but its not sending an email.
0
 
LVL 4

Author Comment

by:seaninman
ID: 24062959
Nevermind... LOL  I forgot to change the mailserver.. :)  Thanks for the help..
0
 
LVL 4

Author Closing Comment

by:seaninman
ID: 31564417
Great JOB!!!
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24062981
You are welcome - glad to help out!
0
 
LVL 4

Author Comment

by:seaninman
ID: 24063057
One more thing I could use with this script..  Is there a way that we can have it write a log file (txt) that shows who an email was sent to and for what user?
0
 
LVL 14

Expert Comment

by:rejoinder
ID: 24063279
OK same as before but this time there will be logging.  See line 1 for the file name.
Like before, please test it out and let me know if you run into any problems.
strFile     = "email-manager-log.csv"
Set objFSO  = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(strFile, 2, True)
objFile.WriteLine """DATE"",""USER"",""MANAGER"",""ACTION"""
 
'Enter the LDAP path to the security group in question
Set objGroup = GetObject("LDAP://CN=Accountants,OU=Finance,DC=fabrikam,DC=com")
 
For Each strUser in objGroup.Member
    Set objUser =  GetObject("LDAP://" & strUser)
    strUserName = objUser.CN
    strManagerDN = objUser.Manager
    strUsersEmailAddress = objUser.mail
    if strManagerDN <> "" then
        Set objManager =  GetObject("LDAP://" & strManagerDN)
        strManagerEmailAddress = objManager.mail
        if strManagerEmailAddress <> "" then
            EmailManager strUserName,"your.email@domain.com",strManagerEmailAddress
            objFile.WriteLine """" & now() & """,""" & strUserName & """,""" & strManagerEmailAddress & """,""Email sent"""
        end if
    else
        objFile.WriteLine """" & now() & """,""" & strUserName & ""","" "",""Manager not assigned - no email sent"""
    end if
Next
 
Sub EmailManager(UserName,FromField,ManagerEmailAddress)
    On Error Resume Next
    Dim objMessage
    Set objMessage = CreateObject("CDO.Message")
    with objMessage
        .From = FromField				'<--From field being pulled from sub variable
        .To = ManagerEmailAddress			'<--To field being pulled from sub variable
        .Subject = "Here is the subject line to change"	'<--Subject line for you to change
        .TextBody = "Here is the email body followed by the username '" & UserName & "' and finishes with more email body."	'<--Email body for you to change.  Note the break in the line for inserting the user name.
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mailserver"	'<--Enter you email server name here
        .Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
        .Configuration.Fields.Update
        .Send
    end with 
    Set objMessage = Nothing
End Sub

Open in new window

0
 
LVL 4

Author Comment

by:seaninman
ID: 24063876
Works perfect.. THANKS
0
 
LVL 4

Author Comment

by:seaninman
ID: 24064276
Here is another one I have that you might be able to help me with...

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24293789.html
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question