Email Service
Email Service
json" |
ConvertFrom-Json
#Write-Host "$config"
$mysqlPassword =$mysqlCred.Password
#Write-Host "$mysqlPassword"
$senderPassword =$senderCred.Password
#Write-Host "$senderPassword"
$mysqlPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropSe
rvices.Marshal]::SecureStringToBSTR($mysqlPassword))
$senderPassword =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropSe
rvices.Marshal]::SecureStringToBSTR($senderPassword))
try {
$mysqlConnection.Open()
$mysqlCommand = $mysqlConnection.CreateCommand()
$mysqlCommand.CommandText = $query
$mysqlCommand.ExecuteNonQuery()
if ($generatedFiles.Count -eq 0) {
# No files generated today, send email
$email = New-Object Net.Mail.MailMessage
$email.From = New-Object System.Net.Mail.MailAddress
$config.senderEmail.ToString().Trim()
$smtp.Send($email)
$smtp.Send($email)
$attachment.Dispose()
# extension changed to .bck after the csv file is sent.
$newFileName = $latestCsv.BaseName + ".bck"
$newFilePath = Join-Path $latestCsv.DirectoryName $newFileName
Move-Item -Path $latestCsv.FullName -Destination $newFilePath
# Only last three generated files are kept, rest are deleted.
$targetExtension = ".bck"
$allFiles = Get-ChildItem -Path $($config.csvDirectory) | Where-Object
{!$_.PSIsContainer}
$targetFiles = $allFiles | Where-Object { $_.Extension -eq
$targetExtension } | Sort-Object LastWriteTime -Descending
$filesToKeep = $targetFiles | Select-Object -First 3
} catch {
Write-Host "Error executing the stored procedure or checking for files: $_"
} finally {
$mysqlConnection.Close()
}