Powershell将本地文件的大小与sharepoint上的文件进行比较

时间:2015-12-21 21:53:49

标签: powershell powershell-v3.0

我正在使用当前代码从sharepoint下载文件...

$webClient = New-Object System.Net.WebClient
$webClient.UseDefaultCredentials = $true
$webClient.DownloadFile($sharepointPathFile, $localPathFile) | Out-Null

但是,如果我想检查文件是否已经在本地位置并且大小匹配或不同,该怎么办?我如何使用PowerShell做到这一点?

更新

这是我能得到的最接近......

$url = $sharepointPathFile
$clnt = [System.Net.WebRequest]::Create($url)
$resp = $clnt.GetResponse()
$fileSize = $resp.ContentLength
Write-Host $fileSize

但是我收到以下错误:

Exception calling "GetResponse" with "0" argument(s): "The remote server returned an error: (401) Unauthorized."
At C:\Scripts\Tests\testCheckUpdatedSearchFiles.ps1:345 char:2
+     $resp = $clnt.GetResponse()
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

我有完整的阅读和下载权限,那么还有什么不在这里吗?

1 个答案:

答案 0 :(得分:2)

我不确定“GetResponse”方法是否会返回您正在寻找的内容。根据ContentLength,您可能希望明确定义类型。我会尝试这样的事情:

$webClient = New-Object System.Net.WebClient
$webClient.OpenRead("path/to/file")
[Int64]$fileSize = $webClient.ResponseHeaders["Content-Length"]
Write-Host $fileSize