Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

VBScript - Using CACLS to Set Permission on a Directory

Posted on 2009-04-16
3
Medium Priority
?
1,595 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:aceklub97
  • 2
3 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 2000 total points
ID: 24161140
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
 

Author Comment

by:aceklub97
ID: 24163611
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
 

Author Closing Comment

by:aceklub97
ID: 31571133
Thanks that worked.  I had to take away an extra "\" and it worked.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

571 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