足球游戏_中国足彩网¥体育资讯$

vbs通过wmi修改文件文件夹的ntfs权限
来源:易贤网 阅读:938 次 日期:2016-06-22 09:47:29
温馨提示:易贤网小编为您整理了“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

中国足彩网信息请查看脚本栏目
由于各方面情况的不断调整与变化,易贤网提供的所有考试信息和咨询回复仅供参考,敬请考生以权威部门公布的正式信息和咨询为准!
关于我们 | 联系我们 | 人才招聘 | 网站声明 | 网站帮助 | 非正式的简要咨询 | 简要咨询须知 | 加入群交流 | 手机站点 | 投诉建议
工业和信息化部备案号:滇ICP备2023014141号-1 足球游戏_中国足彩网¥体育资讯$ 滇公网安备53010202001879号 人力资源服务许可证:(云)人服证字(2023)第0102001523号
云南网警备案专用图标
联系电话:0871-65317125(9:00—18:00) 获取招聘考试信息及咨询关注公众号:hfpxwx
咨询QQ:526150442(9:00—18:00)版权所有:易贤网
云南网警报警专用图标