troubleshooting Question

How to map a drive & create folder  based on AD Group

Avatar of dudure
dudure asked on
Visual Basic ClassicVB Script
4 Comments1 Solution343 ViewsLast Modified:
Hi ,
i have a script that create and map folder to user i want to repleace the for each sentence to if sentence
Like - i want to repleace for each  user is in group do this To if a user is in group do that .
For Each oUser In objGroupDP.Members to IF  oUser In objGroupDP.Members Then .......
the script :
---------------------

On Error Resume Next

Set objGroupDP = GetObject("LDAP://CN=DPCtxBakara,OU=çÑÕæÕê áÙØèÙçá,OU=Groups,OU=MahozDanPT,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupDR = GetObject("LDAP://CN=DRCtxBakara,OU=Citrix,OU=Groups,OU=MahozDarom,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupHF = GetObject("LDAP://CN=HFCtxBakara,OU=Citrix,OU=Groups,OU=MahozHaifa,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupJR = GetObject("LDAP://CN=JRCtxBakara,OU=Citrix,OU=Groups,OU=MahozJerusalem,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupMR = GetObject("LDAP://CN=MRCtxBakara,OU=Citrix,OU=Groups,OU=MahozMerkaz,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupSH = GetObject("LDAP://CN=SHCtxBakara,OU=Citrix,OU=Groups,OU=MahozShosh,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupTA = GetObject("LDAP://CN=TACtxBakara,OU=Citrix,OU=Groups,OU=MahozTayf,OU=Mehozot,DC=Domain,DC=Com")
Set objGroupZA = GetObject("LDAP://CN=ZACtxBakara,OU=Citrix,OU=Groups,OU=MahozZafon,OU=Mehozot,DC=Domain,DC=Com")

For Each oUser In objGroupDP.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\danpt\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\danpt\home\" & sUserName
            WScript.Quit
      
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\danpt\home\" & sUserName
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupDR.Members

If oUser.Class = "user" Then

Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\darom\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\darom\home\" & sUserName
            WScript.Quit

      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\darom\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next


For Each oUser In objGroupHF.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\haifa\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then
            
            objnet.mapnetworkdrive "w:","\\server\ShareName\haifa\home\" & sUserName
            WScript.Quit
 
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\haifa\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing
End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupJR.Members

If oUser.Class = "user" Then

Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\jerusalem\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\jerusalem\home\" & sUserName
            WScript.Quit
 
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\jerusalem\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupMR.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\merkaz\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\Merkaz\home\" & sUserName
            WScript.Quit
 
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\Merkaz\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupSH.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\shosh\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\shosh\home\" & sUserName
            WScript.Quit

      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\shosh\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupTA.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\tayf\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\tayf\home\" & sUserName
            WScript.Quit
 
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\tayf\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

For Each oUser In objGroupZA.Members

If oUser.Class = "user" Then
      
Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")

sUserName = objNet.UserName
sDomainName= objNet.UserDomain

  If LCase(oUser.samAccountName) = LCase(sUserName) Then

      folderToCheckFor = "\\server\ShareName\zafon\home\" & sUserName

      If (FSO.FolderExists(folderToCheckFor)) = True Then

            objnet.mapnetworkdrive "w:","\\server\ShareName\zafon\home\" & sUserName
            WScript.Quit
 
      ElseIf (FSO.FolderExists(folderToCheckFor)) = False Then
                FSO.CreateFolder(folderToCheckFor)
                Set WshShell = WScript.CreateObject("WScript.Shell")
                Call WshShell.Run("CACLS " & folderToCheckFor & " /T /E /C /G " & sDomainName & "\" & sUserName & ":F",2,True)
            WScript.Sleep 2000
            objnet.mapnetworkdrive "w:","\\server\ShareName\zafon\home\" & sUserName  
                Set WshShell = Nothing
            WScript.Quit      
      End If
      
  End If
Set FSO = Nothing
Set objNet = Nothing

End If
oUser.SetInfo
Set oUser = Nothing
Next

WScript.Quit
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 4 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 4 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros