[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

VBSCRIPT help - Active Directory

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
jalamdar
Asked:
jalamdar
  • 3
  • 2
  • 2
  • +1
1 Solution
 
msimnCommented:
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
 
msimnCommented:
** Do not forgot to change the "ou=hr" and "dc=fabrikam,dc=com"
0
 
jalamdarAuthor Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Daz_1234Commented:
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
 
Daz_1234Commented:
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
 
jalamdarAuthor Commented:
Thank You so much Daz...exactly what i needed :)
0
 
chillbill01Commented:
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
 
Daz_1234Commented:
Thank You so much Daz...exactly what i needed :)

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

Daz
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now