Solved

VBScript help assigning permission to multiples folders

Posted on 2012-04-04
16
1,727 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
  • 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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 500 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
An article on effective troubleshooting
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

770 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