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

前端 未结 1 1381
南方客
南方客 2021-01-14 05:49

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 al

相关标签:
1条回答
  • 2021-01-14 06:06

    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 directory listing


    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
    
    0 讨论(0)
提交回复
热议问题