PowerShell Fix permissions issues for Robocopy

Source: Internet
Author: User
Tags ntfs permissions robocopy

Recently beans company in the transfer file server, about 80T of files need to be sent to the cloud. The beans were directly copied using the Robocopy. Because of historical reasons, some folders have a wonderful set of permissions, resulting in the bean account also does not have permission to access, the result is that robocopy inside may have Bay even thousands of folders because of permissions problems can not be copied.


Robocopy Command Example

Robocopy C:\source d:\destination/e/w:30/r:3/log+: "C:\temp\log.txt"/xf. *


What if we solve this problem? The idea of a bean is to get the log file for the Robocopy, extract the failed path through the regular, and then re-seize the administrator rights through the script and then re-assign NTFS permissions.


For example, the following Robocopy log, the error message is as follows

New Dir 0\\syd02\track\. Temporaryitems\folders.1138144168\cleanup at Startup\. BAH. FFTCC2017/11/12 08:58:18 ERROR 5 (0x00000005) scanning Source Directory \\syd02\Track\. Temporaryitems\folders.1138144168\cleanup at Startup\. BAH. Fftcc\altitude Business Card Visualsaccess is denied. Waiting seconds ... Retrying ... 2017/11/12 09:01:18 ERROR 5 (0x00000005) scanning Source Directory \\syd02\Track\. Temporaryitems\folders.1138144168\cleanup at Startup\. BAH. Fftcc\altitude Business Card Visualsaccess is denied. Error:retry LIMIT exceeded.


The PowerShell script for extracting paths is as follows:

#Read robocopy Log files$contents=gc c:\temp\tracktransfer.txt-raw#regular Expression pattern$patt= ' (\\\\syd02.*\n) Access is denied ' [email protected] () #Distract The path of broken folders$contents | Select-string-pattern $patt-allmatches | foreach {$result +=$_.matches.groups | Where-object {$_.name-eq 1}| Select-expandproperty value} #remove duplicated record and blank Lines$result |sort-unique | foreach {$_.trimend ()} | Where{$_-ne ""} | Set-content C:\temp\list.txt

The resulting text file is as follows:


List.txt

\\syd02\track\clients\westpac\westpac cards\1_acquisition\fy 17\q1 campaigns\wbccar7498  Q1.1 campaign\production\\syd02\track\. Temporaryitems\folders.1138144168\cleanup at startup\. BAH. Fftcc\altitude business card visuals\\syd02\track\clients\westpac\westpac cards\1_ Acquisition\fy 17\q1 campaigns\wbccar7487 q1 ooh - low rate\creative\\syd02 \track\. Temporaryitems\folders.1138144168\cleanup at startup\. Bah.cj82c\westpac brand assets\\syd02\track\clients\westpac\westpac cards\2_lifecycle\ Wbccar7602 additional cardholder\copy\\syd02\track\clients\westpac\westpac cards\2_lifecycle\ Wbccar7602 additional cardholder\production\\syd02\track\clients\westpac\westpac crm\business\ 3. complete\2016\wbccrm7595_q4 sme relationship email\creative\final\individual  Modules\modules half width\\syd02\track\clients\westpac\westpac cards\1_acquisition\fy 17\q2 campaigns\wbccar7516 microsite optimisation\finance\\ Syd02\track\clients\westpac\westpac cards\1_acquisition\fy 17\q1 campaigns\wbccar7487 q1  ooh - low rate\finance\


Because recently in the review of Python, handy and Python wrote a similar function, the effect is the same

Fp=open (' C:/temp/tracktransfer.txt ') fp2=open (' C:/temp/list.txt ', ' W ') Contents=fp.read () Pat=r ' (\\\\syd02.*\n) Access is denied ' Ret=re.findall (pat,contents) ret=set (ret) for item on Ret:print (item) fp2.write (item) print ("Total Nu Mber is%d "%len (ret)) Fp.close () Fp2.close ()


-----------------------


And then there's the play, how do I regain permissions and assignments?

PowerShell's own Get-acl and SET-ACL commands, as well as DOS commands takeown I have tried, honestly, not so good, and then fortunately found on the Internet a third-party module ntfssecurity, inside the function fully realized I need the function.

Https://gallery.technet.microsoft.com/scriptcenter/1abd77a5-9c0b-4a2b-acef-90dbb2b84e85#content


After downloading, unzip directly to the corresponding PowerShell module path and restart PowerShell ise to load automatically.


Here is the back half of the code that modifies the owner and access rights of the target directory and its subdirectory files

$a  = Get-Content  "C:\temp\list.txt" #For  each folder and subfolders,  setup the ownership and ntfs permissionsforeach  ($i  in  $a) {     if (test-path  $i)   {        write-host  Taking ownership of Directory  $i  -fore Green          get-item  $i  | Set-NTFSOwner -Account  ' omnicom\group  Australia it access '         get-item  $i  |  add-ntfsaccess -account  ' omnicom\group australia it access '  -AccessRights  fullcontrol        get-item  $i  | add-ntfsaccess - account  ' Omnicom\sydney track all staff '  -AccessRights modify           $items  = @ ()          $items  =  $null           $path  =  $null           #if  need to setup all subfolders, we can use -recusrse  in the following cmdlet.         $items  =  get-childitem  $i  -force        foreach ($item  in   $items)             {                  $path  =  $item. fullname                 WRITE-HOST&NBSP, ..... adding admingroup to  $path  -Fore Green                 get-item -force  $path  | Set-NTFSOwner -Account  ' omnicom\ Group australia it access '                  get-item  $i  | Add-NTFSAccess -account  ' Omnicom\group  australia it access '  -AccessRights FullControl                 get-item  $i  | add-ntfsaccess - account  ' Omnicom\sydney track all staff '  -AccessRights modify             }   }}


The final effect is


650) this.width=650; "Src=" Https://s5.51cto.com/oss/201711/13/ddb721cc3f1e73e1789b1d7bd337bc88.png-wh_500x0-wm_3 -wmp_4-s_173397825.png "title=" 22.PNG "alt=" Ddb721cc3f1e73e1789b1d7bd337bc88.png-wh_ "/>


This way, if the Robocopy out what permissions aspects of the problem, I can easily solve the script, once no, repair and then run once again Robocopy can quickly copy the lost files back ~

This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1981373

PowerShell Fix permissions issues for Robocopy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.