小编Sac*_*ers的帖子

在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)
}
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) },我仍然得到错误.
如果你们中的一些高速大师可以帮助向我展示我的方式的错误,我将不胜感激.我希望下载我网站上每个子文件夹中的所有文件,我知道这将涉及一些递归操作,但同样,我目前还没有自己的技能水平.提前谢谢你帮助我!

powershell

11
推荐指数
2
解决办法
2万
查看次数

标签 统计

powershell ×1