12

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

$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 $test2) {
    $webclient.downloadfile($source + $line, $destination + $line)
}


我还不太擅长 PowerShell,并且遇到了一些错误,但我设法将几个测试文件放入我的 wwwroot 文件夹中(web.config 文件似乎无法下载,所以我想这是我的错误之一)。当我尝试将我的$source值更改为我网站上包含一些测试文本文件的子文件夹时(例如 = http://testsite:8005/subfolder/,我得到错误并且根本没有下载。运行我$testcode1的将在我的子文件夹中给我以下链接:
/subfolder/test2/txt
/
/subfolder/test1.txt
/subfolder/test2.txt
我不知道为什么它列出了 test2 文件两次。我想我的问题是,因为它返回子文件夹/文件格式,所以我遇到了错误,因为我试图下载$source + $line,这基本上是http://testsite:8005/subfolder/subfolder/test1.txt,但是当我试图通过添加来解决这个问题时一个$root值是我网站的根目录并执行foreach($line in $testcode1) { $webclient.downloadfile($root + $line, $destination + $line) },我仍然得到错误。
如果你们中的一些高速大师可以帮助我指出我的方法的错误,我将不胜感激。我希望下载我网站上每个子文件夹中的所有文件,我知道这将涉及使用一些递归操作,但同样,我自己目前没有这样做的技能水平。预先感谢您帮助我!

4

4 回答 4

4

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

Invoke-WebRequest –Uri $url

一旦您能够掌握 html,您就可以解析链接的内容。

$result = (((Invoke-WebRequest –Uri $url).Links | Where-Object {$_.href -like “http*”} ) | select href).href

试试看。它比 $webclient = New-Object System.Net.webclient 更简单

于 2016-09-06T09:49:57.940 回答
4

这是用两个例子来扩充 A_N 的答案。

将此 Stackoverflow 问题下载到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
于 2018-02-15T22:39:37.487 回答
2

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

GitHub链接

# 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"
    }
}
于 2018-07-16T15:03:17.920 回答
1

我会试试这个:

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

我认为您在这里唯一的问题是您从新目录中获取了一个新文件,并将其放入一个尚不存在的文件夹中(我可能弄错了)。

如果这不能解决您的问题,您可以进行一些额外的故障排除:

将每一行单独复制到您的 powershell 窗口中并将它们运行到 foreach 循环。然后输入包含所有黄金的变量:

    $testcode1

当您将其输入控制台时,它应该准确地吐出那里的内容。然后你可以像这样进行额外的故障排除:

    "Attempting to copy $Source$line to $Destination$line"

看看它是否一直看起来像它应该的样子。您可能需要稍微调整一下我的代码。

——戴尔·哈里斯

于 2013-10-24T20:51:24.423 回答