PowerShell -WebClient下载文件通配符

本文关键字:通配符 文件 下载 -WebClient PowerShell | 更新日期: 2023-09-27 18:02:08

我想从SharePoint库复制多个文件到本地目录。可以使用通配符吗?下面的代码不起作用。但是有一种方法来使用WebClient和通配符吗?我必须使用WebClient。不能使用SharePoint web服务:-()

$url = "http://mySharePoint/websites/Site/TestDocBib/*.jpg"
$path = "D:'temp'"
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $path)

PowerShell -WebClient下载文件通配符

不,对不起,您不能在WebClient中使用通配符。它不是HTTP的一部分

如何使用WEBDAV?

c:'> copy ''my.sharepoint.site'sites'foo'doclib'*.jpg c:'temp'

如果客户端(即不是sharepoint)是server 2008+平台,你需要添加"桌面体验"角色并启用"web客户端"服务。这和system.net.webclient不一样;这是HTTP/DAV网络重定向服务

如果您需要使用不同的凭据登录,您可以使用:

c:'> net use * "http://my.sharepoint.site/sites/foo/doclib" /user:foobar
mapped h: to ...
c:'> copy h:'*.jpg c:'temp

可以解析列表的HTML。

# dummy url - i've added allitems.aspx
$url = "http://mySharePoint/websites/Site/TestDocBib/allitems.aspx"
$path = "D:'temp'"
$dl_file = $path + "allitems.html"
$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $dl_file)

一旦你下载了这个文件,你就可以解析这个文件了——谷歌一下就会发现Lee Holmes已经完成了大部分工作:

http://www.leeholmes.com/blog/2005/09/05/unit-testing-in-powershell-%E2%80%93-a-link-parser/

你想要的主要位是正则表达式:

$regex = “<'s*a's*[^>]*?href's*='s*[`"']*([^`"'>]+)[^>]*?>” 

我非常快速地破解-这可能(也可能不)工作…但要点在那里:)

$test = gc $dl_file
$t = [Regex]::Matches($test, $regex, "IgnoreCase")
$i = 0
foreach ($tt in $t) { 
    # this assumes absolute paths - you may need to add the hostname if the paths are relative
    $url = $tt.Groups[1].Value.Trim() 
    $WebClient = New-Object System.Net.WebClient
    $WebClient.UseDefaultCredentials = $true
    $WebClient.DownloadFile($url, $($path + $i + ".jpg"))
    $i = $i + 1
}