Page 1 of 1

Upload a file to Sharepoint

Posted: Tue Aug 13, 2019 7:12 am
by gijsbert
I wrote a powershell script to upload a file to sharepoint online.
The script works fine in PowerShell ISE, but throws an error when used in a powershell studio form. Can some one help me figure this out?

$FileStream = New-Object IO.FileStream($SourceFile.FullName, [System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()

throws error:
ERROR: Exception calling "Add" with "1" argument(s): "The 'parameters.Url' argument is invalid."
Test-OneDrive.psf (93, 5): ERROR: At Line: 93 char: 5
ERROR: + ... $Upload = $targetFolder.Files.Add($FileCreationInfo);
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodInvocationException
ERROR: + FullyQualifiedErrorId : ArgumentException
ERROR:
ERROR: Cannot find an overload for "Load" and the argument count: "1".
Test-OneDrive.psf (97, 4): ERROR: At Line: 97 char: 4
ERROR: + $Context.Load($Upload)
ERROR: + ~~~~~~~~~~~~~~~~~~~~~~
ERROR: + CategoryInfo : NotSpecified: (:) [], MethodException
ERROR: + FullyQualifiedErrorId : MethodCountCouldNotFindBest
ERROR:

Re: Upload a file to Sharepoint

Posted: Tue Aug 13, 2019 7:27 am
by jvierra
The error tells you what the problem is. The "$file" argument is invalid.

Re: Upload a file to Sharepoint

Posted: Tue Aug 13, 2019 7:47 am
by Alexander Riedel
Probably works in the ISE because $File is contaminated from
something else but not initialized in the script.