locked
Help needed: Add-PNPFile modifying attributes during the copy process RRS feed

  • Question

  • Hi,

    I'm relatively new to PowerShell / Scripting, having only been working with it for a few months, so please don't be surprised if this is an obvious fix :D Your assistance will be greatly appreciated all the same.

    With that, I'm hoping the community can shed some light as I'm at a bit of a loss trying to figure this one out.

    Note: This may be TMI but I'd like to be thorough, so I'll start with a little background on the objective/goal, before going in to the error I'm seeing.

    I've been asked to create a PowerShell script for a team at work, to help improve the processing time of reports being moved from a remote server in to our SharePoint Online environment.

    Each source (reporting team) directory on the remote server contains sub-folders, one for each frequency (Daily, Weekly, Monthly, Other). i.e.

    • Team1\Daily
    • Team1\Weekly
    • Team2\Daily
    • and so on..

    The team have requested that each uploaded file has the frequency value added to an attribute field we've created in SharePoint, based on the sub-folder it originated from.

    The script is initiated via a trigger file, which contains data in the form of "Daily,24", where the frequency value tells the script which sub-folder to use as it's source and where the number value is how many hours they'd like to look back, using the LastWriteTime attribute of each source file.

    For the majority of uploads, the script works and does what it's meant to. However, in every bulk upload (I'm typically grabbing ~50-150 files at a time in testing), a handful of files will error out during the run and, while they still upload, they do not have any frequency attribute.

    Coding issues from here.

    The specific line I'm having problems with is:

    $spfile += Add-PNPFile -Path $file.FullName -Folder $variable.destination -Values @{Frequency0 = $frequency}

    where $variable is pulled from a csv file containing a list of source folders on the remote server and their corresponding SPO library and where $frequency is provided by the team inside the trigger file.

    The error I'm seeing for a handful of files is:

    Add-PNPFile : The file MarkReports/POL194_RENEWALS_WITH_NB_QUOTE_01OCT2019.XML has been modified by 
    i:0#.f|membership|<# OMITTED #> on 03 Oct 2019 03:15:34 -0700.
    At F:\Scripts\BI_Sharepoint_Reports\SPOTransfer_v3.ps1:72 char:39
    + ... pdateFile = Add-PNPFile  -Path $file.FullName -Folder $variable.desti ...
    +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : WriteError: (:) [Add-PnPFile], ServerException
        + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile

    It's worth noting that if I clear down the libraries and test again with the same batch of files, it's always the same files that I receive this error on - it doesn't appear to be random.

    and finally, in case this is needed / helps, my full script it's current form (Usernames, UNC Paths, and IP Addresses omitted):

    ## Script to move BI Reports from TPA-Turpin to SharePoint Online
    
    ## Start the transcript
    Start-Transcript -Path 'F:\Scripts\BI_Sharepoint_Reports\Logs\transcript0.txt' -Append
    
    ## Remove Z drive if it exists
    if (Test-Path 'Z:\') {
    
        C:
        Remove-PSDrive -Name 'Z' -Force
    
    }    
    
    ## Build TPA Turpin credentials
    $TPAPassword = Get-Content <# OMITTED #> | ConvertTo-SecureString
    $TPACredentials = New-Object System.Management.Automation.PSCredential(<# OMITTED #>,$TPAPassword)
    
    ## Map Z drive to TPA-Turpin remote server
    New-PSDrive -Name 'Z' -Root <# OMITTED #> -PSProvider FileSystem -Credential $TPACredentials -Persist
    
    ## Check for the GO trigger file
    #$go = Get-Item -Path 'Z:\Data\Motor\Meta Data\PRD\triggers\COPY REPORTS TO SHAREPOINT.txt'
    
    
    ## TESTING ##
    $go = Get-Item -Path 'F:\Scripts\BI_Sharepoint_Reports\TEST TRIGGER.txt'
    
    if ($go) {
    
            ## Copy the variable tables from TPA-Turpin to the local system
            #Copy-Item 'Z:\Data\Motor\Meta Data\SHAREPOINT_REPORTS\Variables.CSV' -Destination 'F:\Scripts\BI_Sharepoint_Reports\TMP' -Force
    
            ## Build SharePoint Online credentials
            $SPOPassword = Get-Content <# OMITTED #> | 
                           ConvertTo-SecureString -Key (Get-Content <# OMITTED #>)
            $SPOCredentials = New-Object System.Management.Automation.PsCredential(<# OMITTED #>,$SPOPassword)
    
            ## Connect to SPonline
            Connect-PnPOnline -Url <# OMITTED #> -Credentials $SPOCredentials
        
            ## Get-PnPList
            $connection = Get-PnPConnection
            $spfile = Get-PnPList -Connection $connection
            
            ## Variables in the GO file
            $frequency = (Get-Culture).TextInfo.ToTitleCase(((Get-Content $go).Split(',')[0]).Trim().ToLower())
            $hours = ((Get-Content $go).Split(',')[-1]).Trim()
    
            ## Build the source variables table - Filename filter, source, destination
            $variables = Import-Csv -Path <# OMITTED #>
    
            ## move to Z drive root
            Z:
            cd\
    
            ForEach ($variable in $variables) {
                
                #cd\
                #cd $variable.folder
    
                $subFolders = Get-ChildItem -Path $('Z:\' + $variable.folder) -Directory |
                              Where-Object {($_.Name -like $frequency)}
                    
                    ForEach ($subFolder in $subFolders) {
                             
                        $files = Get-ChildItem -Path $subFolder.FullName -File |
                                 Where-Object {($_.LastWriteTime -gt (Get-Date).AddHours(-$hours))}
    
                            ForEach ($file in $files) {
                            
                                Write-Host $("Copying " + $file.FullName + ' to ' + $connection.Url + $variable.destination) -ForegroundColor Cyan
                                $spfile += Add-PNPFile -Path $file.FullName -Folder $variable.destination -Values @{Frequency0 = $frequency}
    
                            }
    
                    }
    
            }
            
            ## Delete the trigger file
            #remove-item -path $go.FullName
        
            ## Remove Z drive if it exists
            if (Test-Path 'Z:\'){
    
                C:
                Remove-PSDrive -Name Z -Force
    
            }
            
            ## Stop the transcript
            Stop-Transcript
    
    } else { 
            
            ## Remove Z drive if it exists
            if (Test-Path 'Z:\'){
    
                C:
                Remove-PSDrive -Name Z -Force
    
            }
    
            ## Strop the transcript
            Stop-Transcript
    
    }
    


    I'd greatly appreciate any assistance and insights that can be offered for this.

    Many Thanks,

    grober8070

    Friday, October 4, 2019 8:03 AM

All replies

  • The files are in use and cannot be copied. This may be caused by an AV program or another process modifying or using the files/  This is not a scripting issue.

    Post in SharePoint forum for guidance on how to resolve this kind of conflict.


    \_(ツ)_/

    Friday, October 4, 2019 2:34 PM
  • The files are in use and cannot be copied. This may be caused by an AV program or another process modifying or using the files/  This is not a scripting issue.

    Post in SharePoint forum for guidance on how to resolve this kind of conflict.


    \_(ツ)_/

    Hi jrv,

    Thanks for the response, I'll head over to the SharePoint forums as advised.

    I guess this didn't occur to me, as the files do indeed copy over to SharePoint, even when the error occurs.

    Many Thanks.

    Sunday, October 6, 2019 6:01 AM
  • Hi,

    Was your issue resolved?

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

    If no, please reply and tell us the current situation in order to provide further help.

    Best Regards,

    Lee

    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Friday, November 8, 2019 2:16 PM