Solved

VBScript help assigning permission to multiples folders

Posted on 2012-04-04
16
1,700 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
 

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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Storage devices are generally used to save the data or sometime transfer the data from one computer system to another system. However, sometimes user accidentally erased their important data from the Storage devices. Users have to know how data reco…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
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.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now