Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

VBScript help assigning permission to multiples folders

Posted on 2012-04-04
16
Medium Priority
?
1,796 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
Technology Partners: 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

610 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