在powershell中下载网站文件

在powershell中下载网站文件,powershell,Powershell,我正在尝试获取一个脚本来查询IIS网站上的文件,然后自动下载这些文件。到目前为止,我有: $webclient=New Object System.Net.webclient $source=”http://testsite:8005/" $destination=“C:\users\administrator\desktop\testfolder\” #下一行返回网页中的链接 $testcode1=$webclient.downloadstring($source)-split“我会尝试以下

我正在尝试获取一个脚本来查询IIS网站上的文件,然后自动下载这些文件。到目前为止,我有:

$webclient=New Object System.Net.webclient
$source=”http://testsite:8005/"
$destination=“C:\users\administrator\desktop\testfolder\”
#下一行返回网页中的链接
$testcode1=$webclient.downloadstring($source)-split“我会尝试以下方法:

$webclient = New-Object System.Net.webclient
$source = "http://testsite:8005/"
$destination = "C:\users\administrator\desktop\testfolder\"
#The following line returns the links in the webpage
$testcode1 = $webclient.downloadstring($source) -split "<a\s+" | %{ [void]($_ -match  "^href=['"]([^'">\s]*)"); $matches[1] }
foreach ($line in $testcode1) {
    $Destination = "$destination\$line"
    #Create a new directory if it doesn't exist
    if (!(Test-Path $Destination)){
        New-Item $Destination -type directory -Force
    }
    $webclient.downloadfile($source + $line, $destination + $line)
}
当您将其输入控制台时,它应该准确地吐出其中的内容。然后您可以执行以下附加故障排除:

    "Attempting to copy $Source$line to $Destination$line"
看看它是否看起来像它应该一直向下。你可能需要调整一下我的代码


-Dale Harris从网站下载文件的最佳方法是使用

调用WebRequest–Uri$url

一旦您能够获得html,您就可以解析链接的内容

$result=((调用WebRequest–Uri$url).Links | Where Object{$\uu.href-如“http*”})选择href.href


试一试。它比$webclient=New Object System.Net.webclient更简单。这是用两个例子来补充a的答案

将此堆栈溢出问题下载到
C:/temp/question.htm

Invoke-RestMethod -Uri stackoverflow.com/q/19572091/1108891 -OutFile C:/temp/question.htm
将一个简单的文本文档下载到
C:/temp/rfc2616.txt

Invoke-RestMethod -Uri tools.ietf.org/html/rfc2616 -OutFile C:/temp/rfc2616.txt

我制作了一个简单的Powershell脚本来克隆openbsd包repo。对于类似的事情,它可能会工作/可以用其他方式/用例来实现


这是一个非常明确的问题,+1请考虑使用wget执行此任务。其他详细信息:我正在使用PowerShell 2.0,无法升级到v3或使用PowerShell本机以外的任何cmdlet,也无法在我的计算机上安装任何其他内容,因此我正在尝试获得一个可以在标准Windows 7计算机上执行的解决方案。您尝试过其他方法吗盖伊的方法?是的,这个方法被用作我在这里尝试做的事情的基础,但是当我输入这个方法时,它不会下载任何东西。我理解他试图做的事情,但是他分裂源代码以获取链接的方法似乎效率低下,但是如果不使用他的方法,我很难清除一些有用的东西我自己的代码更好(目前我的部分缺乏熟练程度)。您关于我的
$destination+$line
部分是我的问题的陈述是正确的。在我的测试文件夹中创建子文件夹后,下载了文件,尽管我仍然收到以下错误消息:
异常调用带有“2”参数的“DownloadFile”:webClient请求期间发生异常“
第12行字符:25
+$webClient.downloadfile我想此错误是因为作为链接返回的其中一行指向父目录,在返回的结果中以\列出。我如何获取一条语句以忽略这些?
Invoke-RestMethod -Uri stackoverflow.com/q/19572091/1108891 -OutFile C:/temp/question.htm
Invoke-RestMethod -Uri tools.ietf.org/html/rfc2616 -OutFile C:/temp/rfc2616.txt
# Quick and dirty script to clone a package repo. Only tested against OpenBSD.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$share = "\\172.16.10.99\wmfbshare\obsd_repo\"
$url = "https://ftp3.usa.openbsd.org/pub/OpenBSD/snapshots/packages/amd64/"
cd $share
$packages = Invoke-WebRequest -Uri $url -UseBasicParsing $url
$dlfolder = "\\172.16.10.99\wmfbshare\obsd_repo\"
foreach ($package in $packages.links.href){
    if ((get-item $package -ErrorAction SilentlyContinue)){
        write-host "$package already downloaded"
    } else {
        write-host "Downlading $package"
        wget "$url/$package" -outfile "$dlfolder\$package"
    }
}