如何在Perl中修改Windows NTFS权限?

时间:2008-11-19 16:34:03

标签: perl winapi permissions

我在Windows Server 2003上使用ActiveState Perl。

我想在Windows NTFS分区上创建一个目录,然后授予Windows NT安全组对该文件夹的读取权限。在Perl中这可能吗?我是否必须使用Windows NT命令或是否有Perl模块来执行此操作?

非常感谢一个小例子!

2 个答案:

答案 0 :(得分:10)

标准方法是使用Win32::FileSecurity模块:

use Win32::FileSecurity qw(Set MakeMask);

my $dir = 'c:/newdir';
mkdir $dir or die $!;
Set($dir, { 'Power Users' 
            => MakeMask( qw( READ GENERIC_READ GENERIC_EXECUTE ) ) });

请注意Set将覆盖该目录的权限。如果您要编辑现有权限,则需要先Get他们:

my %permissions;
Win32::FileSecurity::Get($dir, \%permissions);
$permissions{'Power Users'}
  = MakeMask( qw( READ GENERIC_READ GENERIC_EXECUTE ) ) });
Win32::FileSecurity::Set($dir, \%permissions);

答案 1 :(得分:7)

Here是ActivePerl的通用权限包。

use Win32::Perms;

# Create a new Security Descriptor and auto import permissions
# from the directory
$Dir = new Win32::Perms( 'c:/temp' ) || die;

# One of three ways to remove an ACE
$Dir->Remove('guest');

# Deny access for all attributes (deny read, deny write, etc)
$Dir->Deny( 'joel', FULL );

# Set the directory permissions (no need to specify the
# path since the object was created with it)
$Dir->Set();

# If you are curious about the contents of the SD
# dump the contents to STDOUT $Dir->Dump;