We help IT Professionals succeed at work.

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

dudure
dudure asked
on
335 Views
Last Modified: 2013-11-25
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
Comment
Watch Question

I might have screwed up the nesting of some of the operations you're doing for each group, but you get the idea: put the group names in an external file and read them one by one, so you're only getting one group object at a time.

On the other hand, if you want to apply this change to all groups, you can skip making a list and use a recursive function to crawl through all the groups.

Set objNet = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("scripting.filesystemobject")
Set objGroupNamesFile = FSO.OpenTextFile("yourfile.txt", 1)
 
sUserName = objNet.UserName
sDomainName= objNet.UserDomain
 
Do While Not objGroupNamesFile.AtEndOfStream
	Set objGroup = GetObject("LDAP://" & objGroupNamesFile.ReadLine)
 
		For Each oUser In objGroup.Members
	
		If oUser.Class = "user" Then
	      	
			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
	      
		End If
		oUser.SetInfo
		Set oUser = Nothing
		Set objGroup = Nothing
	Next
Loop

Open in new window

Author

Commented:
Hi , i meen i want to convert the "for each" to " if then " like if you are member in GROUP create a folder with full premession and map it . i have other script that only map i want to add the option to create a folder for user if he dont have one and if he have only map it .
like this script :


Option Explicit
Dim objNetwork, objUser, CurrentUser  
Dim strGroup


Const DPCtxBakara_Group = "cn=DPCtxBakara"
Const DRCtxBakara_Group = "cn=DRCtxBakara"
Const HFCtxBakara_Group = "cn=HFCtxBakara"
Const JRCtxBakara_Group = "cn=JRCtxBakara"
Const MRCtxBakara_Group = "cn=MRCtxBakara"
Const SHCtxBakara_Group = "cn=SHCtxBakara"
Const TACtxBakara_Group = "cn=TACtxBakara"
Const ZACtxBakara_Group = "cn=ZACtxBakara"


Set objNetworkwork = CreateObject("WScript.Network")
Set objUser = CreateObject("ADSystemInfo")
Set CurrentUser = GetObject("LDAP://" & objUser.UserName)
strGroup = LCase(Join(CurrentUser.MemberOf))

If InStr(strGroup, lcase(DpCtxBakara_Group)) Then
WScript.Echo "DPCtxBakara "
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\danpt\home\" _
& objNetwork.UserName


ElseIf InStr(strGroup, lcase(DRCtxBakara_Group)) Then
WScript.Echo "DPCtxBakara "
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\darom\home\" _
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(HFCtxBakara_Group)) Then
WScript.Echo " HFCtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\haifa\home\"_
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(JRCtxBakara_Group)) Then
WScript.Echo " JRCtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\jerusalem\home\"_
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(MRCtxBakara_Group)) Then
WScript.Echo " MRCtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\merkaz\home\"_
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(SHCtxBakara_Group)) Then
WScript.Echo " SHCtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\shosh\home\"_
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(TACtxBakara_Group)) Then
WScript.Echo " TACtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\tayf\home\"_
& objNetwork.UserName

ElseIf InStr(strGroup, lcase(ZACtxBakara_Group)) Then
WScript.Echo " ZACtxBakara " 
objNetwork.MapNetworkDrive "w:", "\\Servername\dfs$\citrix\zafon\home\"_
& objNetwork.UserName


End If
Wscript.Echo "Finished mapping Groups - check drives "
WScript.Quit





Top Expert 2009
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Top Expert 2009

Commented:
Sorry I missed changing the Dim lines I missed seeing Option Explicit

Dim objNetwork, objUser, CurrentUser, objFSO, WshShell
Dim strGroup, UserFolder

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.