简体   繁体   English

未经许可复制文件

[英]copy files without permissions

I have a powershell script that copies some files for later use by a Python script. 我有一个Powershell脚本,该脚本复制了一些文件供以后由Python脚本使用。 The copy operation is this: 复制操作是这样的:

ls $env:MINGW_32 -r -ea silentlycontinue -fo -inc "*.dll" | % { cp $_.fullname "build\lib.win32-2.7" }

Nothing particularly interesting or unusual. 没有什么特别有趣或不寻常的。 The interesting (frustrating) part is what happens later: 有趣的(令人沮丧的)部分是稍后发生的事情:

error: [Error 5] Access is denied: 'build\\bdist.win32\\4758ccaeay32.dll'

CPython in fact has logic in distutils that explicitly resets the file permissions after it copies the DLLs to avoid this exact issue. 实际上,CPython在distutils中具有逻辑,该逻辑在复制DLL之后会显式重置文件权限,以避免出现此确切问题。 I have attempted to replicate this in powershell but have not been successful: 我试图在Powershell中复制此文件,但未成功:

$location = ".\build";
#Search recursivly through location defined;
get-childitem -r $location | foreach{
     $tempLocation = $_.FullName;
     #Get ACL for tempLocation;
     $acl = get-acl $tempLocation;
     #Get SID of explicit ACL;
     $acl.Access | where{
          $_.isinherited -like $false} | foreach{
          #Foreach SID purge the SID from the ACL;
          $acl.purgeaccessrules($_.IdentityReference); 
          #Reapply ACL to file or folder without SID;
          Set-Acl -AclObject $acl -path $tempLocation;
     }
}

Are there any suggestions to reset or preferably avoid copying the file permissions in the first place so that the Python script does not fail? 是否有任何建议重置或最好避免首先复制文件权限,以便Python脚本不会失败?

Try a slightly different approach. 尝试稍微不同的方法。 Instead of rebuilding each file's ACLs build one ACL and apply it to all files. 无需重建每个文件的ACL,而是构建一个ACL并将其应用于所有文件。 Also, I believe that your call to .PurgeAccessRules(<IdentityReference>) should instead be calls to .RemoveAccessRule(<AccessRule>) 另外,我相信您对.PurgeAccessRules(<IdentityReference>)调用应该是对.RemoveAccessRule(<AccessRule>)调用。

$location = ".\build"

#Get ACLs for first file in the folder
$ACL = GCI $location -File | Select -First 1 | Get-Acl
#Remove all non-inherited access rules, piped to Out-Null to avoid 'True' spam as it removes rules
$ACL.Access | ?{!$_.IsInherited} | %{$ACL.RemoveAccessRule($_) | Out-Null}

#Search recursivly through location defined;
GCI -r $location | Set-Acl -AclObject $ACL

Aliases used (truncated results): 使用的别名(结果被截断):

PS C:\Users\TMTech> Get-Alias GCI,?
CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Alias           GCI -> Get-ChildItem
Alias           % -> ForEach-Object
Alias           ? -> Where-Object 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM