从SharePoint文档库下载最新文件

时间:2015-11-18 21:53:35

标签: powershell sharepoint

警告:不能使用Microsoft.SharePoint.Powershell管理单元。

我想创建一个PowerShell脚本,该脚本将在SharePoint文档库中搜索最新文件。

https://sharepoint.url.com/site/Your_Site_Name/Sub_Site/SharedDocuments/

我正在尝试合并Get-ChildItem以搜索最新文件。

以下是一个例子:

$fromfile = "https://sharepoint.url.com/site/Your_Site_Name/Sub_Site/SharedDocuments/File_name*"
$tofile   = "c:\temp\temp_file.xlsx"

$latest = Get-ChildItem -Path $fromfile | Sort-Object LastAccessTime -Descending | Select-Object -First 1
$latest.name

$webclient = New-Object System.Net.WebClient

$webclient.UseDefaultCredentials = $true

$webclient.DownloadFile($latest.name, $tofile)

我的问题是,当我运行此脚本时,出现以下错误:

Get-ChildItem : Cannot find drive. A drive with the name 'https' does not exist.
At line:4 char:11
+ $latest = Get-ChildItem -Path $fromfile | Sort-Object LastAccessTime

我无法在服务器上使用UNC路径(Win 2012)我正在使用。当我尝试使用'用资源管理器打开'时,我得到“将网站添加到'可信站点'并再试一次。

1 个答案:

答案 0 :(得分:0)

我和您有类似的要求,但我首先开始映射SharePoint UNC路径。问题是它并不总能立即找到它,因此可能需要重试几次:

$SharePoint  = '\\xxx.domain.net\site\Documents'
$LocalDrive  = 'S:'
$Credentials = Get-Credential


if (!(Test-Path $LocalDrive -PathType Container)) {
    $retrycount = 0; $completed = $false
    while (-not $completed) {
        Try {
            if (!(Test-Path $LocalDrive -PathType Container)) {
                (New-Object -ComObject WScript.Network).MapNetworkDrive($LocalDrive,$SharePoint,$false,$($Credentials.username),$($Credentials.GetNetworkCredential().password))
            }
            $Completed = $true
        }
        Catch {
            if ($retrycount -ge '5') {
                Write-Verbose "Mapping SharePoint drive failed the maximum number of times"
                throw "SharePoint drive mapping failed for '$($SharePoint)': $($Global:Error[0].Exception.Message)"
            } else {
                Write-Verbose "Mapping SharePoint drive failed, retrying in 5 seconds."
                Start-Sleep '5'
                $retrycount++
            }
        }
    }
}

映射SharePoint Document Library后,您可以使用所需的所有CmdLets过滤掉您要查找的文件(Get-ChildItem S:\,...)。

如何找到 UNC路径

在浏览器中打开您的网站,转到View all site content,点击Documents,然后选择ActionsOpen with Windows Explorer。地址栏中的链接是您需要的链接。

enter image description here