#######################################################
#    Send Annoying Emails
#    ***   Powered by BG   ***
#
# CMD :
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
# -File P:\Test\Powershell\Script\SendAnnoyingEmails.ps1
###################################

Write-Host "/// --- \\\" -ForegroundColor Cyan
Write-Host "Start sending annoying emails : ) Ready?" -ForegroundColor Cyan

# Input by User 1 :
Write-Host "Who is the victim's email address?" -BackgroundColor DarkMagenta
$Email = Read-Host "Email"
# baptiste.george-prestataire@ca-cib.com

# Input by User 2 :
Write-Host "How many times do you want to send this Email?" -BackgroundColor DarkMagenta
$Number = Read-Host "Total"

# Input by User 3 :
Write-Host "How often do we send emails (in seconds)?" -BackgroundColor DarkMagenta
$Seconds = Read-Host "Seconds"

Write-Host "Processing emails with your inputs..." -ForegroundColor Cyan

# Loop
For ($i=0; $i -lt $Number; $i++) {

# Email
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "$Email" #"ISEC-ISS-Controls@ca-cib.com"
$Mail.Subject = "[Error 401] [BOX EMAIL HACKED]"
$Mail.Body = "Hi boy,
 
Bla bla
Keep bla blating
ni night

Cheers
Your favorite hacker!"
$Mail.Send()
Write-Host "1 email has been sent successfully" -ForegroundColor Green

# On attend encore 30 secondes pour ensuite ranger les emails # rep auto
start-sleep -s $Seconds

}# End

Write-Host "\\\ --- ///" -ForegroundColor Cyan
Write-Host "The End" -ForegroundColor Cyan

#######################################################
#    Script ReCertification
#    ***   Move Emails From Shared inbox to subfolder   ***
#
# CMD :
# C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
# https://gallery.technet.microsoft.com/scriptcenter/Outlook-Automation-using-d7584688
# -File P:\Test\Powershell\Script\Recertification\ManagingEmails
###################################


Write-Host "/// --- \\\" -ForegroundColor Cyan
Write-Host "Start scanning Emails on Inbox 'ISEC-ISS'..." -ForegroundColor Cyan



# Date
$date = get-date -format d
$date = $date.ToString().Replace(“/”, “-”)
$time = get-date -format t
# $time = $time.ToString().Replace(":", "-")
$time = $time.ToString().Replace(" ", "")
Write-Host "Date:" $date $time -ForegroundColor Cyan





# Input by User 1 :
Write-Host "How old are emails (received time)? #0=Today; 1=From yesterday; ..." -BackgroundColor DarkMagenta
$NumberOfDays = Read-Host "Days"

# Input by User 2 :
Write-Host "What is the subject of emails are we looking for" -BackgroundColor DarkMagenta

Write-Host "Message de sécurité pour les utilisateurs du Système d’Information de CACIB" -ForegroundColor Cyan
Write-Host "Security message for all CACIB Information System users" -ForegroundColor Cyan
Write-Host "[RAPPEL] [POUR ACTION]" -ForegroundColor Cyan
Write-Host "[REMINDER] [ACTION REQUIRED]" -ForegroundColor Cyan

$Subject1 = Read-Host "Subject 1?"
$Subject2 = Read-Host "Subject 2?"
$Subject3 = Read-Host "Subject 3?"
$Subject4 = Read-Host "Subject 4?"

# Input by User 3 :
Write-Host "What is the folder destination?" -BackgroundColor DarkMagenta
Write-Host "13 - Welcome emails"  -ForegroundColor Cyan
Write-Host "23 - Recertification"  -ForegroundColor Cyan

$FolderDest = Read-Host "Target SubFolder"
# # # # #



# Parameter Emails "received time:"
$Date = [DateTime]::Now.AddDays(-$NumberOfDays)
$ReceivedDays =  $Date.tostring("MM/dd/yyyy")

# Parameter Subject:
# $subjectComparisonExpression = "Message de sécurité pour les utilisateurs du Système d’Information de CACIB / Security message for all CACIB Information System users"




# Connecting to Outlook Inbox
$ol = New-Object -ComObject Outlook.Application
$ns = $ol.GetNamespace('mapi')
$mb = $ns.Stores['baptiste.george-prestataire@ca-cib.com'].GetRootFolder()
$inbox = $mb.Folders['Inbox']
$recipient =  $ns.CreateRecipient('ISEC-ISS-Controls@ca-cib.com')



if($recipient.Resolve()){
    # Write-Host 'OK'
    $shared = $ns.GetSharedDefaultFolder($recipient, [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
}else{
     Write-Host 'Unable to resolve. Can not connect to sharedMailBox ISEC-ISS!'
     }


Write-Host "Query is running with user's input parameters..." -ForegroundColor Cyan



################################### Filter Emails #######################################
# $messages = $inbox.items
# $messages = $shared.items | Where-Object -FilterScript {$_.senton -Ge "$ReceivedDays" -and $_.Subject -eq $Subject1}
$messages = $shared.items | Where-Object -FilterScript {
        $_.senton -Ge "$ReceivedDays" -and ( ($_.Subject.SubString(0,[math]::min($Subject1.length,$_.Subject.length) ) -eq $Subject1) -or
                                              ($_.Subject.SubString(0,[math]::min($Subject2.length,$_.Subject.length) ) -eq $Subject2) -or
                                               ($_.Subject.SubString(0,[math]::min($Subject3.length,$_.Subject.length) ) -eq $Subject3) -or
                                                ($_.Subject.SubString(0,[math]::min($Subject4.length,$_.Subject.length) ) -eq $Subject4)
)}


# Query by User result :
# Count Emails into Inbox :
write-host "Query Result:" $messages.count "Emails were found in Shared Inbox" -ForegroundColor Green
$messcount = $messages.count



foreach($message in $messages){

     $moveTarget = $shared.Folders.item("$FolderDest")
     # $MoveTarget = $shared.Folders.item("23 - Recertification").Folders.item("Temp_Question")
    [void]$message.Move($MoveTarget)
    write-host "Email moved !" -BackgroundColor Blue


}






Write-Host "-->" $messcount "Emails moved to '$FolderDest' SubFolder" -ForegroundColor Green


 #Synthèse :
    Write-Host "End processing Emails from 'Inbox'" -ForegroundColor Cyan
    Write-Host "\\\ --- ///" -ForegroundColor Cyan






# Playing wav sound
$musicPath = "\\smb15-nas1.par.emea.cib\ISS_DATA\05 - Controls\I3C\2020\Task #6 - Recertification campaign\01 - Préparation\Baptiste\Sounds\Powershell Notification.wav"
$Song = New-Object System.Media.SoundPlayer
$Song.SoundLocation = $musicPath
$Song.Play()