Can Powershell be used to list the contents of a URL directory?

有些话、适合烂在心里 提交于 2019-12-01 08:25:58

问题


We have a bunch of zip files hosted on an FTP server, which are also accessible via HTTP. I would love to do something like (gci http://test.com/test/ *.zip ) and give me all the zip files which exist on the webserver.

Does anyone know of a way to do such a thing in a clean way?

TIA


回答1:


this is quite easy with invoke-webrequest (PS V3)

$r=iwr http://asite.com/test2/ -UseBasicParsing  
$r.Links |?{$_.href -match ".zip"} 

of course as +arco444 states, the directory index must be enable


Edit To get the last modified file, you will have to parse the HTML, here is an example (the regex will have to be addapted to your config) :

$col=@()
$link,$date,$size=""
$r=iwr http://asite.com/test2/ 
$r.ParsedHtml.body.getElementsByTagName('TR')|%{ 
    $_.getElementsByTagName('TD') |select -expand innerHTML |%{     
        switch -regex ($_){
            "(.)*zip"{ $link = $_;break}
            "\d{2}-...-\d{4}(.)*"{$date=$_;break}
              "^\d*[KM]"    {$size=$_;break }       
            default{}
        }

    }
        if( $link -and $date -and $size){
        $o=new-object -typename psobject |select  -property "link","date","size"
        $o.link=$link
        $o.date=$date
        $o.size=$size

        $col+=$o
        }
    } 
    $col |select -unique "link","date","size" |sort -desc date |select -last 1


来源:https://stackoverflow.com/questions/27944884/can-powershell-be-used-to-list-the-contents-of-a-url-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!