Get username from all users in specific group - vbscript

I must be missing something here, or I'm blind or I've had too much coffee.
Basically, I'm trying to get the username of each user in a specific group i AD.
Then I want to take these username and pass those to a powershell script. But that's another case. This is probably a quick win for you guys.
Dim groupName
groupName = "LDAP://CN=groupname,OU=MailGroups,OU=Exchange,OU=MainContainer,DC=MyDomain,DC=com"
Set objGroup = GetObject(groupName)
For Each strUser in objGroup.Member
Set objUser = GetObject("LDAP://" & strUser.UserName)
Next
And I get this error message Object required: 'strUser'
Why is this happening? strUser is right there!
If I change the snippet a bit to this:
Set objGroup = GetObject(groupName)
For Each strUser in objGroup.Member
Set objUser = GetObject("LDAP://" & strUser)
msgbox objUser.Name
Next
Then I get the result CN=Doe John in the message box for each member.

try
For Each strUser in objGroup.Members
not Member but Member*s*

This site will tell you more than you ever want to know about AD and VBS:
http://www.computerperformance.co.uk/vbscript/vbscript_group_enumerate_members.htm

Related

Getting network details from Active Directory in VB script?

There are needs when we want to populate user details from Active Directory. Sharing my code to access user details from AD.
Introduction
People often ask for querying active directory by passing network account name or email. There are many articles already available on this, and one might get confused. Here is simple working code to access user detail from active directory database-
Please note in order to access AD, you have to specify valid network account credentials in connection.
function GetADDetails(userId)
Set objConn = Server.CreateObject("ADODB.Connection")
objConn.provider ="ADsDSOObject"
objConn.Properties("User ID") = "domain\userId" 'specify domain and
network account
objConn.Properties("Password") = "password" 'specify network password
objConn.Properties("Encrypt Password") = True
objConn.open "Active Directory Provider"
Set objCom = CreateObject("ADODB.Command")
Set objCom.ActiveConnection = objConn
strTarget="GC://abc.com" 'your domain name
objCom.CommandText ="select sn, givenName, sAMAccountName, name,mail,
telephoneNumber FROM '"+strTarget+"' where sAMAccountname='"+userId+"'"
Set objRS = objCom.Execute
If Not (objRS.EOF Or objRS.BOF) Then
GetADDetails=objRS.GetRows
Else
GetUserData = Null
End If
'Close objects and remove from memory
objRS.Close
objConn.Close
Set objRS = Nothing
Set objConn = Nothing
Set objCom = Nothing
end function

WScript.Arguments issue

I have a VBScript:
Set objUser = GetObject("WScript.Arguments.Item(0)")
objUser.TerminalServicesProfilePath = WScript.Arguments.Item(1)
objUser.AllowLogon = Enabled
objUser.SetInfo
This is called by:
wscript LDAPTSChanger.vbs LDAP://cn=Scott.Test,ou=Administration,dc=JPddRESS,dc=CO,dc=UK SCOMANTEST
But I can't get it to accept the WScript.Arguments.Item(0) as the search base (this will change per what's outputted elsewhere).
There is a space after the ldaptschanger.vbs to say its a parameter.
Remove the quotes:
Set objUser = GetObject(WScript.Arguments.Item(0))

VBScript: Search remote registry to return SID and mapped share drives

I'm looking for some help creating script that would search for username in
ProfileImagePath string in Remote_Machine\HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-21*
and would get parent directory-the SID S-1-5-21*, store it in memory and later use it to output keys under
Remote_machine\HKEY_USERS*SID*\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2.
I was only able to find script:
Set objNetwork = CreateObject("Wscript.Network")
strComputerName = InputBox("Please input PC name:","")'objNetwork.ComputerName
strLogonUser = objNetwork.UserName
Domaccount = trim(cstr(InputBox("Please input Domain User Account:","Type User_
Account")))
strDomain = "Dom"
Set objWMIService = GetObject("winmgmts:\\" & strComputerName & "\root\cimv2")
Set objAccount = objWMIService.Get ("Win32_UserAccount.Name='" & Domaccount &_
"',Domain='" & strDomain & "'")
Wscript.Echo objAccount.SID
but that one does not work all the time - I get SWbemServicesEx: Not found
and I think it uses different method than search. Moreover it does only first part of my request.
Can anyone help a brother out? :)
I am not sure to have understood your exact problem, anyway this hotfix released by Microsoft has been useful to me in a similar situation:
http://support.microsoft.com/kb/2465990

Secure LDAP object manipulation with VBscript using alternate credentials

I'm aware of using ADsDSOobject with explicit credentials to connect to an AD object to read attributes, list members, etc. And the GetObject("LDAP//...") method for manipulating those objects (adding group members, changing properties, etc.), but is there a way to manipulate attributes and memberships with explicit credentials?
The first method I'm referring to is something like...
Set conn = Server.CreateObject("ADODB.Connection")
Set cmd = Server.CreateObject("ADODB.Command")
conn.Provider = "ADsDSOobject"
conn.Properties("User ID") = AD_Username
conn.Properties("Password") = AD_Password
conn.Properties("Encrypt Password") = True
conn.Open "Active Directory Provider"
Set cmd.ActiveConnection = conn
But none of the script examples that perform tasks like adding a user to a domain group can use this approach as far as I know. Is there a way to do that somehow?
In VBScript, very often, you are using ADSI to add user to group. Here is a sample code to add a user to a domain group
Set objUser = GetObject("LDAP://CN=jeffsmith,DC=fabrikam,DC=com")
Set objGroup = GetObject("LDAP://CN=group1,DC=fabrikam,DC=com")
objGroup.add(objUser.ADsPath)
It works fine but it's always using your current user credentails. It's because GetObject doesn't allow you to specify alternate credentials.
To specify another credentails, you need to replace GetObject by OpenDSObject
Const ADS_SECURE_AUTHENTICATION = 1
Set openDS = GetObject("LDAP:")
Set objUser = openDS.OpenDSObject("LDAP://CN=jeffsmith,DC=fabrikam,DC=com",
"username",
"password",
ADS_SECURE_AUTHENTICATION)
Set objGroup = openDS.OpenDSObject("LDAP://CN=group1,DC=fabrikam,DC=com",
"username",
"password",
ADS_SECURE_AUTHENTICATION)
objGroup.add(objUser.ADsPath)

Sending email from webpage using Outlook

I have a webpage that has a button that sends a letter on the page to an email recipent. Currently we are use Lotus Notes and with VB script, we are able to create an object of Lotus Notes and one of the properties for this object is PutInFolder. After the user clicks on the email button, the script will send the email and also put save the email in a certain folder on the user's computer. Our company is now switching over to Outlook 2007 and I'm looking to do the same thing with an Outlook object instead. Our development is local intranet only, and there are only a few users that will have access to this. Anyway, my problem is I cannot seem to find the same functionality with an Outlook Application.
I do have the send of the email currently working using this logic. Does anyone have any ideas on how to save the email in the user's outlook folder? I tried looking for a list of properties that I can call but I cannot find anything searching. Maybe I don't have the right terminalogy in the searches.
Thank you.
sub send_mailvb(sendto, sendcc, sendbcc, subject_text, body_text, attachment1, attachment2, attachment3)
'Open mail, adress, attach report
dim objOutlk 'Outlook
dim objMail 'Email item
dim strMsg
const olMailItem = 0
'Create a new message
set objOutlk = createobject("Outlook.Application")
set objMail = objOutlk.createitem(olMailItem)
' Setup send to
objMail.To = sendto
' Setup send cc
If sendcc <> "" Then
objMail.cc = sendcc
End If
' Setup send bcc
If sendbcc <> "" Then
objMail.bcc = sendbcc
End If
'Set up Subject Line
objMail.subject = subject_text
'Add the body
strMsg = body_text & vbcrlf
'Add an attachments
If attachment1 <> "" Then
objMail.attachments.add(attachment1)
End If
If attachment2 <> "" Then
objMail.attachments.add(attachment2)
End If
If attachment3 <> "" Then
objMail.attachments.add(attachment3)
End If
objMail.body = strMsg
objMail.display 'Use this to display before sending, otherwise call objMail.Send to send without reviewing
'Clean up
set objMail = nothing
set objOutlk = nothing
End Sub
For future reference... I found the solution I was looking for. It wasn't too bad of a mess. Here's the modified source to replicate the Send and save email to a specific folder incase someone else comes looking. Thanks to Tester101 for the website I was looking for. Again this is vbscript imbedded in the HTML page.
sub send_mailvb(sendto, sendcc, sendbcc, subject_text, body_text, attachment1, attachment2, attachment3)
'Open mail, adress, attach report
dim objOutlk 'Outlook
dim objMail 'Email item
dim strMsg
dim myInbox
const olMailItem = 0
'Create a new message
set objOutlk = createobject("Outlook.Application")
Set objNameSpace = objOutlk.Session
set objMail = objOutlk.createitem(olMailItem)
Set myNameSpace = objOutlk.GetNamespace("MAPI")
Set myExplorer = objOutlk.ActiveExplorer
' 6 at least on my machine pointed to the Inbox (should be the same as constant olFolderInbox). Within the Inbox I have a folder called Test
Set myExplorer.CurrentFolder = myNameSpace.GetDefaultFolder(6).Folders("Test")
Set myFolder = myExplorer.CurrentFolder
' Setup send to
objMail.To = sendto
' Setup send cc
If sendcc "" Then
objMail.cc = sendcc
End If
' Setup send bcc
If sendbcc "" Then
objMail.bcc = sendbcc
End If
'Set up Subject Line
objMail.subject = subject_text
'Add the body
strMsg = body_text & vbcrlf
'Add an attachments
If attachment1 "" Then
objMail.attachments.add(attachment1)
End If
If attachment2 "" Then
objMail.attachments.add(attachment2)
End If
If attachment3 "" Then
objMail.attachments.add(attachment3)
End If
objMail.body = strMsg
// objMail.display 'Use this to display before sending, otherwise call objMail.Send to send without reviewing
objMail.Save
objMail.Move(myFolder)
objMail.Send
'Clean up
set objMail = nothing
set objOutlk = nothing
End Sub
I found this article. It might be something tha could help.
http://www.outlookcode.com/codedetail_print.aspx?id=1041
If not this site has great resources for working with outlook.
It looks like the MailItem object has a Save method, as well as a SaveAs method. So you should be able to do something like this.
objMail.SaveAs "C:\Test.msg", 3
The 3 is to save the message in olMSG format see OlSaveAsType Enumeration.
I have a solution. We've decided to bcc the person sending the email and then use an outlook rule to move the email to the specified outlook folder. Thanks to everyone that replied.

Resources