Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
613 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
[X]
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
  • 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 2000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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