网站首页  汉语字词  英语词汇  考试资料  写作素材  旧版资料

请输入您要查询的考试资料:

 

标题 vbs通过wmi修改文件文件夹的ntfs权限
内容
    使用wmi修改文件文件夹的ntfs权限实现方法,需要的朋友可以参考下
    使用wmi修改文件文件夹的ntfs权限, 代码:
    代码如下:
    struser = guests
    strpath = d:\\abc.txt
    retval = addpermission(struser,strpath,r,true)
    '-------------------------------------------------------------------------
    '用于给文件和文件夹添加一条权限设置.返回值: 0-成功,1-账户不存在,2-路径不存在
    'struser表示用户名或组名
    'strpath表示文件夹路径或文件路径
    'straccess表示允许权限设置的字符串,字符串中带有相应字母表示允许相应权限: r-读,c-读写,f-完全控制
    'blinherit表示是否继承父目录权限.true为继承,false为不继承
    function addpermission(struser,strpath,straccess,blinherit)
    set objwmiservice = getobject(winmgmts:\\.\root\cimv2)
    set fso = createobject(scripting.filesystemobject)
    '得到win32_sid并判断用户/组/内置账户是否存在
    set colusers = objwmiservice.execquery(select * from win32_account where name='&struser&')
    if colusers.count<>0 then
    for each objuser in colusers
    strsid = objuser.sid
    next
    else
    addpermission = 1
    exit function
    end if
    set objsid = objwmiservice.get(win32_sid.sid='&strsid&')
    '判断文件/文件夹是否存在
    pathtype =
    if fso.fileexists(strpath) then pathtype = file
    if fso.folderexists(strpath) then pathtype = folder
    if pathtype = then
    addpermission = 2
    exit function
    end if
    '设置trustee
    set objtrustee = objwmiservice.get(win32_trustee).spawninstance_()
    objtrustee.domain = objsid.referenceddomainname
    objtrustee.name = objsid.accountname
    objtrustee.sid = objsid.binaryrepresentation
    objtrustee.sidlength = objsid.sidlength
    objtrustee.sidstring = objsid.sid
    '设置ace
    set objnewace = objwmiservice.get(win32_ace).spawninstance_()
    objnewace.trustee = objtrustee
    objnewace.acetype = 0
    if instr(ucase(straccess),r) > 0 then objnewace.accessmask = 1179817
    if instr(ucase(straccess),c) > 0 then objnewace.accessmask = 1245631
    if instr(ucase(straccess),f) > 0 then objnewace.accessmask = 2032127
    if pathtype = file and blinherit = true then objnewace.aceflags = 16
    if pathtype = file and blinherit = false then objnewace.aceflags = 0
    if pathtype = folder and blinherit = true then objnewace.aceflags = 19
    if pathtype = folder and blinherit = false then objnewace.aceflags = 3
    '设置sd
    set objfilesecsetting = objwmiservice.get(win32_logicalfilesecuritysetting.path='&strpath&')
    call objfilesecsetting.getsecuritydescriptor(objsd)
    blse_dacl_auto_inherited = true
    if (objsd.controlflags and &h400) = 0 then
    blse_dacl_auto_inherited = false
    objsd.controlflags = (objsd.controlflags or &h400)
    '自动继承位置位,如果是刚创建的目录或文件该位是不置位的,需要置位
    end if
    if blinherit = true then
    objsd.controlflags = (objsd.controlflags and &hefff)
    '阻止继承复位
    else
    objsd.controlflags = (objsd.controlflags or &h1400)
    '阻止继承位置位,自动继承位置位
    end if
    objolddacl = objsd.dacl
    redim objnewdacl(0)
    set objnewdacl(0) = objnewace
    if isarray(objolddacl) then
    '权限为空时objolddacl不是集合不可遍历
    for each objace in objolddacl
    if (blse_dacl_auto_inherited=false and blinherit=true) or ((objace.aceflags and 16)>0 and (blinherit=true) or (lcase(objace.trustee.name)=lcase(struser))) then
    'do nothing
    '当自动继承位置位为0时即使时继承的权限也会显示为非继承,这时所有权限都不设置
    '当自动继承位置位为0时,在继承父目录权限的情况下不设置继承的权限.账户和需要加权限的账户一样时不设置权限
    else
    ubd = ubound(objnewdacl)
    redim preserve objnewdacl(ubd+1)
    set objnewdacl(ubd+1) = objace
    end if
    next
    end if
    objsd.dacl = objnewdacl
    '提交设置修改
    call objfilesecsetting.setsecuritydescriptor(objsd)
    addpermission = 0
    set fso = nothing
    end function
随便看

 

在线学习网考试资料包含高考、自考、专升本考试、人事考试、公务员考试、大学生村官考试、特岗教师招聘考试、事业单位招聘考试、企业人才招聘、银行招聘、教师招聘、农村信用社招聘、各类资格证书考试等各类考试资料。

 

Copyright © 2002-2024 cuapp.net All Rights Reserved
更新时间:2025/5/16 5:15:46