[英]Downloading website files in powershell
我正在尝试获取一个脚本来查询 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
值更改$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) }
,我仍然遇到错误。
如果你们中的一些高速大师能帮助我指出我的方法的错误,我将不胜感激。 我希望下载我网站上每个子文件夹中的所有文件,我知道这会涉及使用一些递归操作,但同样,我自己目前没有这样做的技能水平。 预先感谢您帮助我!
从网站下载文件的最佳方式是使用
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 更简单
这是用两个例子来补充 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
我制作了一个简单的 Powershell 脚本来克隆 openbsd 包存储库。 对于类似的事情,它可能会工作/可以以其他方式/用例实现。
# 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"
}
}
我会试试这个:
$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"
看看它是否看起来应该一直向下。 您可能需要稍微调整一下我的代码。
-戴尔哈里斯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.