· 8 years ago · Oct 01, 2017, 03:34 PM
1<# : Batch portion
2@rem # The previous line does nothing in Batch, but begins a multiline comment block
3@rem # in PowerShell. This allows a single script to be executed by both interpreters.
4@echo off & Mode 100,5 & color 0A
5Title Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script by Hackoo
6echo(
7rem # This a Powershell command executes the hybrid portion at the bottom of this script
8for /f "delims=" %%I in ('powershell -noprofile "iex (${%~f0}|out-string)"') do set "%%I"
9exit /b
10rem # End multi-line PowerShell comment block. Begin PowerShell scripting.
11: end Batch / begin PowerShell hybrid code #>
12#################################### First 1 Step ###########################################
13# First Step we encrypt the Plain Text Password to an encrypted one using the key AES.key
14# Première étape, nous cryptons le mot de passe en clair vers un mot de passe chiffré
15# à l'aide de la clé AES.key
16$AppData = [Environment]::GetFolderPath('ApplicationData')
17$KeyFile = $AppData+"\AES.key"
18$Key = New-Object Byte[] 32 # You can use 16, 24, or 32 for AES
19[Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($Key)
20$Key | out-file $KeyFile
21$AppData = [Environment]::GetFolderPath('ApplicationData')
22$PasswordFile = $AppData+"\Password.txt"
23$Key = Get-Content $KeyFile
24$GmailUserName = Read-Host "Please enter your Gmail Account without ""@gmail.com"" " # Without @gmail.com
25$Password = Read-Host "Please enter your Gmail Password to be encrypted " -AsSecureString | ConvertFrom-SecureString -key $Key | Out-File $PasswordFile
26#################################### First 1 Step ###########################################
27
28#################################### Second 2 Step ##########################################
29# We send the email with our encrypted Credentials
30# Nous envoyons le courrier électronique avec les informations d'identification cryptés
31#############################################################################################
32$AppData = [Environment]::GetFolderPath('ApplicationData')
33$PasswordFile = $AppData+"\Password.txt"
34$keyFile = $AppData+"\AES.Key"
35$key = Get-Content $KeyFile
36$GmailEncryptedPassword = Get-Content $PasswordFile | ConvertTo-SecureString -Key $key
37$Credentials = New-Object -TypeName System.Management.Automation.PSCredential `
38-ArgumentList($GmailUserName,$GmailEncryptedPassword)
39$EmailFrom = $GmailUserName+"@gmail.com"
40$EmailTo = $EmailFrom
41$Subject = "Sending E-mail with SSL Authentification with an Hybrid code Batch and Powershell Script"
42$Body = (Get-Date -format F) + " Hello ! the sending email is working now with PowerShell and Batch Script!"
43$SMTPServer = "smtp.gmail.com"
44$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer,587)
45$SMTPClient.EnableSsl = $true
46$SMTPClient.Credentials = $Credentials
47$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)
48################################################################################################
49Function Show-BalloonTip {
50 [CmdletBinding(SupportsShouldProcess = $true)]
51 param (
52 [Parameter(Mandatory=$true)]$Text,
53 [Parameter(Mandatory=$true)]$Title,
54 [ValidateSet('None', 'Info', 'Warning', 'Error')]$Icon = 'Info',
55 $Timeout = 10000
56 )
57 Add-Type -AssemblyName System.Windows.Forms
58
59 if ($script:balloon -eq $null) { $script:balloon = New-Object System.Windows.Forms.NotifyIcon }
60
61 $path = Get-Process -id $pid | Select-Object -ExpandProperty Path
62 $balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
63 $balloon.BalloonTipIcon = $Icon
64 $balloon.BalloonTipText = $Text
65 $balloon.BalloonTipTitle = $Title
66 $balloon.Visible = $true
67 $balloon.ShowBalloonTip($Timeout)
68 Start-Sleep -s 5
69 $balloon.Dispose()
70}
71################################################################################################
72Show-BalloonTip -Text 'E-mail was sent, check your email' -Title 'E-mail was sent, check your email !' -Icon 'Info'