Optimizing Invoke-WebRequest and email script.

Hi

I am pretty new to PowerShell but have made some pretty handy routines so far.
They are pretty simple, and I was hoping that maybe someone in here could help me optimize this routine I am using to download a .zip file, and then emailing it.

The scripts run every time, but once in a while, the attached file in the email is missing?
Not sure why, because my routines generate no logs, so maybe I should start there.
al I think that I should add some sort of - If the email does not contain the attached file, try again or something like that.

Let me hear you thought about, what you would add in a routine like the one in my example?

# Downloading the file needed for the email repport
$GetDate = Get-Date -Format 'yyyy-MM-dd'
$securepassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("Username", $securepassword)
$OutPutPathZip = 'C:\Temp\Zip'
$OutputPathCSV = 'C:\Temp\CSV'
$OutputPathList = 'C:\Temp\Download '

Invoke-WebRequest -Uri "https://webadress" -Credential $credentials -OutFile $OutPutPathZip\data_${GetDate}.zip
Expand-Archive $OutPutPathZip\*.zip -DestinationPath $OutputPathCSV 
Remove-Item $OutputPathZip\*.zip
Move-Item $OutputPathCSV\FileFromZipFile.txt -Destination $OutputPathList\FileFromZipFile_${GetDate}.csv
Remove-Item $OutputPathCSV\*.txt

# Processing the file that was downloaded.
Start-Process "C:\Program Files\QlikView\qv.exe" "/r C:\locationOfAOtherprogram.qvw"

Start-Sleep -s 15

#Sending the file that has now been processed.
#Variables
$GetDate = Get-Date -Format 'yyyy-MM-dd'

# Sender and Recipient Info
$MailFrom = "sender@mail.com"
$MailTo = "receiver@mail.com"
$MailBcc = "sender@mail.com"
$Attachment = "C:\_${GetDate}.csv"

# Sender Credentials
$Username = "Username"
$Password = "Password"

# Server Info
$SmtpServer = "servername"

# Message stuff
$MessageSubject = "Errorlist $GetDate " 
$Message = New-Object System.Net.Mail.MailMessage $MailFrom,$MailTo
$Message.bcc.Add($MailBcc)
$Message.IsBodyHTML = $true
$Message.Subject = $MessageSubject
$Message.Attachments.Add($Attachment)
$Message.Body = @'
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p style="text-align: left;">Hej</p>
<p style="text-align: left;">Here is the error list</p>
<p style="text-align: left;">/Daniel</p>
</body>
</html>
'@

# Construct the SMTP client object, credentials, and send
$Smtp = New-Object Net.Mail.SmtpClient($SmtpServer)
$Smtp.EnableSsl = $true
$Smtp.Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
$Smtp.Send($Message)

Start-Sleep -s 15

Remove-Item $OutputPathList\PropertyErrorList_${GetDate}.csv

First off, if there isn’t a very good, specific reason that you aren’t using Send-MailMessage - then you should switch. Next, if you’re trying to avoid the email being sent if the attachment isn’t present or do something else (such as send a message to support) then you could consider something like this.

# Downloading the file needed for the email repport
$GetDate = Get-Date -Format 'yyyy-MM-dd'
$securepassword = ConvertTo-SecureString "Password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("Username", $securepassword)
$OutPutPathZip = 'C:\Temp\Zip'
$OutputPathCSV = 'C:\Temp\CSV'
$OutputPathList = 'C:\Temp\Download '

Invoke-WebRequest -Uri "https://webadress" -Credential $credentials -OutFile $OutPutPathZip\data_${GetDate}.zip
Expand-Archive $OutPutPathZip\*.zip -DestinationPath $OutputPathCSV 
Remove-Item $OutputPathZip\*.zip
Move-Item $OutputPathCSV\FileFromZipFile.txt -Destination $OutputPathList\FileFromZipFile_${GetDate}.csv
Remove-Item $OutputPathCSV\*.txt

# Processing the file that was downloaded.
Start-Process "C:\Program Files\QlikView\qv.exe" "/r C:\locationOfAOtherprogram.qvw"

Start-Sleep -s 15

#Sending the file that has now been processed.
#Variables
$GetDate = Get-Date -Format 'yyyy-MM-dd'
$Attachment = "C:\_${GetDate}.csv"

# Sender Credentials
$Username = "Username"
$Password = "Password"

# Message stuff
$Body = @'
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p style="text-align: left;">Hej</p>
<p style="text-align: left;">Here is the error list</p>
<p style="text-align: left;">/Daniel</p>
</body>
</html>
'@

'@

$mailparams = @{

    SMTPServer  = "servername"
    Credentials = New-Object System.Net.NetworkCredential($Username,$Password)
    From        = "Some User <sender@mail.com>"
    BCC         = "sender@mail.com"
    BodyAsHtml  = $true
    UseSSL      = $true
    Priority    = 'Normal'

}

$attempts = 1
Do 
{
    $file = "C:\_${GetDate}.csv"
    Start-Sleep -Seconds 1
    $attempts++
}
Until($attempts -eq 5 -or $file.count -gt 0)

if($file.count -gt 0)
{
    $mailparams += @{

        To      = "receiver@mail.com"
        Subject     = "Errorlist $GetDate " 
        Attachment  = $Attachment

    }
}
else
{
    $mailparams += @{

        To      = "Support Team <support@mail.com>"
        Subject = "Important attachement missing"

    }
}

Send-MailMessage @mailparams

Start-Sleep -s 15

Remove-Item $OutputPathList\PropertyErrorList_${GetDate}.csv