Announcement

Collapse
No announcement yet.

Unzip Password Protected ZIP File And Send It's Content By Email

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unzip Password Protected ZIP File And Send It's Content By Email

    Hi, there!!
    Here is my goal, I'm trying accomplish with Powershell script.
    I have administrative user that need to be able extract csv file from password protected zip file and send this csv file by email.
    Given directory has many zip files named by username (e.g. dana.zip) and protected with the same password (123456). The administrative user (who know the password for zip files) needs to run powershell script which asking for input desired user name and then do it's staff - extract file to the same directory and send it by email.
    So far I find and adopt for above needs following powershell scripts.

    Unzip Password protected file:

    Code:
    $7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
    $User = Read-Host -Prompt 'Please Input Desired User Name'
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    write-host " Desired file will be extracted to W:\ADMINISTRATION folder " -foregroundcolor Cyan
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    $zipFile = '"W:\ADMINISTRATION\$User.zip"'
    $zipFilePassword = "123456"
    $command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
    iex $command
    This script doing it's job, but I'm trying to avoid usage of password as plain text in the script. Since this script will be run under same administrative user account I've tried to use encrypted password file in the script.

    First, I've run following command to create encrypted password file:

    Code:
    "123456" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "W:\Admin\ZipPassword.txt"
    Afterwards I've adopt my script to use encrypted password file:

    Code:
    $7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
    $User = Read-Host -Prompt 'Please Input Desired User Name'
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    write-host " Desired file will be extracted to W:\\ADMINISTRATION folder " -foregroundcolor Cyan
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    $zipFile = '"W:\ADMINISTRATION\$User.zip"'
    $cred = Get-Content "W:\Admin\ZipPassword.txt" | ConvertTo-SecureString
    $zipFilePassword = new-object -typename System.Management.Automation.PSCredential -argumentlist ($cred)
    $command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
    iex $command
    When running this script I'm getting following error:

    7-Zip [64] 16.04 : Copyright (c) 1999-2016 Igor Pavlov : 2016-10-04

    Scanning the drive for archives:

    ERROR: The system cannot find the file specified.
    .Management.Automation.PSCredential


    System ERROR:
    The system cannot find the file specified.
    If it possible to make this script use encrypted password file it will me very beneficial...

    The second script - sending extracted file by email.

    First, I've created encrypted password file (in this script it 's working perfectly):

    Code:
    "myPassword" | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File "W:\Admin\EmailPassword.txt"
    And here is the script itself:

    Code:
    $User = "[email protected]"
    $File = "W:\Admin\EmailPassword.txt"
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = "[email protected]"
    $EmailFrom = "[email protected]"
    $Subject = "Some text here"
    $Body = "Some text here"
    $SMTPServer = "smtp.gmail.com"
    $filenameAndPath = "W:\ADMINISTRATION\dana.csv"
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host "Mail Sent Successfully !!"  -foregroundcolor Green
    This script working as expected... The only problem is that administrative user need edit it each time and put proper filename (dana.csv, david.csc... etc). Of cource I can use user input method in this script as well, but I want to combine both scripts into the single one... So far I tried this one:

    Code:
    $7ZipPath = '"C:\Program Files\7-Zip\7z.exe"'
    $User = Read-Host -Prompt 'Please Input Desired User Name'
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    write-host " Desired file will be extracted to W:\\ADMINISTRATION folder " -foregroundcolor Cyan
    write-host ""
    write-host " --------------------------------------------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    $zipFile = '"W:\ADMINISTRATION\$User.zip"'
    $zipFilePassword = "123456"
    $command = "& $7ZipPath e -oW:\ADMINISTRATION -y -tzip -p$zipFilePassword $zipFile"
    iex $command
    
    $User = "[email protected]"
    $File = "W:\Admin\EmailPassword.txt"
    $cred=New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $File | ConvertTo-SecureString)
    $EmailTo = "[email protected]"
    $EmailFrom = "[email protected]"
    $Subject = "Some text here"
    $Body = "Some text here"
    $SMTPServer = "smtp.gmail.com"
    $filenameAndPath = "W:\ADMINISTRATION\$User.csv"
    $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
    $Attachment = New-Object System.Net.Mail.Attachment($filenameAndPath)
    $SMTPMessage.Attachments.Add($attachment)
    $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
    $SMTPClient.EnableSsl = $true
    $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($cred.UserName, $cred.Password);
    $SMTPClient.Send($SMTPMessage)
    write-host "Mail Sent Successfully !!"  -foregroundcolor Green
    But it's failed to attach file to email. I think I have problem here (wrong syntax):

    Code:
    $filenameAndPath = "W:\ADMINISTRATION\$User.csv"
    So, if someone can help me to fix following issues, it will be much appreciated:

    1. In the first portion of script, use encrypted password file instead of plain text
    2. In the second portion, adopt user input from the first part of the script ($User) to be used as file name (for instance, if user input was "dana" , $User.csv will be equal to dana.csv)
    3. Remove *.csv file after mail was sent.

    Thank you in advance,

    Igor.



    Last edited by igor7; 11th September 2017, 14:30.

  • #2
    I've figured out what was wrong with issue #2, - I've using same variable $User in first portion of script and in second... so second $User variable override already existing, first one. That's why file wasn't attached to email. I've changed $User variable to $Username in firs portion of the script and now all good:

    Code:
    $Username = Read-Host -Prompt 'Please Input Desired User Name'....
    $zipFile = "W:\ADMINISTRATION\$UserName.zip"...
    $filenameAndPath = "W:\ADMINISTRATION\$UserName.csv"
    Issue #1: It looks like 7zip not supporting Windows-specific encrypted password, so I need convert it back to plain text before using it. I've found how to do it in those sites:https://blogs.msdn.microsoft.com/tim.../#comment-6495 and http://www.travisgan.com/2015/06/pow...ncryption.html

    So, this peace of code helped me out fixing encrypted password issue:

    Code:
    $Password = Get-Content 'W:\Administration\ZipPassword.txt'| ConvertTo-SecureString  
    $Marshal = [System.Runtime.InteropServices.Marshal]
    $Bstr = $Marshal::SecureStringToBSTR($Password)
    $ZipPassword = $Marshal::PtrToStringAuto($Bstr)
    $Marshal::ZeroFreeBSTR($Bstr)

    Issue #3: I've accomplished this goal using separate PowerShell script:

    Code:
    # Removing *.csv file after sending it by email
    
    Get-ChildItem W:\Administration\*.csv | Where { ! $_.PSIsContainer } | remove-item
    
    write-host " ---------------------------------------------- " -foregroundcolor DarkCyan
    write-host ""
    write-host "     All Operations Completed Successfully!!"     -foregroundcolor Green                    
    write-host ""
    write-host " ---------------------------------------------- " -foregroundcolor DarkCyan
    Hope, my experience will be useful for somebody else...
    Last edited by igor7; 12th September 2017, 14:51.

    Comment


    • #3
      Excellent job.

      Thanks for posting back with your outcome.

      Comment

      Working...
      X