Objective
Recently because of work needs, need to a batch server directory of files under the unified authorization, for Linux, authorization is very convenient, for window, to the directory files under the volume authorization is very inconvenient, Windows platform licensing naturally thought of using icacls or cacls tools to accomplish tasks and be flexible.
Demand
- Perform authorization work with a VBS script for remote invocation of bulk execution
- can specify authorization directory
- The ability to specify the authorization method, that is, only the directory authorization or the directory file Unified authorization
- If you authorize the file, you need to traverse the specified directory recursively, and the subdirectory file will authorize
- Specify authorized Users
- Specify permissions granted to users (for example: rx,w,f)
Realize
Based on the above requirements, write the following VBS script
If WScript.Arguments.Count = 0 Then wscript.echo "Usage:do_auth.vbs [-d|-f] [FilePath] [Authuser]" Wscript.Quit (1) E nd ifSet WshShell = CreateObject ("Wscript.Shell") Set objFSO = CreateObject ("Scripting.FileSystemObject") objAuthOption =wscript.arguments (0) Objstartfolder = wscript.arguments (1) objauthuser = wscript.arguments (2) Set ObjFolder = Objfso.getfolder (Objstartfolder) showsubfolders objfso.getfolder (objstartfolder) Sub showsubfolders (Folder) ' WScript.Echo Subfolder.pathif objauthoption = "-F" then Set Curfiles = Objfso.getfolder (Folder.path). Files for each curfile in curfiles intrunerror = Wshshell.run ("icacls" & Curfile.path & "/grant" & Chr & Objauthuser & Chr ("the") & ": F", 2, True) If intrunerror <> 0 Then W Script. Echo "Error Assigning permissions for user" & Objauthuser & "to File" & Curfile.name Else WScript.Echo "Success:file" & Curfile.name & "Assigned user "& Objauthuser End If Next for each subfolder in folder.subfolders Set objfolder = objfso.g Etfolder (subfolder.path) Set colfiles = Objfolder.files for each objfile in Colfiles Intruner Ror = Wshshell.run ("icacls" & Objfile.path & "/grant" & Chr ("the") & Objauthuser & Chr ("the") & " : F ", 2, True) If intrunerror <> 0 Then WScript.Echo" Error Assigning permissions for user "& Objauthuser &" to File "& Objfile.name Else WScript.Echo" Success:fi Le "& objfile.name &" Assigned user "& Objauthuser End If Next showsubfolders SUBF Older Nextelseif objauthoption = "-D" then for each subfolder in folder.subfolders Intrunerror = Wshshell.run ("IC ACLS "& Subfolder.path &"/grant "& Chr (" "") & Objauthuser & Chr ("the") & ": F", 2, True) If Intrunerror <> 0Then WScript.Echo ' Error Assigning permissions for user ' & Objauthuser & ' to Folder ' & Subfolder.pat H Else WScript.Echo "Success:folder" & Subfolder.path & "Assigned user" & Objauthuse R End If showsubfolders Subfoldernextelsewscript.echo "Parameter Error" Wscript.Quit (1) End IfEnd Sub
Summarize
The above code implements the requirements listed in the file, hoping to help everyone
Application of Icacls in Windows directory file authorization