Solved

VBSCRIPT help - Active Directory

Posted on 2010-11-23
8
434 Views
Last Modified: 2012-08-14
I wrote a VBscript that will create security groups for me in AD.
If the group exists, i get a VB error 'the object exists'.
Is there a way to check if that group already exists, display a 'friendlier' message then quit the script before trying to create it.
Thanks
Set objGroup1 = objOU1.Create("Group", "cn=wachs-sw " & site & " "  & strLine & " (M)")
	objGroup1.Put "sAMAccountName", "wachs-sw " & site & " " &  strLine & " (M)"
	objGroup1.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
	ADS_GROUP_TYPE_SECURITY_ENABLED
	objGroup1.Put "Description", "SC: " & ServiceCall
	objGroup1.SetInfo
	

Open in new window

0
Comment
Question by:jalamdar
[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
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 3

Expert Comment

by:msimn
ID: 34202840
Try adding the following lines before your create scripts.

Set objOU = GetObject("LDAP://ou=hr, dc=fabrikam,dc=com")
If objOU is Nothing Then
     MsgBox("This group is already there!")
     WScript.Quit
End If

Set objGroup1 = objOU1.Create("Group", "cn=wachs-sw " & site & " "  & strLine & " (M)")
	objGroup1.Put "sAMAccountName", "wachs-sw " & site & " " &  strLine & " (M)"
	objGroup1.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
	ADS_GROUP_TYPE_SECURITY_ENABLED
	objGroup1.Put "Description", "SC: " & ServiceCall
	objGroup1.SetInfo

Open in new window

0
 
LVL 3

Expert Comment

by:msimn
ID: 34202842
** Do not forgot to change the "ou=hr" and "dc=fabrikam,dc=com"
0
 

Author Comment

by:jalamdar
ID: 34203252
how is this checking if the GROUP exits!
If i read that properly, you are checking if the OU is not valid...but the OU is there, it is actually where the group is to be created.
What i need to find out inside that OU if the group i am trying to create already exists i.e. was created earlier.
0
Industry Leaders: 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 13

Expert Comment

by:Daz_1234
ID: 34204124
Hi,

The following should do the check you want.

Hope this helps,
Daz.


Set objGroup1 = objOU1.Create("Group", "cn=wachs-sw " & site & " "  & strLine & " (M)")
    objGroup1.Put "sAMAccountName", "wachs-sw " & site & " " &  strLine & " (M)"
    objGroup1.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objGroup1.Put "Description", "SC: " & ServiceCall
    On Error Resume Next
    objGroup1.SetInfo
    If Err.Number = &h80071392 Then
        MsgBox "Group " & sName & " already exists", vbExclamation + vbSystemModal, "ERROR"
    ElseIf Err.Number <> 0 Then
        MsgBox "Error creating group " & sName & " - Error: " & Err.Number & " - " & Err.Description, vbExclamation + vbSystemModal, "ERROR"
    End If
    On Error Goto 0

Open in new window

0
 
LVL 13

Expert Comment

by:Daz_1234
ID: 34204314
Sorry: because I copied / pasted from a working script of mine, I forgot to change the lines.

2nd try below.

Daz.
strGroup = "wachs-sw " & site & " "  & strLine & " (M)"
    Set objGroup1 = objOU1.Create("Group", "cn=" & strGroup)
    objGroup1.Put "sAMAccountName", strGroup
    objGroup1.Put "groupType", ADS_GROUP_TYPE_GLOBAL_GROUP Or _
    ADS_GROUP_TYPE_SECURITY_ENABLED
    objGroup1.Put "Description", "SC: " & ServiceCall
    On Error Resume Next
    objGroup1.SetInfo
    If Err.Number = &h80071392 Then
        MsgBox "Group " & strGroup & " already exists", vbExclamation + vbSystemModal, "ERROR"
    ElseIf Err.Number <> 0 Then
        MsgBox "Error creating group " & strGroup & " - Error: " & Err.Number & " - " & Err.Description, vbExclamation + vbSystemModal, "ERROR"
    End If
    On Error Goto 0

Open in new window

0
 

Author Comment

by:jalamdar
ID: 34210056
Thank You so much Daz...exactly what i needed :)
0
 
LVL 3

Accepted Solution

by:
chillbill01 earned 500 total points
ID: 34218637
Just add if you didn't want to simply check the error but query add to see if it existed and place this as a function you could use:

wscript.echo FindGroup("Domain Users")


Function FindGroup(strGroupname)
   
    dim objrootdse, strdnsdomain, adoconnection, adocommand, strquery
      dim adorecordset, strmember, strbase, strfilter, strattributes
      dim arrProxy, objUser, strMailAddress

      ' determine dns domain name from rootdse object.
      set objrootdse = getobject("LDAP://RootDSE")
      strdnsdomain = objrootdse.get("defaultnamingcontext")
      
      ' use ado to search active directory for all computers.
      set adocommand = createobject("adodb.command")
      set adoconnection = createobject("adodb.connection")
      
      adoconnection.provider = "adsdsoobject"
      adoconnection.open "active directory provider"
      adocommand.activeconnection = adoconnection
      
      ' search entire domain.
      strbase = "<LDAP://" & strdnsdomain & ">"
      
      strfilter = "(&(objectclass=group)(name=" & strGroupname & "))"
      
      ' comma delimited list of attribute values to retrieve.
      strattributes = "displayname,distinguishedName"
      
      ' construct the ldap syntax query.
      strquery = strbase & ";" & strfilter & ";" & strattributes & ";subtree"
   
    adocommand.commandtext = strquery
      adocommand.properties("page size") = 1000
      adocommand.properties("timeout") = 30
      adocommand.properties("cache results") = false
      
      set adorecordset = adocommand.execute

      if adorecordset.recordcount <= 0 then
        FindGroup = "Group does not exist"
    else
        FindGroup = "Group does exist"
    end if

end function
0
 
LVL 13

Expert Comment

by:Daz_1234
ID: 34237590
Thank You so much Daz...exactly what i needed :)

... I guess not so much exactly what you needed ;o)

Daz
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

733 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