我正在尝试获取一个脚本来查询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)
}
Run Code Online (Sandbox Code Playgroud)
我还不擅长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) }
,我仍然得到错误.
如果你们中的一些高速大师可以帮助向我展示我的方式的错误,我将不胜感激.我希望下载我网站上每个子文件夹中的所有文件,我知道这将涉及一些递归操作,但同样,我目前还没有自己的技能水平.提前谢谢你帮助我!
这是用两个例子来补充 A_N 的答案。
下载此 Stackoverflow 问题至C:/temp/question.htm
。
Invoke-RestMethod -Uri stackoverflow.com/q/19572091/1108891 -OutFile C:/temp/question.htm
Run Code Online (Sandbox Code Playgroud)
将简单的文本文档下载到C:/temp/rfc2616.txt
.
Invoke-RestMethod -Uri tools.ietf.org/html/rfc2616 -OutFile C:/temp/rfc2616.txt
Run Code Online (Sandbox Code Playgroud)
小智 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)
}
Run Code Online (Sandbox Code Playgroud)
我认为你这里唯一的问题是你从一个新目录中获取一个新文件,并将其放入一个尚不存在的文件夹中(我可能是错误的)。
如果这不能解决您的问题,您可以执行一些其他故障排除:
将每一行单独复制到 powershell 窗口中,然后将它们运行到 foreach 循环。然后输入持有所有黄金的变量:
$testcode1
Run Code Online (Sandbox Code Playgroud)
当您将其输入控制台时,它应该准确地输出其中的内容。然后您可以执行其他故障排除,如下所示:
"Attempting to copy $Source$line to $Destination$line"
Run Code Online (Sandbox Code Playgroud)
看看它看起来是否像它应该一直向下的样子。您可能需要稍微调整一下我的代码。
——戴尔·哈里斯
归档时间: |
|
查看次数: |
19062 次 |
最近记录: |