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