VBScript - Using CACLS to Set Permission on a Directory

Greetings Experts,
I am setting permissions on a Home drive and I can't get it to work.  It doesn't error on me so I know that is is Close, but I can't figure out why this won't work.  During my testing, if I set strBackupFolder to a Const path, it works perfectly.  It clears all the default rights and sets the rights to that subfolder to: Full Control for the user and Full Control for the Domain Admin group.  No one esle has access to it. But when take aways the Const path and I insert this  function into my full script and let strBackupFolder be feed the folder that is created, it doesn't change the permissions.  All it does it add the user name with empty properties.   It doesn't clear out the existing permissions and it doesn't add the domain admin group.  I think that I am close, but I must be missing something small.

Can someone help me out
Set objNetwork = WScript.CreateObject("WScript.Network")
strUsername2 = objNetwork.UserDomain & "\" & objNetwork.UserName
 
'WScript.Echo strUsername2
a = executePermissions()
 
Function executePermissions()
	strHomeFolder = strBackupFolder
	strUser = strUsername2
	Set objShell = CreateObject("Wscript.Shell")
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	If objFSO.FolderExists(strHomeFolder) Then
		' Assign user permission to home folder.
		strCMD = "%COMSPEC% /c Echo Y| cacls " & strHomeFolder   & " /t /c /g Allied\G_CSOG:F "& strUser & ":F"
		WScript.Echo strCMD
		intRunError = objShell.Run(strCMD, 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 Function

Open in new window

aceklub97Asked:
Who is Participating?
 
sirbountyCommented:
Not really returning anything, so I changed that to a sub and passed in the strUsername2 value.
I don't know what strBackupFolder is, so presumably you're setting that else where.
I also added a reference to pull in the %comspec% variable before launching the shelled run command.

Honestly, I'm not sure run will return an error - I would probably use objShell.Exec for this and redirect both StdErr & StdOut and evaluate the success or failure there.

Give this a shot though and let me know.
Set objNetwork = WScript.CreateObject("WScript.Network")
strUsername2 = objNetwork.UserDomain & "\" & objNetwork.UserName
 
'WScript.Echo strUsername2
executePermissions(strUsername2)
wscript.quit
 
Sub  executePermissions(strUser)
        strHomeFolder = strBackupFolder
        Set objShell = CreateObject("Wscript.Shell")
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FolderExists(strHomeFolder) Then
                ' Assign user permission to home folder.
                cmd = objShell.ExpandEnvironmentStrings ("%COMSPEC%")
                strCMD = cmd & " /c Echo Y| cacls " & strHomeFolder   & " /t /c /g Allied\G_CSOG:F "& strUser & ":F"
                WScript.Echo strCMD
                intRunError = objShell.Run(strCMD, 2, True)
                        If intRunError <> 0 Then
Wscript.Echo "Error assigning permissions for user " & strUser & " to home folder " & strHomeFolder
                    End If
        End If
End Sub

Open in new window

0
 
aceklub97Author Commented:
sirbounty,
It didn't error out, but it didn't chance the permission still.  Let me put the part of the code that I put to derive the "strBackupFolder" .  I'll spare you the copy commands, but this should give you what ifnromation you need to test a value.
 The script I am including does everything is susposed to do, I just can't add permissions.   Do I need to call the execute permission somewhere else?


backupOrRestoreResponse = InputBox("Type a 0 for Backup, a 1 for Restore", "Select Operation")

If IsNumeric(strResponse) = True Then
      intResponse = CInt(strResponse)
      If intResponse >=1 And intResponse <= intNum Then
            
            strUsername = objNetwork.UserName
            strBackupFolder = strParentFolder & "\" & objTrainers(intResponse) & "\" & strUsername & "\"
      
' This is where we use the backupOrRestoreResponse Inbox we declared earlier.
'**************************************************************************************************************

If backupOrRestoreResponse = 0 Then
      executeBackup
      ElseIf backupOrRestoreResponse = 1 Then
      executeRestore
      deleteBackup
      Else
      MsgBox "You did not enter a valid number."      
End If      
  Else
     MsgBox "You did not enter a valid number."
  End If
Else
    MsgBox "You did not enter a valid number."
End If

Set fso = Nothing
Set Shell = Nothing
Set network = Nothing
Set objFso = Nothing
Set objTrainers = Nothing
Set objNetwork = Nothing
Set objShell   = Nothing

'FUNCTIONS ****************************************************************************************************
' 1. If 0 is is selected, then the Backup function will run.

Function executeBackup()
'- Create the master backup folder strBackupFolder
      On error resume next
Set folder = fso.createfolder(strBackupFolder)
 if folder is nothing then
     'This folder exists
 else
     'folder created
 end If           
          
 MsgBox "Please close all applications and windows before continuing with the process."

'Prompt the customer for user input:
 
' 1. Ask the customer what trainer they are under
' 2. Create a folder that has the format: \\10.120.28.74\Training\%Trainer%\%USERPROFILE%.
' 3. Ask the customer whether they want to backup for restore.
 
strParentFolder = "\\10.120.28.74\training\"
If Right(strParentFolder, 1) = "\" Then strParentFolder = Left(strParentFolder, Len(strParentFolder) - 1)
strMessage = "Please enter the number that corresponds to your trainer:" & VbCrLf
intNum = 0
 
	For Each objSubFolder In objFSO.GetFolder(strParentFolder).SubFolders
		intNum = intNum + 1
		strMessage = strMessage & VbCrLf & intNum & ": " & objSubFolder.Name
		objTrainers.Add intNum, objSubFolder.Name
	Next
strResponse = InputBox(strMessage, "Select Trainer")
backupOrRestoreResponse = InputBox("Type a 0 for Backup, a 1 for Restore", "Select Operation")
 
If IsNumeric(strResponse) = True Then
	intResponse = CInt(strResponse)
	If intResponse >=1 And intResponse <= intNum Then
		
		strUsername = objNetwork.UserName
		strBackupFolder = strParentFolder & "\" & objTrainers(intResponse) & "\" & strUsername & "\"
	
' This is where we use the backupOrRestoreResponse Inbox we declared earlier.
'**************************************************************************************************************
 
If backupOrRestoreResponse = 0 Then
	executeBackup
	ElseIf backupOrRestoreResponse = 1 Then
	executeRestore
	deleteBackup
	Else
	MsgBox "You did not enter a valid number."	
End If	
  Else
     MsgBox "You did not enter a valid number."
  End If
Else
    MsgBox "You did not enter a valid number."
End If
 
Set fso = Nothing
Set Shell = Nothing 
Set network = Nothing
Set objFso = Nothing
Set objTrainers = Nothing
Set objNetwork = Nothing 
Set objShell   = Nothing
 
'FUNCTIONS ****************************************************************************************************
' 1. If 0 is is selected, then the Backup function will run.
 
Function executeBackup()
'- Create the master backup folder strBackupFolder
	On error resume next
Set folder = fso.createfolder(strBackupFolder)
 if folder is nothing then
     'This folder exists
 else
     'folder created
 end If	      
    	
 MsgBox "Please close all applications and windows before continuing with the process."
 
....
 
 
fso.copyfile & fsocopyfolder commands
End Function

Open in new window

0
 
aceklub97Author Commented:
Thanks that worked.  I had to take away an extra "\" and it worked.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.