script to copy files from on premise file server to SP ONLINE

%3CLINGO-SUB%20id%3D%22lingo-sub-743506%22%20slang%3D%22en-US%22%3Escript%20to%20copy%20files%20from%20on%20premise%20file%20server%20to%20SP%20ONLINE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-743506%22%20slang%3D%22en-US%22%3E%3CDIV%20class%3D%22ltr%22%3E%3CDIV%3E%3CDIV%3E%3CDIV%3E%3CDIV%20class%3D%22alley%22%3E%3CDIV%20class%3D%22wrapper%22%3E%3CDIV%20class%3D%22inner%22%3E%3CDIV%20class%3D%22Technet%22%3E%3CDIV%20class%3D%22contentWrapperFull%22%3E%3CDIV%20class%3D%22contentFull%22%3E%3CDIV%3E%3CDIV%3E%3CDIV%20class%3D%22section%22%3E%3CUL%3E%3CLI%3E%3CDIV%20class%3D%22messageContentContainer%22%3E%3CDIV%20class%3D%22messageSidebar%22%3E%3CDIV%20class%3D%22iconWrapper%22%3E%3CDIV%20class%3D%22status%20messageIcon%20%20question%20root%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22votingouterbox%22%3E%3CDIV%20class%3D%22voting%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fi1.social.s-msft.com%2Fglobalresources%2FImages%2Ftrans.gif%3Fcver%3D0001%22%20border%3D%220%22%20alt%3D%22You%20cannot%20vote%20on%20your%20own%20post%22%20title%3D%22You%20cannot%20vote%20on%20your%20own%20post%22%20%2F%3E%3CDIV%20class%3D%22votenumber%22%3E0%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22votinglabel%22%3E%26nbsp%3B%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CDIV%20class%3D%22messageContent%22%3E%3CDIV%20class%3D%22container%22%3E%3CDIV%20class%3D%22body%22%3E%3CP%3EI%20have%20a%20script%20that%20I%20found%20in%20many%20forms%20online%20that%20I%20re-tooled%20to%20work%20in%20my%20environment.%3C%2FP%3E%3CP%3EThe%20purpose%20of%20the%20script%20is%20to%20copy%20files%20from%20local%20File%20Server%20to%20a%20Library%20on%20my%20SharePoint%20Online.%3C%2FP%3E%3CP%3EI%20have%20it%20working%20to%20the%20site%20I%20want%20it%20to%20go%20to%20as%20long%20as%20the%20site's%20%22Members%20Group%22%20has%20Write%20Access%20to%20the%20sub%20directory.%20in%20this%20scenario%2C%20I%20need%20to%20limit%20access%20to%20this%20library%20and%20don't%20want%20the%20Sites%20%22Members%20Group%22%20to%20have%20access.%3C%2FP%3E%3CP%3Ewhen%20I%20remove%20the%20%22Members%20Group%22%20and%20add%20explicit%20permissions%20for%20the%20Licensed%20O365%26nbsp%3BUser%20that%20executes%20the%20upload%2C%20I%20get%20these%20errors%3A%3C%2FP%3E%3CP%3EYou%20cannot%20call%20a%20method%20on%20a%20null-valued%20expression.%3CBR%20%2F%3EAt%20C%3A%5CTESTMove_file.ps1%3A70%20char%3A5%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24FileUploaded%20%3D%20%24FolderToUpload.Files.Add(%24FileCreationInfo)%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20CategoryInfo%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3A%20InvalidOperation%3A%20(%3A)%20%5B%5D%2C%20RuntimeException%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20FullyQualifiedErrorId%20%3A%20InvokeMethodOnNull%3C%2FP%3E%3CP%3ECannot%20find%20an%20overload%20for%20%22Load%22%20and%20the%20argument%20count%3A%20%221%22.%3CBR%20%2F%3EAt%20C%3A%5CTESTMove_file.ps1%3A73%20char%3A2%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24Context.Load(%24FileUploaded)%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20~~~~~~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20CategoryInfo%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodException%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20FullyQualifiedErrorId%20%3A%20MethodCountCouldNotFindBest%3C%2FP%3E%3CP%3EException%20calling%20%22ExecuteQuery%22%20with%20%220%22%20argument(s)%3A%20%22The%20remote%20server%20returned%20an%3CBR%20%2F%3Eerror%3A%20(401)%20Unauthorized.%22%3CBR%20%2F%3EAt%20C%3A%5CTESTMove_file.ps1%3A74%20char%3A5%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%24Context.ExecuteQuery()%3CBR%20%2F%3E%2B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20~~~~~~~~~~~~~~~~~~~~~~~%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20CategoryInfo%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3A%20NotSpecified%3A%20(%3A)%20%5B%5D%2C%20MethodInvocationException%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2B%20FullyQualifiedErrorId%20%3A%20WebException%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20haven't%20been%20able%20to%20find%20a%20reason%20this%20wouldn't%20work%3F%20Please%20advise.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethanks%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLI%3E%3C%2FUL%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-743506%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ESharePoint%20Online%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-754150%22%20slang%3D%22en-US%22%3ERe%3A%20script%20to%20copy%20files%20from%20on%20premise%20file%20server%20to%20SP%20ONLINE%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-754150%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F372976%22%20target%3D%22_blank%22%3E%40DH_Savage%3C%2FA%3E%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDid%20you%20give%20these%20named%20users%20Contribute%20or%20Edit%20permissions%3F%26nbsp%3B%20The%20members%20group%20has%20Edit%20by%20default%20and%20I've%20seen%20strange%20behavior%20if%20you%20drop%20to%20Contribute.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
New Contributor
  •  
    You cannot vote on your own post
    0
     

    I have a script that I found in many forms online that I re-tooled to work in my environment.

    The purpose of the script is to copy files from local File Server to a Library on my SharePoint Online.

    I have it working to the site I want it to go to as long as the site's "Members Group" has Write Access to the sub directory. in this scenario, I need to limit access to this library and don't want the Sites "Members Group" to have access.

    when I remove the "Members Group" and add explicit permissions for the Licensed O365 User that executes the upload, I get these errors:

    You cannot call a method on a null-valued expression.
    At C:\TESTMove_file.ps1:70 char:5
    +     $FileUploaded = $FolderToUpload.Files.Add($FileCreationInfo)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : InvokeMethodOnNull

    Cannot find an overload for "Load" and the argument count: "1".
    At C:\TESTMove_file.ps1:73 char:2
    +     $Context.Load($FileUploaded)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodCountCouldNotFindBest

    Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an
    error: (401) Unauthorized."
    At C:\TESTMove_file.ps1:74 char:5
    +     $Context.ExecuteQuery()
    +     ~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : WebException

     

     

    I haven't been able to find a reason this wouldn't work? Please advise.

     

    thanks

4 Replies
Highlighted

@DH_Savage,

 

Did you give these named users Contribute or Edit permissions?  The members group has Edit by default and I've seen strange behavior if you drop to Contribute.  

Highlighted

@Doug Allen 

 

yes the User that I use to Connect to Sharepoint is in the "Owners" group for that document library.

 

I have even explicitly granted it Full control over the library.

Highlighted

@DH_Savage,

 

Besides the script, was there a reason why you weren't using the free SharePoint migration tool to migrate the files instead of the script?  

 

https://docs.microsoft.com/en-us/sharepointmigration/fileshare-to-odsp-migration-guide

Highlighted

@Doug Allen 

 

Yeah, trying to automate it, schedule it for when a file is created in the on prem directory.