简体   繁体   中英

PowerShell WebClient downloads leaves corrupted file

I am running the following PowerShell to try and download an Excel xlsx formatted spreadsheet form SharePoint.

$ExcelFileLocal = 'C:\Temp\Test.xlsx'
$ExcelFileRemote = '<long https url to Excel file taken from SharePoint using Copy Shortcut command>'
$credentials = Get-Credential -UserName $strName
$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = $credentials 
$webclient.DownloadFile($ExcelFileRemote, $ExcelFileLocal)

When trying to open the file with Excel or a new COM object in PowerShell the following error is given:

Excel cannot open the file 'Test.xlsx' because the file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file.

If a test using a .txt file instead of an Excel file there is no problem in opening the resultant file.

The answer listed here: Opening an Excel document from SharePoint using PowerShell gives the same error as shown.

EDIT:

The file I am trying to download and further work on appears to be XML and can be opened in a text editor to show this header:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns:o="urn:schemas-microsoft-com:office:office" lang="en-us" dir="ltr">
<head><meta http-equiv="X-UA-Compatible" content="IE=8" /><meta name="GENERATOR" content="Microsoft SharePoint" /><meta name="progid" content="SharePoint.WebPartPage.Document" /><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><meta http-equiv="Expires" content="0" /><title>

A more simple .xlsx file I have tested with successfully doesn't have this XML format and can be further processed. Not sure why this difference is coming about.

长网址错误-正确现在可以正常工作了。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM