0

I am trying to upload a file (actually copy a file from a local machine) to folder in OneDrive for Business

Unfortunately I have not been able to find any PowerShell examples that can upload a file to OneDrive.

Is it even possible?

$User = "user@domain.fr" 
$SiteURL = "https://domaincorp.sharepoint.com/sites/GED-Domain/Shared%20Documents/Forms/AllItems.aspx?cid=59d99667%2D45be%2D4ad9%2D8df8%2D569b7ce5bfeb&RootFolder=%2Fsites%2FGED%2DDomain%2DIT%2FShared%20Documents%2F04%5FINFRASTRUCTURE%2FBackup&FolderCTID=0x0120003A5DFAA3F20FCA48BBF44305D69A465B"
$Folder = "C:\Users\SysAdmin\Backup\"
#DocDocLibName is document libary name 
$DocLibName = "Backup"
$foldername = "tes"
#Add references to SharePoint client assemblies and authenticate to Office 365 site – required for CSOM
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

$Password  = ConvertTo-SecureString ‘StrongPassword’ -AsPlainText -Force

#Bind to site collection
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$Creds = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User,$Password)

$Context.Credentials = $Creds

#Retrieve list
$List = $Context.Web.Lists.GetByTitle("$DocLibName")
$Context.Load($List)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$ServerRelativeUrlOfRootFolder = $List.RootFolder.ServerRelativeUrl
$uploadFolderUrl=  $ServerRelativeUrlOfRootFolder+"/"+$foldername

#Upload file
Foreach ($File in (dir $Folder -File))
{
$FileStream = New-Object IO.FileStream($File.FullName,[System.IO.FileMode]::Open)
$FileCreationInfo = New-Object Microsoft.SharePoint.Client.FileCreationInformation
$FileCreationInfo.Overwrite = $true
$FileCreationInfo.ContentStream = $FileStream
$FileCreationInfo.URL = $File
 if($foldername -eq $null)
  {
  $Upload = $List.RootFolder.Files.Add($FileCreationInfo)
  }
  Else
  {
   $targetFolder = $Context.Web.GetFolderByServerRelativeUrl($uploadFolderUrl)
   $Upload = $targetFolder.Files.Add($FileCreationInfo);
  }
#$Upload = $List.RootFolder.Files.Add($FileCreationInfo)
$Context.Load($Upload)
$Context.ExecuteQuery()
}

The error is :

Exception lors de l'appel de «ExecuteQuery» avec «0» argument(s): «Cannot contact site at the specified URL https://domaincorp.sharepoint.com/sites/GED-Domain/Shared%20Documents/Forms/AllI
tems.aspx?cid=59d99667%2D45be%2D4ad9%2D8df8%2D569b7ce5bfeb&RootFolder=%2Fsites%2FGED%2Ddomain%2DIT%2FShared%20Documents%2F04%5FINFRASTRUCTURE%2FBackup&FolderCTID=0x0120003A5DFAA3F20FCA48BBF443
05D69A465B.»
Au caractère C:\Users\SysAdmin\Desktop\script.ps1:23 : 1
+ $Context.ExecuteQuery()
+ ~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : ClientRequestException
0

I haven't tested your PoSh, but your $SiteUrl variable should be set to https://domaincorp.sharepoint.com/sites/GED-Domain. $DocLibName would be Documents. With that said, this is much easier using the PnP cmdlets. You'll like it :-)

Connect-PnPOnline https://tenant-my.sharepoint.com/personal/username_domain_tld -SPOManagementShell
Add-PnPFile -Path .\localFileName.docx -Folder 'Documents/parentFolder/subfolder'

So in this example, we connect to our personal OneDrive for Business site (you mentioned ODfB but your example shows an SPO site -- doesn't matter) then we add the file named localFileName.docx from our local computer to the Document Library named Documents (aka https://tenant-my.sharepoint.com/personal/username_domain_tld/Shared%20Documents/) and then within the Documents library, we have a parent folder and subfolder. The file will be uploaded underneath the subfolder.

  • I will test and i will return to you – J.dock Jun 28 at 16:23
  • Not work , but what is PnP cmdlets ? – J.dock Jun 29 at 7:03
  • You don't have access with the creds you used; the cmdlets do work. PnP are open source cmdlets for interacting with SharePoint. They're deveoped by MSFT and the community. – Trevor Seward Jun 29 at 14:13
0
$UserName="BLABLABLA"
$pwd ="StrongPassword"
$SiteUrl = "https://domaincorp-my.sharepoint.com/personal/user_domain_fr/"
$encpassword = convertto-securestring -String $pwd -AsPlainText -Force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword
Connect-PnPOnline -Url $SiteUrl -Credentials $cred
Add-PnPFile -Path .\localFileName.docx -Folder 'Shared Documents\Backup'

The error is :

Add-PnPFile : Access denied. You do not have permission to perform this action or access this resource.
Au caractère C:\Users\SysAdmin\Desktop\scripting.ps1:7 : 1
+ Add-PnPFile -Path .\localFileName.docx -Folder 'Shared Documents\Back ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Add-PnPFile], ServerUnauthorizedAccessException
    + FullyQualifiedErrorId : EXCEPTION,SharePointPnP.PowerShell.Commands.Files.AddFile

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.