Link to home
Start Free TrialLog in
Avatar of BLACK THANOS
BLACK THANOSFlag for United States of America

asked on

USING CACLS IN A VBS SCRIPT

Hi out there in programming land! Are there any scripting gurus that can solve the following delimma. The following code snippet is supposed to use the CACLS executable to set permissions on a particular folder. Until I can figure out how to do this using WMI, this is my only option. As you will see, the script works great if you are trying to set permissions  with a user or group that has no spaces in it, but the minute a user or group has a space in it, the script falls apart. The details are below.  by the way this is worth 500 points to me.
*****************************************************************************
Set objFSO = CreateObject("Scripting.FileSystemObject")
 Set objNetwork = CreateObject("WScript.Network")
 Set objShell = CreateObject("WScript.Shell")
'Set objShell = CreateObject("WScript.Shell")

      
      
      
'Disregard these three steps
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)'
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)


      
Share = "FOLDER TEST"

GrpOrAcct = "IT FOLDER TEST FULL"  
GrpOrAcct1 = "IT FOLDER TEST READ"
GrpOrAcct2 ="RBAACCOUNT"



If objFSO.FolderExists("Z:") Then
    Set objFolder = objFSO.GetFolder("Z:")
    objNetwork.RemoveNetworkDrive "Z:",True,True
End If  

SelectValue =  "\\dfssvr\DFSROOT\I T\SHARED\" & Share

 objNetwork.MapNetworkDrive "Z:", "\\dfssvr\DFSROOT\I T\SHARED\" & Share  , False






Set objExecObject = objShell.Exec("%comspec% /c CACLS.EXE " & Chr(34)& "Z:"_
& Chr(34)& " /E /G " & Chr(34)&  GrpOrAcct1 & Chr(34)&_
":R > C:\TXT\RESULT.TXT")


' Disregard last three steps
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)
'Result = objShell.Popup("Processing......",1, "Please Wait...", 0)

End Sub
            
Avatar of sirbounty
sirbounty
Flag of United States of America image

This should work, enclosing it in quotes - but it looks like you missed some spaces...and you're not pointing to an actual 'directory'
try this:


Set objExecObject = objShell.Exec("%comspec% /c CACLS.EXE " & Chr(34) & "Z:\ "_
& Chr(34) & " /E /G " & Chr(34) &  GrpOrAcct1 & Chr(34) & ":R > C:\TXT\RESULT.TXT")
Avatar of BLACK THANOS

ASKER

Respectfully,

I am indeed  pointing to a valid 'directory'. If you will look at the following snippet:

***************************************************************
If objFSO.FolderExists("Z:") Then
    Set objFolder = objFSO.GetFolder("Z:")
    objNetwork.RemoveNetworkDrive "Z:",True,True
End If  
SelectValue =  "\\dfssvr\DFSROOT\I T\SHARED\" & Share
 objNetwork.MapNetworkDrive "Z:", "\\dfssvr\DFSROOT\I T\SHARED\" & Share  , False
****************************************************************


Your code: Doesnt work either. I tried it with and without the spaces and \ you recommended.
******************************************************************
Set objExecObject = objShell.Exec("%comspec% /c CACLS.EXE " & Chr(34) & "Z:\ "_
& Chr(34) & " /E /G " & Chr(34) &  GrpOrAcct1 & Chr(34) & ":R > C:\TXT\RESULT.TXT")
*******************************************************************
is identical to mine other than the z:\, which is unecessary. z: has sufficed. Keep in mind that the script I submitted works
perfectly for accounts or groups that have no spaces.

Any other suggestions????




Well, noramlly when you reference just a drive letter (in this case Z:) it resolves to the present working folder on that drive....so, if it is or has worked, I'd recommend you placing that trailing backslash, just to be sure...

Other than that, I was having trouble using comspec - not sure if that's part of the problem for you or not?  I know you said it works with groups without spaces....

Lastly, I used the Shell.Run method, rather than the Exec...give it a try and let me know:

Dim objShell: Set objShell = CreateObject("Wscript.shell")
GrpOrAcct1 = "Test Users"
objShell.Run "cmd /k CACLS.EXE C:\testing /E /G " & Chr(34) & GrpOrAcct1 & Chr(34) & ":R"

*I also changed the /c to a /k so that the window is left open afterwards, so you can see the results right away...

Just to complete your statement, the drive letter Z is indeed referenced, which is then subsequently map rooted, therebye making the backlash unecessary. However, this is a mute point , as I have tested both options with no luck. Additionally, my first choice was actually to use the shell run method, that to0 did not handle users or groups with spaces. I re-iterate, My script works great with no spaces involved, however, I will concede that this is in fact a wscript.shell issue. I failed to mention that I get perfect results when I go to the dos prompt to perform the same actions. The only caviat is that I must use appropriate quotes on either side of the user or group with spaces, ergo the need for chr(34) on the script, which emulates the quotes around the user or group with spaces.

Note: changing the /c /k didnt yield any results that I could see. Am I missing something?
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
THANKS sirbounty,

Excellent job
Happy to have helped. :^ )