BLACK THANOS
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.Fi leSystemOb ject")
Set objNetwork = CreateObject("WScript.Netw ork")
Set objShell = CreateObject("WScript.Shel l")
'Set objShell = CreateObject("WScript.Shel l")
'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.RemoveNetworkDr ive "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
**************************
Set objFSO = CreateObject("Scripting.Fi
Set objNetwork = CreateObject("WScript.Netw
Set objShell = CreateObject("WScript.Shel
'Set objShell = CreateObject("WScript.Shel
'Disregard these three steps
'Result = objShell.Popup("Processing
'Result = objShell.Popup("Processing
'Result = objShell.Popup("Processing
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.RemoveNetworkDr
End If
SelectValue = "\\dfssvr\DFSROOT\I T\SHARED\" & Share
objNetwork.MapNetworkDrive
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
'Result = objShell.Popup("Processing
'Result = objShell.Popup("Processing
End Sub
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.RemoveNetworkDr ive "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????
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.RemoveNetworkDr
End If
SelectValue = "\\dfssvr\DFSROOT\I T\SHARED\" & Share
objNetwork.MapNetworkDrive
**************************
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.shel l")
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...
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.shel
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...
ASKER
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?
Note: changing the /c /k didnt yield any results that I could see. Am I missing something?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
THANKS sirbounty,
Excellent job
Excellent job
Happy to have helped. :^ )
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")