none
powershell script to download RRS feed

  • Question

  • $FilePath =  "https://xxxl.com/clients/AllItems.aspx/ClientRepo.xlsx"
    $localPath = "c:\test\ClientRep.xlsx"
    $wc = New-Object System.Net.Webclient
    $wc.UseDefaultCredentials = $true
    $wc.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    $wc.DownloadFile($FilePath, $localPath)

    I am able to download Excel from sharepoint site with above script but unable to open Excel file ,  getting error message "format or file type is invalid",  when i try to download manually from site it works fine but trying to do through script giving error.  

    do i need to specify any header information in above script

    Thanks

    V

    Tuesday, January 3, 2017 3:19 AM

All replies

  • Hello,

    This is a forum for SQL Server Transact-SQL; you better should post your question to a PowerShell or SharePoint related forum.


    Olaf Helper

    [ Blog] [ Xing] [ MVP]

    Tuesday, January 3, 2017 6:15 AM
  • Hi,

    Please try to use SharePoint CSOM with PowerShell to achieve it, the following script for your reference:

    $siteUrl = "https://xxx.sharepoint.com"
    
    $library = "Class Documents"
    $listItemId = 24 #item's list ID
    
    $toPath = "c:\xfer\" #yourfile.xlsx"
    
    $username="username"
    $password="***"
    $domain="doamin name"
    
    #$username = "xxx@somesite.onmicrosoft.com"
    #$password = Read-Host -Prompt "Password for $username" -AsSecureString 
    
    $context = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
    $context.Credentials=New-Object System.Net.NetworkCredential($username, $password, $domain);
    #$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username,$password) 
    $web = $context.Web
    $context.Load($web)
    $context.ExecuteQuery()
    
    $list = $context.Web.Lists.GetByTitle($library)
    $listitem = $list.GetItemById($listItemId)
    $context.Load($list)
    $file=$listItem.File
    $context.Load($file)
    $context.ExecuteQuery()
    
    $fileRef = $file.ServerRelativeUrl
    $fileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($context, $fileRef)
    $fileName = $toPath + $File.Name
    $fileStream = [System.IO.File]::Create($fileName)
    $fileInfo.Stream.CopyTo($fileStream);
    
    $fileStream.Close()

    More information:

    CSOM SharePoint PowerShell Reference and Example Codes

    https://social.technet.microsoft.com/wiki/contents/articles/29518.csom-sharepoint-powershell-reference-and-example-codes.aspx

    Download a document from  SharePoint Document Library using CSOM 2013

    http://www.letsharepoint.com/download-a-document-from-sharepoint-document-library-using-csom-2013/

    Best Regards,

    Dennis


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


    Wednesday, January 4, 2017 6:58 AM
    Moderator