?
Solved

VBScript help assigning permission to multiples folders

Posted on 2012-04-04
16
Medium Priority
?
1,786 Views
Last Modified: 2012-04-07
I can't figure out how to set permission on 2 folders in calcs

Here a sample:
' Cacls.vbs
' Example VBScript to set Administrators permissions with Cacls
' ---------------------------------------------------------'
Option Explicit
Dim strHomeFolder, strHome, strUser
Dim intRunError, objShell, objFSO
 
strHomeFolder = "c:\test"
strHomeFolder = "c:\program files\test"

 
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strHomeFolder) Then
' Assign user permission to home folder.
intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
& strHomeFolder & " /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
   If intRunError <> 0 Then
   Wscript.Echo "Error assigning permissions for user " _
   & strUser & " to home folder " & strHomeFolder
   End If
End If
 
WScript.Quit
 
' End of Cacls example VBScript

Thank you for your help.
0
Comment
Question by:dbadouraly
[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
  • 8
  • 6
  • 2
16 Comments
 
LVL 2

Expert Comment

by:sebest
ID: 37808926
Trying using the /E switch insted of /T switch for cacls.
0
 

Author Comment

by:dbadouraly
ID: 37808955
It will just replace the permission, the problem I am having is i don't the command line on how to replace permission on c:\test and c:\program files\test folder in the same script.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37808996
You can use an array to list folders and loop this section:
intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls " _
& strHomeFolder & " /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
   If intRunError <> 0 Then
   Wscript.Echo "Error assigning permissions for user " _
   & strUser & " to home folder " & strHomeFolder
   End If

Open in new window


so use something like this:
arrFolders = Array(strHomeFolder, "C:\Test", "C:\Program Files\Test Folder")

For Each strFolder In arrFolders
	intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls """ _
		& strFolder & """ /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
	If intRunError <> 0 Then
		Wscript.Echo "Error assigning permissions for user " _
			& strUser & " to home folder " & strFolder
	End If
Next

Open in new window


Regards,

Rob.
0
Independent Software Vendors: 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!

 

Author Comment

by:dbadouraly
ID: 37809040
Hi Rob,
I am novice is script I am getting an error on line 4 please see the attached files
Doc1.docx
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37809067
No problem.  Here's the full code.

Rob.

' Cacls.vbs
' Example VBScript to set Administrators permissions with Cacls
' ---------------------------------------------------------' 
Option Explicit
Dim strHomeFolder, strHome, strUser
Dim intRunError, objShell, objFSO
 
arrFolders = Array("C:\Test", "C:\Program Files\Test Folder")

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strHomeFolder) Then
' Assign user permission to the folders.
For Each strFolder In arrFolders
	intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls """ _
		& strFolder & """ /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
	If intRunError <> 0 Then
		Wscript.Echo "Error assigning permissions to folder " & strFolder
	End If
Next
 
WScript.Quit
 
' End of Cacls example VBScript

Open in new window

0
 

Author Comment

by:dbadouraly
ID: 37809136
Hi Rob,
I am sorry to bug you again, I got another error script on line 24 char 32, I tried to figured what the error was, couldn't figure out
Doc2.docx
0
 
LVL 2

Expert Comment

by:sebest
ID: 37809187
Add the

End If

prior to the Wscript.Quit statement
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37809191
oh yeah, whoops, missed that.
0
 

Author Comment

by:dbadouraly
ID: 37809261
Thank you for your prompt reply and help,
I am still getting an error message on line 8 char 1
error Variable is undefined: "arrFolders"
Doc3.docx
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37809272
Sorry, forgot you had Option Explicit in there.

This should work now.

Rob.

' Cacls.vbs
' Example VBScript to set Administrators permissions with Cacls
' ---------------------------------------------------------' 
Option Explicit
Dim strHome, strUser, arrFolders, strFolder
Dim intRunError, objShell, objFSO
 
arrFolders = Array("C:\Test", "C:\Program Files\Test Folder")

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FolderExists(strHomeFolder) Then
' Assign user permission to the folders.
For Each strFolder In arrFolders
	intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls """ _
		& strFolder & """ /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
	If intRunError <> 0 Then
		Wscript.Echo "Error assigning permissions to folder " & strFolder
	End If
Next
End If

WScript.Quit
 
' End of Cacls example VBScript

Open in new window

0
 

Author Comment

by:dbadouraly
ID: 37809324
Hi Rob,
this must be the last error we should getting on the script, this time is in line 12 char 1 error: Variable is undefined: "strHomeFolder"

I am sorry, I hope  I am not taking to much of your time.
Doc4.docx
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 37809331
Oh, I'm being slow today.  Sorry.  I think this should do it.

Regards,

Rob.

' Cacls.vbs
' Example VBScript to set Administrators permissions with Cacls
' ---------------------------------------------------------' 
Option Explicit
Dim strHome, strUser, arrFolders, strFolder
Dim intRunError, objShell, objFSO
 
arrFolders = Array("C:\Test", "C:\Program Files\Test Folder")

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
' Assign user permission to the folders.
For Each strFolder In arrFolders
	If objFSO.FolderExists(strFolder) Then
		intRunError = objShell.Run("%COMSPEC% /c Echo Y| cacls """ _
			& strFolder & """ /t /c /g ""Administrators:F"" ""Users:F"" ""Authenticated Users:F"" Everyone:F ", 2, True)
		If intRunError <> 0 Then
			Wscript.Echo "Error assigning permissions to folder " & strFolder
		End If
	End If
Next

WScript.Quit
 
' End of Cacls example VBScript

Open in new window

0
 

Author Closing Comment

by:dbadouraly
ID: 37809361
Thank you so much for helping me on this.

Regards,
0
 

Author Comment

by:dbadouraly
ID: 37812380
Hi Rob,
I am trying this script at work, I don't get any error messages but the permission doesn't apply to the workstation any idea?
0
 

Author Comment

by:dbadouraly
ID: 37812607
Hi rob,
please disregard my previous message, I forgot to update the folder name.

Thank you again for all you did for me.

Sincerely,
Dinesh
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 37818803
No problem. Glad it worked.

Rob.
0

Featured Post

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
This article shows how to use a free utility called 'Parkdale' to easily test the performance and benchmark any Hard Drive(s) installed in your computer. We also look at RAM Disks and their speed comparisons.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Suggested Courses

765 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