PowerShell的-WebClient DownloadFile通配符? [英] PowerShell -WebClient DownloadFile Wildcards?

查看:984
本文介绍了PowerShell的-WebClient DownloadFile通配符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要多个文件从SharePoint libary复制到本地目录。
它可以使用通配符?
下面的代码是行不通的。但有使用Web客户端和通配符的方法吗?
(我必须使用Web客户端,这是不可能使用SharePoint Web服务:-()

  $网址=HTTP://mySharePoint/websites/Site/TestDocBib/*.jpg
$ PATH =D:\temp\

$的WebClient =新对象系统。 Net.WebClient
$ WebClient.UseDefaultCredentials = $真
$ WebClient.DownloadFile($网址,$ PATH)


解决方案

您可以分析,虽然该列表的HTML。

 #假网址 - 我已经添加allitems.aspx 
$ URL =HTTP://mySharePoint/websites/Site/TestDocBib/allitems.aspx
$ PATH =D:\temp \
$ dl_file = $ PATH +allitems.html

$的WebClient =新对象System.Net.WebClient
$ WebClient.UseDefaultCredentials = $真
$ WebClient.DownloadFile($网址,$ dl_file)

一旦你下载了该文件可以分析,虽然该文件 - 一个快速谷歌打开了李霍姆斯做了大部分早已:



http://www.leeholmes.com/blog/2005 / 09/05 /单元测试功能于powershell-%E2%80%93-A-链接解析器/



您希望主位是正则表达式:

  $正则表达式=< \s * a\s * [^>] *? href\s * = * \s [`] *([^`'>] +)[^>] *>中

我非常快的黑客 - 这可能(也可能不会)工作......但要点有:)

  $测试= $ GC dl_file 

$ T = [正则表达式] ::匹配($考验,正则表达式$IGNORECASE)
$ I = 0
的foreach(在$ T $ TT){
#这是假设绝对路径 - 你可能需要添加主机名,如果路径是相对于
$ URL = $ TT。组[1] .Value.Trim()
$的WebClient =新对象System.Net.WebClient
$ WebClient.UseDefaultCredentials = $真
$ WebClient.DownloadFile($网址,$( $ PATH + $ I +.JPG))
$ I = $ I + 1
}


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)

解决方案

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
}

这篇关于PowerShell的-WebClient DownloadFile通配符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆