Advertisement
timsstuff

Get-WebDirectory.ps1

Nov 6th, 2016
1,456
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. [CmdletBinding()]
  2. param(
  3.     [Parameter(Mandatory=$true)][string]$url,
  4.     [Parameter()][string]$dest='.\',
  5.     [Parameter()][switch]$UseBITS=$true,
  6.     [Parameter()][switch]$Recurse=$true,
  7.     [Parameter()][string[]]$FileTypes,
  8.     [Parameter()][switch]$ListOnly=$false,
  9.     [Parameter()][switch]$RecurseIntoFolders=$true,
  10.     [Parameter()][int16]$Wait=0,
  11.     [Parameter()][System.Management.Automation.PSCredential]$Credential
  12.     )
  13.  
  14. Function GetDir($folder, $CurrentUrl, $destfldr) {
  15.     if(!$CurrentUrl.EndsWith('/')) {$CurrentUrl += '/'}
  16.     $fname = $null
  17.     $href = $null
  18.     $head = $null
  19.     ForEach($link in $folder.links) {
  20.         $fname = $link.innerHtml
  21.         $href = $link.href
  22.         If (@('Name','Size','Last modified','Description','Parent Directory','../',$hostname, '[To Parent Directory]', 'Parent directory/', '#', 'File Name', 'File Size', ' ↓ ', 'Date') -notcontains $fname -and $href -notlike '`?*') {
  23.             $head = Invoke-WebRequest $CurrentUrl$href -Method Head -Credential $Credential
  24.             If($head.Headers['content-type'] -like 'text/html*') {
  25.                 #Assume folder
  26.                 If(!$href.EndsWith('/')) { $href += '/'}
  27.                 if($Recurse) {
  28.                     Write-Host "Navigating to subfolder $CurrentUrl$href"
  29.                     Try {
  30.                         $subfolder = Invoke-WebRequest $CurrentUrl$href -Credential $Credential
  31.  
  32.                         If($RecurseIntoFolders) {
  33.                             $destfldr = $dest + $href.Replace('/', '\')
  34.                         }
  35.                         GetDir -Folder $subfolder -CurrentUrl $CurrentUrl$href -destfldr $destfldr
  36.                     }
  37.                     catch { Write-Host "Error reading $CurrentUrl$href $($_.Exception.Message) line $($_.InvocationInfo.ScriptLineNumber)" -ForegroundColor Red }
  38.                 }
  39.             } else {
  40.                 if(@('Name','Size','Last modified','Description','Parent Directory','../',$hostname, '[To Parent Directory]', '#') -notcontains $href `
  41.                 -and ($FileTypes -eq $null -or $FileTypes -contains [System.IO.Path]::GetExtension($href))) {
  42.                     $htmlfile = $href.Replace('&','&')
  43.                     $file = [System.IO.Path]::GetFileName([System.Net.WebUtility]::UrlDecode($href))
  44.                     if(!(Test-Path -LiteralPath $destfldr$file)) {
  45.                         if($ListOnly) {
  46.                             "$CurrentUrl$file" | Out-File $outfile -Append
  47.                         } else {
  48.                             Write-Host "Getting $CurrentUrl$file"
  49.                             #pause
  50.                             $dir = [System.IO.Path]::GetDirectoryName("$destfldr$file")
  51.                             if(!(Test-Path $dir)) {
  52.                                 New-Item $dir -ItemType Directory -Force
  53.                             }
  54.                             if($UseBITS) {
  55.                                 Start-BitsTransfer -Source $CurrentUrl$htmlfile -Destination $destfldr$file
  56.                             } else {
  57.                                 Invoke-WebRequest $CurrentUrl$htmlfile -outfile $destfldr$file -ErrorAction SilentlyContinue -Credential $Credential
  58.                             }
  59.                             if($wait -gt 0) {Start-Sleep $wait}
  60.                         }
  61.                     }
  62.                 }
  63.             }
  64.         }
  65.     }
  66. }
  67.  
  68. $start = Get-Date
  69. Import-Module BitsTransfer
  70.  
  71. $hostname = ([System.Uri]$url).Host
  72.  
  73. if($ListOnly) {
  74.     $outfile = "$dest$hostname.lst"
  75.     "#Directory listing of $($hostname):" | Out-File $outfile
  76. }
  77.  
  78. if($dest.Substring($dest.Length-1, 1) -ne '\') { $dest +='\' }
  79. if(!(Test-Path $dest)) { New-Item $dest -type directory }
  80.  
  81. if($FileTypes -ne $null) {
  82.     For($i=0; $i -lt $FileTypes.length; $i++) {
  83.         $ext = $FileTypes[$i]
  84.         if (!$ext.StartsWith('.')) {
  85.             $ext = ".$ext"
  86.             $FileTypes[$i] = $ext
  87.         }
  88.     }
  89. }
  90.  
  91. $folder = Invoke-WebRequest $url -Credential $Credential
  92.  
  93. GetDir -Folder $folder -CurrentUrl $url -destfldr $dest
  94.  
  95. [gc]::collect()
  96. $end = Get-Date
  97. $elapsed = $end - $start
  98.  
  99. Write-Host "Done! $elapsed" -ForegroundColor Green
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement