Powershell: Sample for Download Files from an HTTP page

Import-Module BitsTransfer

$emailFileList = ""
$destinationFolder = "E:\DestTest\"
$uri = "https://test.com/test/test/"
$hrefs = (invoke-webrequest -Uri $uri).links.innerText
$existingFolder = "E:\ExistingFolder"
$existingFiles = Get-ChildItem -Path $existingFolder
$download = $true

foreach ($href in $hrefs)
{
    $download = $true
    foreach ($existingFile in $existingFiles)
    {
        
        if ($href -eq $existingFile.Name) {
            $download = $false
        }
    }

    if ($download -eq $true -and $href -ne "[To Parent Directory]") {
        Start-BitsTransfer -Source "$uri$href" -Destination "$destinationFolder$href"
        $emailFileList += $href + "</br>"
    }
}

$download.Dispose
$existingFiles.Dispose
$existingFolder.Dispose

#### EMAIL NOTIFICATION ####

if ($emailFileList -ne "")
{
    $smtpToday = Get-Date
    $smtpserver = "localhost"
    $msg = new-object Net.Mail.MailMessage
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $msg.From = "test@test.com"
    $msg.To.Add("receipt@test.com")
    $msg.Subject = "Teste $smtpToday"
    $msg.IsBodyHtml = $true
    $msg.Body = $emailFileList
    $smtp.Send($msg)
    $emailFileList.Dispose
}

Comments are closed