使用具有适当名称的Powershell从http下载多个文件 [英] Download Multiple Files from http using Powershell with proper names

查看:267
本文介绍了使用具有适当名称的Powershell从http下载多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了类似的内容,并且不断浏览FTP下载答案.这是有用的信息,但最终证明很难翻译.我已经找到了一个Powershell脚本,它可以工作,但是我想知道是否可以根据我的需要对其进行调整.我没有关于Powershell脚本的丰富经验,但是我正在尝试学习.

I have searched for something similar and I keep running across the FTP download answers. This is helpful information, but ultimately proving to be difficult to translate. I have found a powershell script and it works, but I am wondering if it can be tweaked for my needs. I don't have much experience with powershell scripting, but I'm trying to learn.

需要的是这个.我需要在无人照管的情况下将一系列文件下载并安装到远程计算机上.这些文件通过电子邮件通过tinyurls分发.我目前将它们放入.txt文件中,然后使用Powershell脚本读取列表并下载每个文件.

The need is this. I need to download and install a series of files to a remote machine, unattended. The files are distributed via email via tinyurls. I currently throw those into a .txt file, then have a powershell script read the list and download each file.

该项目的需求以及我转向Powershell(而不是其他实用程序)的原因是这些都是非常专业的机器.唯一可用的工具是嵌入Windows 7的工具.

Requirements of the project and why I have turned to powershell (and not other utilities), is that these are very specialized machines. The only tools available are ones that are baked into Windows 7 embedded.

我遇到的困难是: 这些文件一次下载一个.我想在网络服务器允许的同时获取尽可能多的下载. (通常为6)

The difficulties I run into are: The files download one at the time. I would like to grab as many downloads at the same time that the web server will allow. (usually 6)

当前脚本基于tinyurl创建文件名.我需要Web服务器上的实际文件名.

The current script creates file names based off the tinyurl. I need the actual file name from the webserver.

在此先感谢您提出任何建议.

Thanks in advance for any suggestions.

下面是我当前正在使用的脚本.

Below is the script I’m currently using.

#  Copyright (C) 2011 by David Wright (davidwright@digitalwindfire.com)
#  All Rights Reserved.

#  Redistribution and use in source and binary forms, with or without
#  modification or permission, are permitted.

#  Additional information available at http://www.digitalwindfire.com.

$folder = "d:\downloads\"
$userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1"
$web = New-Object System.Net.WebClient
$web.Headers.Add("user-agent", $userAgent)


Get-Content "d:\downloads\files.txt" |
    Foreach-Object { 
        "Downloading " + $_
        try {
            $target = join-path $folder ([io.path]::getfilename($_))
            $web.DownloadFile($_, $target)
        } catch {
            $_.Exception.Message
        }
}

推荐答案

如果在确定文件名之前执行了Web请求,则应该能够获得扩展路径(否则,您将必须发出两个Web请求,一个是获取扩展路径并下载文件).

If you do the web request before you decide on file name you should be able to get the expanded path (otherwise you would have to make two web requests, one to get the extended path and one to download the file).

尝试此操作时,我发现Invoke-WebRequest cmdlet返回的Microsoft.PowerShell.Commands.HtmlWebResponseObjectBaseResponse属性具有ResponseUri属性,这是我们要查找的扩展路径.

When I tried this, I found that the BaseResponse property of the Microsoft.PowerShell.Commands.HtmlWebResponseObject returned by the Invoke-WebRequest cmdlet had a ResponseUri property which was the extended path we are looking for.

如果得到正确的响应,只需使用扩展路径中的名称保存文件,如下所示(此示例代码不会查看HTTP响应代码或类似内容,但希望一切正常):

If you get the correct response, just save the file using the name from the extended path, something like the following (this sample code does not look at HTTP response codes or similar, but expects everything to go well):

function Save-TinyUrlFile
{
    PARAM (
        $TinyUrl,
        $DestinationFolder
    )

    $response = Invoke-WebRequest -Uri $TinyUrl
    $filename = [System.IO.Path]::GetFileName($response.BaseResponse.ResponseUri.OriginalString)
    $filepath = [System.IO.Path]::Combine($DestinationFolder, $filename)
    try
    {
        $filestream = [System.IO.File]::Create($filepath)
        $response.RawContentStream.WriteTo($filestream)
        $filestream.Close()
    }
    finally
    {
        if ($filestream)
        {
            $filestream.Dispose();
        }
    }
}

鉴于存在$HOME\Documents\Temp文件夹,可以使用类似于以下内容的方法来调用此方法:

This method could be called using something like the following, given that the $HOME\Documents\Temp folder exists:

Save-TinyUrlFile -TinyUrl http://tinyurl.com/ojt3lgz -DestinationFolder $HOME\Documents\Temp

在我的计算机上,该文件将从github存储库中获取的名为robots.txt的文件保存到我的计算机上.

On my computer, that saves a file called robots.txt, taken from a github repository, to my computer.

如果您想同时下载许多文件,可以让PowerShell帮您实现.使用PowerShell工作流并行功能,或仅为每个URL启动Job.以下是有关如何使用PowerShell Jobs做到这一点的示例:

If you want to download many files at the same time, you could let PowerShell make this happen for you. Either use PowerShell workflows parallel functionality or simply start a Job for each url. Here's a sample on how you could do it using PowerShell Jobs:

Get-Content files.txt | Foreach {
    Start-Job {
        function Save-TinyUrlFile
        {
            PARAM (
                $TinyUrl,
                $DestinationFolder
            )

            $response = Invoke-WebRequest -Uri $TinyUrl
            $filename = [System.IO.Path]::GetFileName($response.BaseResponse.ResponseUri.OriginalString)
            $filepath = [System.IO.Path]::Combine($DestinationFolder, $filename)
            try
            {
                $filestream = [System.IO.File]::Create($filepath)
                $response.RawContentStream.WriteTo($filestream)
                $filestream.Close()
            }
            finally
            {
                if ($filestream)
                {
                    $filestream.Dispose();
                }
            }
        }

        Save-TinyUrlFile -TinyUrl $args[0] -DestinationFolder $args[1]
    } -ArgumentList $_, "$HOME\documents\temp"
}

这篇关于使用具有适当名称的Powershell从http下载多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆