We will use PowerShell to do the following:

Create a Random Password

Create a zip file and protect it using the random password

Send the zip file and the password on 2 separate emails

Creating a random password:

The script below will create a string of 8 characters which contains 2 Non Alpha Numeric Characters

[Reflection.Assembly]::LoadWithPartialName("System.Web") $randomPassword = [System.Web.Security.Membership]::GeneratePassword(8,2) 1 2 3 4 [ Reflection .Assembly ] :: LoadWithPartialName ( "System.Web" ) $randomPassword = [ System .Web .Security .Membership ] :: GeneratePassword ( 8 , 2 )

Creating the Zip file:

Note the arguments section where you speicfy the password

$arguments = "a -tzip ""$ZipOutputFilePath"" ""$FilesToZip"" -mx9 -p$randomPassword" $windowStyle = "Normal" $p = Start-Process $pathTo64Bit7Zip -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle 1 2 3 4 5 $arguments = "a -tzip " "$ZipOutputFilePath" " " "$FilesToZip" " -mx9 -p$randomPassword" $windowStyle = "Normal" $p = Start-Process $pathTo64Bit7Zip -ArgumentList $arguments -Wait -PassThru -WindowStyle $windowStyle

Sending the Emails:

First let’s send the csv file as attachment

$SMTPServer = "smtp.whatever.com" $Mailer = new-object Net.Mail.SMTPclient($SMTPServer) $From = "whatever@whatever.com" $To = "someone@some.com" $Subject = "CSV for $(get-date -f yyyy-MM-dd)" $Body = "BatMan Team" $Msg = new-object Net.Mail.MailMessage($From,$To,$Subject,$Body) $Msg.IsBodyHTML = $False $Attachment = new-object Net.Mail.Attachment($ZipOutputFilePath) $Msg.attachments.add($Attachment) $Mailer.send($Msg) $Attachment.Dispose() $Msg.Dispose() $Mailer.Dispose() 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 $SMTPServer = "smtp.whatever.com" $Mailer = new-object Net . Mail . SMTPclient ( $SMTPServer ) $From = "whatever@whatever.com" $To = "someone@some.com" $Subject = "CSV for $(get-date -f yyyy-MM-dd)" $Body = "BatMan Team" $Msg = new-object Net . Mail . MailMessage ( $From , $To , $Subject , $Body ) $Msg . IsBodyHTML = $False $Attachment = new-object Net . Mail . Attachment ( $ZipOutputFilePath ) $Msg . attachments . add ( $Attachment ) $Mailer . send ( $Msg ) $Attachment . Dispose ( ) $Msg . Dispose ( ) $Mailer . Dispose ( )

Now let’s send the password

$SMTPServer = "smtp.whatever.com" $Mailer = new-object Net.Mail.SMTPclient($SMTPServer) $From = "whatever@whatever.com" $To = "someone@some.com" $Subject = "CSV for $(get-date -f yyyy-MM-dd)" $Body = $randomPassword $Msg = new-object Net.Mail.MailMessage($From,$To,$Subject,$Body) $Msg.IsBodyHTML = $False $Mailer.send($Msg) $Msg.Dispose() $Mailer.Dispose() 1 2 3 4 5 6 7 8 9 10 11 12 13 $SMTPServer = "smtp.whatever.com" $Mailer = new-object Net . Mail . SMTPclient ( $SMTPServer ) $From = "whatever@whatever.com" $To = "someone@some.com" $Subject = "CSV for $(get-date -f yyyy-MM-dd)" $Body = $randomPassword $Msg = new-object Net . Mail . MailMessage ( $From , $To , $Subject , $Body ) $Msg . IsBodyHTML = $False $Mailer . send ( $Msg ) $Msg . Dispose ( ) $Mailer . Dispose ( )

Thanks for reading… 🙂

Follow me on Twitter for more tips about #coding, #learning, #technology, #Java, #JavaScript, #Autism, #Parenting…etc.

Check my Apps on Google Play