问题描述
创建用户AD帐户时,我正在使用Python创建一个新的个人文件夹.正在创建文件夹,但权限不正确.Python可以将用户添加到新创建的文件夹并更改其权限吗?我不确定从哪里开始编写代码.
I'm using Python to create a new personal folder when a users AD account is created. The folder is being created but the permissions are not correct. Can Python add the user to the newly created folder and change their permissions? I'm not sure where to begin coding this.
推荐答案
您需要 win32security
模块,该模块是 pywin32 .这是一个例子你想做.
You want the win32security
module, which is a part of pywin32. Here's an example of doing the sort of thing you want to do.
该示例为文件创建了一个新的DACL并替换了旧的DACL,但是修改现有的DACL很容易;您需要做的就是从安全描述符中获取现有的DACL,而不是创建一个空的DACL,就像这样:
That example creates a new DACL for the file and replaces the old one, but it's easy to modify the existing one; all you need to do is get the existing DACL from the security descriptor instead of creating an empty one, like so:
import win32security
import ntsecuritycon as con
FILENAME = "whatever"
userx, domain, type = win32security.LookupAccountName ("", "User X")
usery, domain, type = win32security.LookupAccountName ("", "User Y")
sd = win32security.GetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION)
dacl = sd.GetSecurityDescriptorDacl() # instead of dacl = win32security.ACL()
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_GENERIC_READ | con.FILE_GENERIC_WRITE, userx)
dacl.AddAccessAllowedAce(win32security.ACL_REVISION, con.FILE_ALL_ACCESS, usery)
sd.SetSecurityDescriptorDacl(1, dacl, 0) # may not be necessary
win32security.SetFileSecurity(FILENAME, win32security.DACL_SECURITY_INFORMATION, sd)
这篇关于如何在Windows中设置文件夹权限?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!