PowerShell -WebClient DownloadFile Wildcards?

风格不统一 提交于 2019-12-02 01:55:18

问题


I want to copy multiple files from a SharePoint libary to a local directory. It is possible to use Wildcards? The following code is not working. But is there a way to use the WebClient and Wildcards? (I must use the WebClient. It is not possible to use the SharePoint WebServices :-( )

$url = "http://mySharePoint/websites/Site/TestDocBib/*.jpg"
$path = "D:\temp\"

$WebClient = New-Object System.Net.WebClient
$WebClient.UseDefaultCredentials = $true
$WebClient.DownloadFile($url, $path)

回答1:


you can parse though the html of the list.

# 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)

once you've downloaded the file you can parse though the file - a quick google turned up that Lee Holmes had done most of it already:

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

the main bit you want is the regex:

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

I very quick hack - that may (or may not) work... but the gist is there :)

$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
}



回答2:


No, sorry, you can't use wildcards with WebClient. It's not part of HTTP.




回答3:


What about using WEBDAV?

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

If the client end (i.e. not sharepoint) is a server 2008+ platform, you'll need to add the "desktop experience" role and enable the "webclient" service. This is not the same thing as system.net.webclient; it's the HTTP/DAV network redirector service.

If you need to log in with different credentials, you can use this:

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

Hope this helps.



来源:https://stackoverflow.com/questions/6691272/powershell-webclient-downloadfile-wildcards

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!