Application of Icacls in Windows directory file authorization

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.