Solved

VBScript help assigning permission to multiples folders

Posted on 2012-04-04
16
1,776 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article helps those who get the 0xc004d307 error when trying to rearm (reset the license) Office 2013 in a Virtual Desktop Infrastructure (VDI) and/or those trying to prep the master image for Microsoft Key Management (KMS) activation. (i.e.- C…
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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